Skip to content

Commit 4871779

Browse files
committed
http: set socket.server unconditionally
This is useful for situations in which the socket was not created for HTTP, e.g. when using arbitrary `Duplex` streams. (The added test fails because previously, `socket.server.emit()` would not work for emitting the `clientError` event, as `socket.server` was `undefined`.) PR-URL: nodejs#30571 Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent a7d031b commit 4871779

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/_http_server.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,7 @@ function connectionListenerInternal(server, socket) {
363363

364364
// Ensure that the server property of the socket is correctly set.
365365
// See https://github.com/nodejs/node/issues/13435
366-
if (socket.server === null)
367-
socket.server = server;
366+
socket.server = server;
368367

369368
// If the user has added a listener to the server,
370369
// request, or response, then it's their responsibility.

test/parallel/test-http-generic-streams.js

+14
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,17 @@ const MakeDuplexPair = require('../common/duplexpair');
138138
req.write(testData);
139139
req.end();
140140
}
141+
142+
// Test 5: The client sends garbage.
143+
{
144+
const server = http.createServer(common.mustNotCall());
145+
146+
const { clientSide, serverSide } = MakeDuplexPair();
147+
server.emit('connection', serverSide);
148+
149+
server.on('clientError', common.mustCall());
150+
151+
// Send something that is not an HTTP request.
152+
clientSide.end(
153+
'I’m reading a book about anti-gravity. It’s impossible to put down!');
154+
}

0 commit comments

Comments
 (0)