MCPcopy Create free account
hub / github.com/nodejs/node / processResponse

Function processResponse

deps/undici/src/lib/web/websocket/connection.js:96–215  ·  view source on GitHub ↗
(response)

Source from the content-addressed store, hash-verified

94 useParallelQueue: true,
95 dispatcher: options.dispatcher,
96 processResponse (response) {
97 // 1. If response is a network error or its status is not 101,
98 // fail the WebSocket connection.
99 // if (response.type === 'error' || ((response.socket?.session != null && response.status !== 200) && response.status !== 101)) {
100 if (response.type === 'error' || response.status !== 101) {
101 // The presence of a session property on the socket indicates HTTP2
102 // HTTP1
103 if (response.socket?.session == null) {
104 failWebsocketConnection(handler, 1002, 'Received network error or non-101 status code.', response.error)
105 return
106 }
107
108 // HTTP2
109 if (response.status !== 200) {
110 failWebsocketConnection(handler, 1002, 'Received network error or non-200 status code.', response.error)
111 return
112 }
113 }
114
115 if (warningEmitted === false && response.socket?.session != null) {
116 process.emitWarning('WebSocket over HTTP2 is experimental, and subject to change.', 'ExperimentalWarning')
117 warningEmitted = true
118 }
119
120 // 2. If protocols is not the empty list and extracting header
121 // list values given `Sec-WebSocket-Protocol` and response’s
122 // header list results in null, failure, or the empty byte
123 // sequence, then fail the WebSocket connection.
124 if (protocols.length !== 0 && !response.headersList.get('Sec-WebSocket-Protocol')) {
125 failWebsocketConnection(handler, 1002, 'Server did not respond with sent protocols.')
126 return
127 }
128
129 // 3. Follow the requirements stated step 2 to step 6, inclusive,
130 // of the last set of steps in section 4.1 of The WebSocket
131 // Protocol to validate response. This either results in fail
132 // the WebSocket connection or the WebSocket connection is
133 // established.
134
135 // 2. If the response lacks an |Upgrade| header field or the |Upgrade|
136 // header field contains a value that is not an ASCII case-
137 // insensitive match for the value "websocket", the client MUST
138 // _Fail the WebSocket Connection_.
139 // For H2, no upgrade header is expected.
140 if (response.socket.session == null && response.headersList.get('Upgrade')?.toLowerCase() !== 'websocket') {
141 failWebsocketConnection(handler, 1002, 'Server did not set Upgrade header to "websocket".')
142 return
143 }
144
145 // 3. If the response lacks a |Connection| header field or the
146 // |Connection| header field doesn't contain a token that is an
147 // ASCII case-insensitive match for the value "Upgrade", the client
148 // MUST _Fail the WebSocket Connection_.
149 // For H2, no connection header is expected.
150 if (response.socket.session == null && response.headersList.get('Connection')?.toLowerCase() !== 'upgrade') {
151 failWebsocketConnection(handler, 1002, 'Server did not set Connection header to "upgrade".')
152 return
153 }

Callers

nothing calls this directly

Calls 8

hashMethod · 0.80
includesMethod · 0.80
failWebsocketConnectionFunction · 0.70
parseExtensionsFunction · 0.70
getMethod · 0.65
hasMethod · 0.65
getDecodeSplitFunction · 0.50
onMethod · 0.45

Tested by

no test coverage detected