@@ -20,7 +20,6 @@ import {
2020} from '../requests/request-handlers' ;
2121import { getEffectivePort } from '../../util/url' ;
2222import { resetOrDestroy } from '../../util/socket-util' ;
23- import { LastHopEncrypted } from '../../util/socket-extensions' ;
2423import { isHttp2 } from '../../util/request-utils' ;
2524import {
2625 findRawHeader ,
@@ -39,7 +38,8 @@ import {
3938 getClientRelativeHostname ,
4039 getDnsLookupFunction ,
4140 shouldUseStrictHttps ,
42- getTrustedCAs
41+ getTrustedCAs ,
42+ getUrlHostname
4343} from '../passthrough-handling' ;
4444
4545import {
@@ -243,7 +243,9 @@ export class PassThroughWebSocketHandler extends PassThroughWebSocketHandlerDefi
243243 async handle ( req : OngoingRequest , socket : net . Socket , head : Buffer , options : RequestHandlerOptions ) {
244244 this . initializeWsServer ( ) ;
245245
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 ( ) ;
247249 const rawHeaders = req . rawHeaders ;
248250
249251 const reqMessage = req as unknown as http . IncomingMessage ;
@@ -287,27 +289,10 @@ export class PassThroughWebSocketHandler extends PassThroughWebSocketHandlerDefi
287289 hostHeader [ 1 ] = updateHostHeader ;
288290 } // Otherwise: falsey means don't touch it.
289291
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 } ` ;
304292 await this . connectUpstream ( wsUrl , reqMessage , rawHeaders , socket , head , options ) ;
305293 } else {
306294 // 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 } ` ;
311296 await this . connectUpstream ( wsUrl , reqMessage , rawHeaders , socket , head , options ) ;
312297 }
313298 }
@@ -407,12 +392,16 @@ export class PassThroughWebSocketHandler extends PassThroughWebSocketHandlerDefi
407392 }
408393 } ) . flat ( ) as RawHeaders ;
409394
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+
410399 options . emitEventCallback ( 'passthrough-websocket-connect' , {
411400 method : upstreamReq . method ,
412401 protocol : upstreamReq . protocol
413402 . replace ( / : $ / , '' )
414403 . replace ( / ^ h t t p / , 'ws' ) ,
415- hostname : upstreamReq . host ,
404+ hostname : urlHost ,
416405 port : effectivePort . toString ( ) ,
417406 path : upstreamReq . path ,
418407 rawHeaders : rawHeaders ,
0 commit comments