Skip to content

Commit e2f8d23

Browse files
addaleaxtargos
authored andcommitted
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: #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 8a92830 commit e2f8d23

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
@@ -369,8 +369,7 @@ function connectionListenerInternal(server, socket) {
369369

370370
// Ensure that the server property of the socket is correctly set.
371371
// See https://github.com/nodejs/node/issues/13435
372-
if (socket.server === null)
373-
socket.server = server;
372+
socket.server = server;
374373

375374
// If the user has added a listener to the server,
376375
// 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)