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 <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
addaleax committed Nov 29, 2019
1 parent a7d031b commit 4871779
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 @@ -363,8 +363,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 4871779

Please sign in to comment.