@@ -5,12 +5,19 @@ const createError = require('http-errors')
55module . exports = async function proxy ( ctx , client ) {
66 const { req, socket } = ctx
77
8+ const headers = getHeaders ( {
9+ headers : req . rawHeaders ,
10+ httpVersion : req . httpVersion ,
11+ socket : req . socket ,
12+ proxyName : ctx . proxyName
13+ } )
14+
815 if ( socket ) {
916 const handler = new WSHandler ( ctx )
1017 client . dispatch ( {
1118 method : req . method ,
1219 path : req . url ,
13- headers : getHeaders ( ctx ) ,
20+ headers,
1421 upgrade : 'Websocket'
1522 } , handler )
1623 return handler . promise
@@ -19,7 +26,7 @@ module.exports = async function proxy (ctx, client) {
1926 client . dispatch ( {
2027 method : req . method ,
2128 path : req . url ,
22- headers : getHeaders ( ctx ) ,
29+ headers,
2330 body : req
2431 } , handler )
2532 return handler . promise
@@ -28,8 +35,9 @@ module.exports = async function proxy (ctx, client) {
2835
2936class HTTPHandler {
3037 constructor ( ctx ) {
31- const { req, res } = ctx
38+ const { req, res, proxyName } = ctx
3239
40+ this . proxyName = proxyName
3341 this . req = req
3442 this . res = res
3543 this . resume = null
@@ -55,7 +63,11 @@ class HTTPHandler {
5563
5664 this . resume = resume
5765 this . res . on ( 'drain' , resume )
58- writeHead ( this . res , statusCode , getHeaders ( { headers } ) )
66+ writeHead ( this . res , statusCode , getHeaders ( {
67+ headers,
68+ proxyName : this . proxyName ,
69+ httpVersion : this . httpVersion
70+ } ) )
5971 }
6072
6173 onData ( chunk ) {
@@ -76,10 +88,12 @@ class HTTPHandler {
7688
7789class WSHandler {
7890 constructor ( ctx ) {
79- const { socket, head } = ctx
91+ const { req , socket, proxyName , head } = ctx
8092
8193 setupSocket ( socket )
8294
95+ this . proxyName = proxyName
96+ this . httpVersion = req . httpVersion
8397 this . socket = socket
8498 this . head = head
8599 this . abort = null
@@ -106,7 +120,11 @@ class WSHandler {
106120
107121 let head = 'HTTP/1.1 101 Switching Protocols\r\nconnection: upgrade\r\nupgrade: websocket'
108122
109- headers = getHeaders ( { headers } )
123+ headers = getHeaders ( {
124+ headers,
125+ proxyName : this . proxyName ,
126+ httpVersion : this . httpVersion
127+ } )
110128 for ( let n = 0 ; n < headers . length ; n += 2 ) {
111129 const key = headers [ n + 0 ]
112130 const val = headers [ n + 1 ]
@@ -140,13 +158,12 @@ const HOP_EXPR = /^(te|host|upgrade|trailers|connection|keep-alive|http2-setting
140158// Removes hop-by-hop and pseudo headers.
141159// Updates via and forwarded headers.
142160// Only hop-by-hop headers may be set using the Connection general header.
143- function getHeaders ( ctx ) {
144- const {
145- proxyName,
146- req,
147- headers = req ? req . rawHeaders : undefined
148- } = ctx
149-
161+ function getHeaders ( {
162+ headers,
163+ proxyName,
164+ httpVersion,
165+ socket
166+ } ) {
150167 let via = ''
151168 let forwarded = ''
152169 let host = ''
@@ -189,26 +206,23 @@ function getHeaders (ctx) {
189206 }
190207 }
191208
192- if ( req ) {
193- const { socket, httpVersion } = req
194-
209+ if ( socket ) {
195210 result . push ( 'forwarded' , ( forwarded ? forwarded + ', ' : '' ) + [
196211 `by=${ printIp ( socket . localAddress , socket . localPort ) } ` ,
197212 `for=${ printIp ( socket . remoteAddress , socket . remotePort ) } ` ,
198213 `proto=${ socket . encrypted ? 'https' : 'http' } ` ,
199214 `host=${ printIp ( authority || host || '' ) } `
200215 ] . join ( ';' ) )
216+ }
201217
202- // TODO (fix): Should also apply to responses.
203- if ( proxyName ) {
204- if ( via ) {
205- if ( via . split ( ',' ) . some ( name => name . endsWith ( proxyName ) ) ) {
206- throw new createError . LoopDetected ( )
207- }
208- via += ', '
218+ if ( httpVersion && proxyName ) {
219+ if ( via ) {
220+ if ( via . split ( ',' ) . some ( name => name . endsWith ( proxyName ) ) ) {
221+ throw new createError . LoopDetected ( )
209222 }
210- via += ` ${ httpVersion } ${ proxyName } `
223+ via += ', '
211224 }
225+ via += `${ httpVersion } ${ proxyName } `
212226 }
213227
214228 if ( via ) {
0 commit comments