Skip to content

Commit a2486d5

Browse files
committed
http: ServerResponse.assignSocket should not throw an internal error
Signed-off-by: Matteo Collina <[email protected]>
1 parent b54504c commit a2486d5

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

lib/_http_server.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const {
7575
ERR_HTTP_HEADERS_SENT,
7676
ERR_HTTP_INVALID_STATUS_CODE,
7777
ERR_HTTP_SOCKET_ENCODING,
78+
ERR_HTTP_SOCKET_ASSIGNED,
7879
ERR_INVALID_ARG_VALUE,
7980
ERR_INVALID_CHAR,
8081
} = codes;
@@ -276,7 +277,9 @@ function onServerResponseClose() {
276277
}
277278

278279
ServerResponse.prototype.assignSocket = function assignSocket(socket) {
279-
assert(!socket._httpMessage);
280+
if (socket._httpMessage) {
281+
throw new ERR_HTTP_SOCKET_ASSIGNED();
282+
}
280283
socket._httpMessage = this;
281284
socket.on('close', onServerResponseClose);
282285
this.socket = socket;

lib/internal/errors.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,8 @@ E('ERR_HTTP_INVALID_STATUS_CODE', 'Invalid status code: %s', RangeError);
11691169
E('ERR_HTTP_REQUEST_TIMEOUT', 'Request timeout', Error);
11701170
E('ERR_HTTP_SOCKET_ENCODING',
11711171
'Changing the socket encoding is not allowed per RFC7230 Section 3.', Error);
1172+
E('ERR_HTTP_SOCKET_ASSIGNED',
1173+
'The socket was already assigned', Error);
11721174
E('ERR_HTTP_TRAILER_INVALID',
11731175
'Trailers are invalid with this transfer encoding', Error);
11741176
E('ERR_ILLEGAL_CONSTRUCTOR', 'Illegal constructor', TypeError);

test/parallel/test-http-server-response-standalone.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@ const ws = new Writable({
3131

3232
res.assignSocket(ws);
3333

34+
assert.throws(function() {
35+
res.assignSocket(ws);
36+
}, {
37+
code: 'ERR_HTTP_SOCKET_ASSIGNED'
38+
});
39+
3440
res.end('hello world');

0 commit comments

Comments
 (0)