Skip to content

Commit

Permalink
http: ServerResponse.assignSocket should not throw an internal error
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina committed Apr 26, 2023
1 parent b54504c commit a2486d5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
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 @@ -1169,6 +1169,8 @@ E('ERR_HTTP_INVALID_STATUS_CODE', 'Invalid status code: %s', RangeError);
E('ERR_HTTP_REQUEST_TIMEOUT', 'Request timeout', Error);
E('ERR_HTTP_SOCKET_ENCODING',
'Changing the socket encoding is not allowed per RFC7230 Section 3.', Error);
E('ERR_HTTP_SOCKET_ASSIGNED',
'The socket was already assigned', Error);
E('ERR_HTTP_TRAILER_INVALID',
'Trailers are invalid with this transfer encoding', Error);
E('ERR_ILLEGAL_CONSTRUCTOR', 'Illegal constructor', TypeError);
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');

0 comments on commit a2486d5

Please sign in to comment.