Skip to content

Commit b56794f

Browse files
committed
fixup
1 parent 656c28c commit b56794f

2 files changed

Lines changed: 11 additions & 13 deletions

File tree

examples/proxy/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async function run () {
1919
for (const name of res.getHeaderNames()) {
2020
res.removeHeader(name)
2121
}
22-
res.writeHead(err.statusCode || 500)
22+
res.statusCode = err.statusCode || 500
2323
res.end()
2424
}
2525
})

examples/proxy/proxy.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ const { pipeline } = require('stream')
33
const createError = require('http-errors')
44

55
module.exports = async function proxy (ctx, client) {
6-
const { req, socket } = ctx
6+
const { req, socket, proxyName } = ctx
77

88
const headers = getHeaders({
99
headers: req.rawHeaders,
1010
httpVersion: req.httpVersion,
1111
socket: req.socket,
12-
proxyName: ctx.proxyName
12+
proxyName
1313
})
1414

1515
if (socket) {
@@ -112,6 +112,8 @@ class WSHandler {
112112
}
113113

114114
onUpgrade (statusCode, headers, socket) {
115+
// TODO: Check statusCode?
116+
115117
if (this.head && this.head.length) {
116118
socket.unshift(this.head)
117119
}
@@ -125,17 +127,12 @@ class WSHandler {
125127
proxyName: this.proxyName,
126128
httpVersion: this.httpVersion
127129
})
130+
128131
for (let n = 0; n < headers.length; n += 2) {
129132
const key = headers[n + 0]
130133
const val = headers[n + 1]
131134

132-
if (!Array.isArray(val)) {
133-
head += `\r\n${key}: ${val}`
134-
} else {
135-
for (let i = 0; i < val.length; i++) {
136-
head += `\r\n${key}: ${val[i]}`
137-
}
138-
}
135+
head += `\r\n${key}: ${val}`
139136
}
140137
head += '\r\n\r\n'
141138

@@ -213,11 +210,12 @@ function getHeaders ({
213210
`proto=${socket.encrypted ? 'https' : 'http'}`,
214211
`host=${printIp(authority || host || '')}`
215212
].join(';'))
216-
} else {
217-
result.push('forwarded', forwarded)
213+
} else if (forwarded) {
214+
// The forwarded header should not be included in response.
215+
throw new createError.BadGateway()
218216
}
219217

220-
if (httpVersion && proxyName) {
218+
if (proxyName) {
221219
if (via) {
222220
if (via.split(',').some(name => name.endsWith(proxyName))) {
223221
throw new createError.LoopDetected()

0 commit comments

Comments
 (0)