From a2486d58526ec9c53b90a927709db432fe7d8794 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 26 Apr 2023 11:02:13 +0200 Subject: [PATCH] http: ServerResponse.assignSocket should not throw an internal error Signed-off-by: Matteo Collina --- lib/_http_server.js | 5 ++++- lib/internal/errors.js | 2 ++ test/parallel/test-http-server-response-standalone.js | 6 ++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/_http_server.js b/lib/_http_server.js index e6e592451b3230..774bdc368f4564 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -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; @@ -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; diff --git a/lib/internal/errors.js b/lib/internal/errors.js index aa4a149b189699..1fe63a5ef28bce 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -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); diff --git a/test/parallel/test-http-server-response-standalone.js b/test/parallel/test-http-server-response-standalone.js index ec6d1e89e38525..bc7ca56f894bde 100644 --- a/test/parallel/test-http-server-response-standalone.js +++ b/test/parallel/test-http-server-response-standalone.js @@ -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');