Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: fix socketOnWrap edge cases #27968

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,10 @@ function connectionListenerInternal(server, socket) {
socket.on('resume', onSocketResume);
socket.on('pause', onSocketPause);

// Override on to unconsume on `data`, `readable` listeners
socket.on = socketOnWrap;
socket.addListener = socket.on;
// Overrides to unconsume on `data`, `readable` listeners
socket.on = generateSocketListenerWrapper('on');
socket.addListener = generateSocketListenerWrapper('addListener');
socket.prependListener = generateSocketListenerWrapper('prependListener');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also add addListener and prependListener to the comment for clarify ?


// We only consume the socket if it has never been consumed before.
if (socket._handle && socket._handle.isStreamBase &&
Expand Down Expand Up @@ -754,19 +755,21 @@ function unconsume(parser, socket) {
}
}

function socketOnWrap(ev, fn) {
const res = net.Socket.prototype.on.call(this, ev, fn);
if (!this.parser) {
this.prependListener = net.Socket.prototype.prependListener;
this.on = net.Socket.prototype.on;
this.addListener = this.on;
return res;
}
function generateSocketListenerWrapper(originalFnName) {
return function socketListenerWrap(ev, fn) {
const res = net.Socket.prototype[originalFnName].call(this, ev, fn);
if (!this.parser) {
this.on = net.Socket.prototype.on;
this.addListener = net.Socket.prototype.addListener;
this.prependListener = net.Socket.prototype.prependListener;
return res;
}

if (ev === 'data' || ev === 'readable')
unconsume(this.parser, this);
if (ev === 'data' || ev === 'readable')
unconsume(this.parser, this);

return res;
return res;
};
}

function resetHeadersTimeoutOnReqEnd() {
Expand Down
41 changes: 20 additions & 21 deletions test/parallel/test-http-server-unconsume.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const http = require('http');
const net = require('net');

let received = '';
['on', 'addListener', 'prependListener'].forEach((testFn) => {
let received = '';

const server = http.createServer(function(req, res) {
res.writeHead(200);
res.end();
const server = http.createServer(function(req, res) {
res.writeHead(200);
res.end();

req.socket.on('data', function(data) {
received += data;
});
req.socket[testFn]('data', function(data) {
received += data;
});

assert.strictEqual(req.socket.on, req.socket.addListener);
assert.strictEqual(req.socket.prependListener,
net.Socket.prototype.prependListener);
server.close();
}).listen(0, function() {
const socket = net.connect(this.address().port, function() {
socket.write('PUT / HTTP/1.1\r\n\r\n');

server.close();
}).listen(0, function() {
const socket = net.connect(this.address().port, function() {
socket.write('PUT / HTTP/1.1\r\n\r\n');
socket.once('data', function() {
socket.end('hello world');
});

socket.once('data', function() {
socket.end('hello world');
socket.on('end', common.mustCall(() => {
assert.strictEqual(received, 'hello world',
`failed for socket.${testFn}`);
}));
});
});
});

process.on('exit', function() {
assert.strictEqual(received, 'hello world');
});