@@ -101,13 +101,13 @@ function establishWebSocketConnection (url, protocols, client, handler, options)
101101 // The presence of a session property on the socket indicates HTTP2
102102 // HTTP1
103103 if ( response . socket ?. session == null ) {
104- failWebsocketConnection ( handler , 1002 , 'Received network error or non-101 status code.' , response . error )
104+ failHandshake ( handler , response , 1002 , 'Received network error or non-101 status code.' , response . error )
105105 return
106106 }
107107
108108 // HTTP2
109109 if ( response . status !== 200 ) {
110- failWebsocketConnection ( handler , 1002 , 'Received network error or non-200 status code.' , response . error )
110+ failHandshake ( handler , response , 1002 , 'Received network error or non-200 status code.' , response . error )
111111 return
112112 }
113113 }
@@ -122,7 +122,7 @@ function establishWebSocketConnection (url, protocols, client, handler, options)
122122 // header list results in null, failure, or the empty byte
123123 // sequence, then fail the WebSocket connection.
124124 if ( protocols . length !== 0 && ! response . headersList . get ( 'Sec-WebSocket-Protocol' ) ) {
125- failWebsocketConnection ( handler , 1002 , 'Server did not respond with sent protocols.' )
125+ failHandshake ( handler , response , 1002 , 'Server did not respond with sent protocols.' )
126126 return
127127 }
128128
@@ -138,7 +138,7 @@ function establishWebSocketConnection (url, protocols, client, handler, options)
138138 // _Fail the WebSocket Connection_.
139139 // For H2, no upgrade header is expected.
140140 if ( response . socket . session == null && response . headersList . get ( 'Upgrade' ) ?. toLowerCase ( ) !== 'websocket' ) {
141- failWebsocketConnection ( handler , 1002 , 'Server did not set Upgrade header to "websocket".' )
141+ failHandshake ( handler , response , 1002 , 'Server did not set Upgrade header to "websocket".' )
142142 return
143143 }
144144
@@ -148,7 +148,7 @@ function establishWebSocketConnection (url, protocols, client, handler, options)
148148 // MUST _Fail the WebSocket Connection_.
149149 // For H2, no connection header is expected.
150150 if ( response . socket . session == null && response . headersList . get ( 'Connection' ) ?. toLowerCase ( ) !== 'upgrade' ) {
151- failWebsocketConnection ( handler , 1002 , 'Server did not set Connection header to "upgrade".' )
151+ failHandshake ( handler , response , 1002 , 'Server did not set Connection header to "upgrade".' )
152152 return
153153 }
154154
@@ -165,7 +165,7 @@ function establishWebSocketConnection (url, protocols, client, handler, options)
165165 const secWSAccept = response . headersList . get ( 'Sec-WebSocket-Accept' )
166166 const digest = crypto . hash ( 'sha1' , keyValue + uid , 'base64' )
167167 if ( secWSAccept !== digest ) {
168- failWebsocketConnection ( handler , 1002 , 'Incorrect hash received in Sec-WebSocket-Accept header.' )
168+ failHandshake ( handler , response , 1002 , 'Incorrect hash received in Sec-WebSocket-Accept header.' )
169169 return
170170 }
171171 }
@@ -184,7 +184,7 @@ function establishWebSocketConnection (url, protocols, client, handler, options)
184184 extensions = parseExtensions ( secExtension )
185185
186186 if ( ! extensions . has ( 'permessage-deflate' ) ) {
187- failWebsocketConnection ( handler , 1002 , 'Sec-WebSocket-Extensions header does not match.' )
187+ failHandshake ( handler , response , 1002 , 'Sec-WebSocket-Extensions header does not match.' )
188188 return
189189 }
190190 }
@@ -205,7 +205,7 @@ function establishWebSocketConnection (url, protocols, client, handler, options)
205205 // the selected subprotocol values in its response for the connection to
206206 // be established.
207207 if ( requestProtocols === null || ! requestProtocols . includes ( secProtocol ) ) {
208- failWebsocketConnection ( handler , 1002 , 'Protocol was not set in the opening handshake.' )
208+ failHandshake ( handler , response , 1002 , 'Protocol was not set in the opening handshake.' )
209209 return
210210 }
211211 }
@@ -307,6 +307,16 @@ function closeWebSocketConnection (object, code, reason, validate = false) {
307307 }
308308}
309309
310+ function failHandshake ( handler , response , code , reason , cause ) {
311+ // The H2 upgrade request has already completed and handed off its stream.
312+ // Aborting the request cannot close that stream after handshake validation fails.
313+ if ( response . socket ?. session != null && ! response . socket . destroyed ) {
314+ response . socket . destroy ( )
315+ }
316+
317+ failWebsocketConnection ( handler , code , reason , cause )
318+ }
319+
310320/**
311321 * @param {import('./websocket').Handler } handler
312322 * @param {number } code
0 commit comments