Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: ServerResponse.assignSocket should not throw an internal error #47723

Merged
merged 1 commit into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,12 @@ Status code was outside the regular status code range (100-999).

The client has not sent the entire request within the allowed time.

<a id="ERR_HTTP_SOCKET_ASSIGNED"></a>

### `ERR_HTTP_SOCKET_ASSIGNED`

The given [`ServerResponse`][] was already assigned a socket.

<a id="ERR_HTTP_SOCKET_ENCODING"></a>

### `ERR_HTTP_SOCKET_ENCODING`
Expand Down Expand Up @@ -3590,6 +3596,7 @@ The native call from `process.cpuUsage` could not be processed.
[`Object.getPrototypeOf`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf
[`Object.setPrototypeOf`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf
[`REPL`]: repl.md
[`ServerResponse`]: http.md#class-httpserverresponse
[`Writable`]: stream.md#class-streamwritable
[`child_process`]: child_process.md
[`cipher.getAuthTag()`]: crypto.md#ciphergetauthtag
Expand Down
5 changes: 4 additions & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const {
ERR_HTTP_HEADERS_SENT,
ERR_HTTP_INVALID_STATUS_CODE,
ERR_HTTP_SOCKET_ENCODING,
ERR_HTTP_SOCKET_ASSIGNED,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_CHAR,
} = codes;
Expand Down Expand Up @@ -276,7 +277,9 @@ function onServerResponseClose() {
}

ServerResponse.prototype.assignSocket = function assignSocket(socket) {
assert(!socket._httpMessage);
if (socket._httpMessage) {
throw new ERR_HTTP_SOCKET_ASSIGNED();
}
socket._httpMessage = this;
socket.on('close', onServerResponseClose);
this.socket = socket;
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,8 @@ E('ERR_HTTP_INVALID_HEADER_VALUE',
'Invalid value "%s" for header "%s"', TypeError);
E('ERR_HTTP_INVALID_STATUS_CODE', 'Invalid status code: %s', RangeError);
E('ERR_HTTP_REQUEST_TIMEOUT', 'Request timeout', Error);
E('ERR_HTTP_SOCKET_ASSIGNED',
'ServerResponse has an already assigned socket', Error);
E('ERR_HTTP_SOCKET_ENCODING',
'Changing the socket encoding is not allowed per RFC7230 Section 3.', Error);
E('ERR_HTTP_TRAILER_INVALID',
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-http-server-response-standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ const ws = new Writable({

res.assignSocket(ws);

assert.throws(function() {
res.assignSocket(ws);
}, {
code: 'ERR_HTTP_SOCKET_ASSIGNED'
});

res.end('hello world');