Skip to content

Commit

Permalink
http: set socket.server unconditionally
Browse files Browse the repository at this point in the history
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 <zyszys98@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
addaleax committed Nov 30, 2019
1 parent d257448 commit 1d1d136
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,7 @@ function connectionListenerInternal(server, socket) {

// Ensure that the server property of the socket is correctly set.
// See https://github.com/nodejs/node/issues/13435
if (socket.server === null)
socket.server = server;
socket.server = server;

// If the user has added a listener to the server,
// request, or response, then it's their responsibility.
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-http-generic-streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,17 @@ const MakeDuplexPair = require('../common/duplexpair');
req.write(testData);
req.end();
}

// Test 5: The client sends garbage.
{
const server = http.createServer(common.mustNotCall());

const { clientSide, serverSide } = MakeDuplexPair();
server.emit('connection', serverSide);

server.on('clientError', common.mustCall());

// Send something that is not an HTTP request.
clientSide.end(
'I’m reading a book about anti-gravity. It’s impossible to put down!');
}

0 comments on commit 1d1d136

Please sign in to comment.