diff --git a/lib/_http_server.js b/lib/_http_server.js index be55784e7e..80880be46b 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -328,9 +328,11 @@ function connectionListener(socket) { // Override on to unconsume on `data`, `readable` listeners socket.on = socketOnWrap; + // We only consume the socket if it has never been consumed before. var external = socket._handle._externalStream; - if (external) { + if (!socket._handle._consumed && external) { parser._consumed = true; + socket._handle._consumed = true; parser.consume(external); } parser[kOnExecute] = diff --git a/test/parallel/test-http-server-unconsume-consume.js b/test/parallel/test-http-server-unconsume-consume.js new file mode 100644 index 0000000000..77baff19fe --- /dev/null +++ b/test/parallel/test-http-server-unconsume-consume.js @@ -0,0 +1,22 @@ +'use strict'; +const common = require('../common'); +const http = require('http'); + +const testServer = http.createServer((req, res) => { + common.fail('Should not be called'); + res.end(); +}); +testServer.on('connect', common.mustCall((req, socket, head) => { + socket.write('HTTP/1.1 200 Connection Established' + '\r\n' + + 'Proxy-agent: Node-Proxy' + '\r\n' + + '\r\n'); + // This shouldn't raise an assertion in StreamBase::Consume. + testServer.emit('connection', socket); + testServer.close(); +})); +testServer.listen(0, common.mustCall(() => { + http.request({ + port: testServer.address().port, + method: 'CONNECT' + }, (res) => {}).end(); +}));