diff --git a/doc/api/errors.md b/doc/api/errors.md index e51982b934f204..d5fa7f43005ad8 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1431,6 +1431,12 @@ Status code was outside the regular status code range (100-999). The client has not sent the entire request within the allowed time. + + +### `ERR_HTTP_SOCKET_ASSIGNED` + +The given [`ServerResponse`][] was already assigned a socket. + ### `ERR_HTTP_SOCKET_ENCODING` @@ -3565,6 +3571,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 diff --git a/lib/_http_server.js b/lib/_http_server.js index c248a9714faff7..b38e6cb1ab9e17 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -76,6 +76,7 @@ const { ERR_HTTP_INVALID_STATUS_CODE, ERR_HTTP_SOCKET_ENCODING, ERR_INVALID_ARG_TYPE, + ERR_HTTP_SOCKET_ASSIGNED, ERR_INVALID_ARG_VALUE, ERR_INVALID_CHAR, } = codes; @@ -279,7 +280,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 390b58b2d05c0a..be91a4b1f917e5 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -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', 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');