summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatteo Collina <hello@matteocollina.com>2023-05-02 22:02:17 +0200
committerGitHub <noreply@github.com>2023-05-02 20:02:17 +0000
commit46c3f4da05e2b5ddbc9100ca170c479575edf013 (patch)
treebfb79ada1e7dac81302fc71d50492e3163a45bda
parentd2156f1bf0130d85ed39886f13c042f801640f55 (diff)
downloadnode-new-46c3f4da05e2b5ddbc9100ca170c479575edf013.tar.gz
http: remove internal error in assignSocket
Change ServerResponse.assignSocket to not throw an internal error, but an error with its own code. Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: https://github.com/nodejs/node/pull/47723 Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
-rw-r--r--doc/api/errors.md7
-rw-r--r--lib/_http_server.js5
-rw-r--r--lib/internal/errors.js2
-rw-r--r--test/parallel/test-http-server-response-standalone.js6
4 files changed, 19 insertions, 1 deletions
diff --git a/doc/api/errors.md b/doc/api/errors.md
index 2336274e70..992ab1e7a2 100644
--- a/doc/api/errors.md
+++ b/doc/api/errors.md
@@ -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`
@@ -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
diff --git a/lib/_http_server.js b/lib/_http_server.js
index e6e592451b..774bdc368f 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 9478f1adce..18f4a2c42c 100644
--- a/lib/internal/errors.js
+++ b/lib/internal/errors.js
@@ -1162,6 +1162,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 ec6d1e89e3..bc7ca56f89 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');