@@ -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
@@ -162,7 +162,7 @@ function establishWebSocketConnection (url, protocols, client, handler, options)
162162 const secWSAccept = response . headersList . get ( 'Sec-WebSocket-Accept' )
163163 const digest = crypto . hash ( 'sha1' , keyValue + uid , 'base64' )
164164 if ( secWSAccept !== digest ) {
165- failWebsocketConnection ( handler , 1002 , 'Incorrect hash received in Sec-WebSocket-Accept header.' )
165+ failHandshake ( handler , response , 1002 , 'Incorrect hash received in Sec-WebSocket-Accept header.' )
166166 return
167167 }
168168
@@ -180,7 +180,7 @@ function establishWebSocketConnection (url, protocols, client, handler, options)
180180 extensions = parseExtensions ( secExtension )
181181
182182 if ( ! extensions . has ( 'permessage-deflate' ) ) {
183- failWebsocketConnection ( handler , 1002 , 'Sec-WebSocket-Extensions header does not match.' )
183+ failHandshake ( handler , response , 1002 , 'Sec-WebSocket-Extensions header does not match.' )
184184 return
185185 }
186186 }
@@ -201,7 +201,7 @@ function establishWebSocketConnection (url, protocols, client, handler, options)
201201 // the selected subprotocol values in its response for the connection to
202202 // be established.
203203 if ( requestProtocols === null || ! requestProtocols . includes ( secProtocol ) ) {
204- failWebsocketConnection ( handler , 1002 , 'Protocol was not set in the opening handshake.' )
204+ failHandshake ( handler , response , 1002 , 'Protocol was not set in the opening handshake.' )
205205 return
206206 }
207207 }
@@ -296,6 +296,16 @@ function closeWebSocketConnection (object, code, reason, validate = false) {
296296 }
297297}
298298
299+ function failHandshake ( handler , response , code , reason , cause ) {
300+ // The H2 upgrade request has already completed and handed off its stream.
301+ // Aborting the request cannot close that stream after handshake validation fails.
302+ if ( response . socket ?. session != null && ! response . socket . destroyed ) {
303+ response . socket . destroy ( )
304+ }
305+
306+ failWebsocketConnection ( handler , code , reason , cause )
307+ }
308+
299309/**
300310 * @param {import('./websocket').Handler } handler
301311 * @param {number } code
0 commit comments