@@ -20,7 +20,6 @@ import {
20
20
} from '../requests/request-handlers' ;
21
21
import { getEffectivePort } from '../../util/url' ;
22
22
import { resetOrDestroy } from '../../util/socket-util' ;
23
- import { LastHopEncrypted } from '../../util/socket-extensions' ;
24
23
import { isHttp2 } from '../../util/request-utils' ;
25
24
import {
26
25
findRawHeader ,
@@ -39,7 +38,8 @@ import {
39
38
getClientRelativeHostname ,
40
39
getDnsLookupFunction ,
41
40
shouldUseStrictHttps ,
42
- getTrustedCAs
41
+ getTrustedCAs ,
42
+ getUrlHostname
43
43
} from '../passthrough-handling' ;
44
44
45
45
import {
@@ -243,7 +243,9 @@ export class PassThroughWebSocketHandler extends PassThroughWebSocketHandlerDefi
243
243
async handle ( req : OngoingRequest , socket : net . Socket , head : Buffer , options : RequestHandlerOptions ) {
244
244
this . initializeWsServer ( ) ;
245
245
246
- let { protocol, hostname, port, path } = url . parse ( req . url ! ) ;
246
+ let { protocol, path } = url . parse ( req . url ! ) ;
247
+ let hostname : string | null = req . destination . hostname ;
248
+ let port : string | null = req . destination . port . toString ( ) ;
247
249
const rawHeaders = req . rawHeaders ;
248
250
249
251
const reqMessage = req as unknown as http . IncomingMessage ;
@@ -287,27 +289,10 @@ export class PassThroughWebSocketHandler extends PassThroughWebSocketHandlerDefi
287
289
hostHeader [ 1 ] = updateHostHeader ;
288
290
} // Otherwise: falsey means don't touch it.
289
291
290
- await this . connectUpstream ( wsUrl , reqMessage , rawHeaders , socket , head , options ) ;
291
- } else if ( ! hostname ) { // No hostname in URL means transparent proxy, so use Host header
292
- const hostHeader = req . headers [ hostHeaderName ] ;
293
- [ hostname , port ] = hostHeader ! . split ( ':' ) ;
294
-
295
- // LastHopEncrypted is set in http-combo-server, for requests that use TLS in the
296
- // inner-most tunnel (or direct connection) to us.
297
- if ( socket [ LastHopEncrypted ] !== undefined ) {
298
- protocol = socket [ LastHopEncrypted ] ? 'wss' : 'ws' ;
299
- } else {
300
- protocol = reqMessage . connection . encrypted ? 'wss' : 'ws' ;
301
- }
302
-
303
- const wsUrl = `${ protocol } ://${ hostname } ${ port ? ':' + port : '' } ${ path } ` ;
304
292
await this . connectUpstream ( wsUrl , reqMessage , rawHeaders , socket , head , options ) ;
305
293
} else {
306
294
// Connect directly according to the specified URL
307
- const wsUrl = `${
308
- protocol ! . replace ( 'http' , 'ws' )
309
- } //${ hostname } ${ port ? ':' + port : '' } ${ path } `;
310
-
295
+ const wsUrl = `${ protocol } //${ hostname } ${ port ? ':' + port : '' } ${ path } ` ;
311
296
await this . connectUpstream ( wsUrl , reqMessage , rawHeaders , socket , head , options ) ;
312
297
}
313
298
}
@@ -407,12 +392,16 @@ export class PassThroughWebSocketHandler extends PassThroughWebSocketHandlerDefi
407
392
}
408
393
} ) . flat ( ) as RawHeaders ;
409
394
395
+ // This effectively matches the URL preprocessing logic in MockttpServer.preprocessRequest,
396
+ // so that the resulting event matches the req.url property elsewhere.
397
+ const urlHost = getUrlHostname ( upstreamReq . host , rawHeaders ) ;
398
+
410
399
options . emitEventCallback ( 'passthrough-websocket-connect' , {
411
400
method : upstreamReq . method ,
412
401
protocol : upstreamReq . protocol
413
402
. replace ( / : $ / , '' )
414
403
. replace ( / ^ h t t p / , 'ws' ) ,
415
- hostname : upstreamReq . host ,
404
+ hostname : urlHost ,
416
405
port : effectivePort . toString ( ) ,
417
406
path : upstreamReq . path ,
418
407
rawHeaders : rawHeaders ,
0 commit comments