Skip to content

Commit fddfabc

Browse files
committed
fixu
1 parent b58713f commit fddfabc

1 file changed

Lines changed: 32 additions & 24 deletions

File tree

examples/proxy/proxy.js

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class HTTPHandler {
5555

5656
this.resume = resume
5757
this.res.on('drain', resume)
58-
writeHead(this.res, statusCode, getResponseHeaders(headers))
58+
writeHead(this.res, statusCode, copyHeaders(headers, null))
5959
}
6060

6161
onData (chunk) {
@@ -102,7 +102,7 @@ class WSHandler {
102102
socket.unshift(this.head)
103103
}
104104

105-
headers = getResponseHeaders(headers)
105+
headers = copyHeaders(headers, null)
106106

107107
setupSocket(socket)
108108

@@ -133,24 +133,16 @@ class WSHandler {
133133
}
134134
}
135135

136-
// This expression matched hop-by-hop headers.
137-
// These headers are meaningful only for a single transport-level connection,
138-
// and must not be retransmitted by proxies or cached.
139-
// Note that only hop-by-hop headers may be set using the Connection general header.
140-
const HOP_EXPR = /^(te|host|upgrade|trailers|connection|keep-alive|http2-settings|transfer-encoding|proxy-connection|proxy-authenticate|proxy-authorization)$/i
141-
142-
// Removes hop-by-hop headers and updates the via and forwarded headers.
143-
// Also removes http2 pseudo headers.
136+
// Update or set forwarded and via headers.
144137
function getRequestHeaders (ctx) {
145138
const { req, proxyName } = ctx
146139
const { rawHeaders: headers, socket } = req
147140

148-
const result = []
149-
150141
let via = ''
151142
let forwarded = ''
152143
let host = ''
153144
let authority = ''
145+
let connection = ''
154146

155147
for (let n = 0; n < headers.length; n += 2) {
156148
const key = headers[n + 0]
@@ -164,11 +156,13 @@ function getRequestHeaders (ctx) {
164156
forwarded = val
165157
} else if (!authority && key.length === 10 && /^:authority$/i.test(key)) {
166158
authority = val
167-
} else if (key.charAt(0) !== ':' && !HOP_EXPR.test(key)) {
168-
result.push(key, val)
159+
} else if (!connection && key.length === 10 && /^connection$/i.test(key)) {
160+
connection = val
169161
}
170162
}
171163

164+
const result = copyHeaders(headers)
165+
172166
// TODO(fix): <host> [ ":" <port> ] vs <pseudonym>
173167
// See, https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Via.
174168
if (proxyName) {
@@ -195,25 +189,39 @@ function getRequestHeaders (ctx) {
195189
return result
196190
}
197191

198-
function getResponseHeaders (headers) {
199-
let remove
200-
for (let n = 0; n < headers.length; n += 2) {
201-
const key = headers[n + 0]
202-
if (key.length === 10 && /^connection$/i.test(key)) {
203-
const val = headers[n + 1]
204-
if (!HOP_EXPR.test(val)) {
205-
remove = val.split(',')
192+
// This expression matched hop-by-hop headers.
193+
// These headers are meaningful only for a single transport-level connection,
194+
// and must not be retransmitted by proxies or cached.
195+
// Note that only hop-by-hop headers may be set using the Connection general header.
196+
const HOP_EXPR = /^(te|host|upgrade|trailers|connection|keep-alive|http2-settings|transfer-encoding|proxy-connection|proxy-authenticate|proxy-authorization)$/i
197+
198+
// Removes hop-by-hop and pseudo headers.
199+
function copyHeaders (headers, connection) {
200+
if (!connection) {
201+
for (let n = 0; n < headers.length; n += 2) {
202+
const key = headers[n + 0]
203+
if (key.length === 10 && /^connection$/i.test(key)) {
204+
connection = headers[n + 1]
205+
break
206206
}
207-
break
208207
}
209208
}
210209

210+
let remove
211+
if (connection && !HOP_EXPR.test(connection)) {
212+
remove = connection.split(/,\s*/)
213+
}
214+
211215
const result = []
212216
for (let n = 0; n < headers.length; n += 2) {
213217
const key = headers[n + 0]
214218
const val = headers[n + 1]
215219

216-
if (!HOP_EXPR.test(key) && (!remove || !remove.includes(key))) {
220+
if (
221+
key.charAt(0) !== ':' &&
222+
!HOP_EXPR.test(key) &&
223+
(!remove || !remove.includes(key))
224+
) {
217225
result.push(key, val)
218226
}
219227
}

0 commit comments

Comments
 (0)