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 request fixes #20075

Closed
wants to merge 1 commit 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
39 changes: 23 additions & 16 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,26 +328,33 @@ function socketCloseListener() {

// NOTE: It's important to get parser here, because it could be freed by
// the `socketOnData`.
var parser = socket.parser;
if (req.res && req.res.readable) {
const parser = socket.parser;
const res = req.res;
if (res) {
// Socket closed before we emitted 'end' below.
if (!req.res.complete) {
req.res.aborted = true;
req.res.emit('aborted');
if (!res.complete) {
res.aborted = true;
res.emit('aborted');
}
var res = req.res;
res.on('end', function() {
req.emit('close');
if (res.readable) {
res.on('end', function() {
this.emit('close');
});
res.push(null);
} else {
res.emit('close');
});
res.push(null);
} else if (!req.res && !req.socket._hadError) {
// This socket error fired before we started to
// receive a response. The error needs to
// fire on the request.
req.socket._hadError = true;
req.emit('error', createHangUpError());
}
} else {
if (!req.socket._hadError) {
// This socket error fired before we started to
// receive a response. The error needs to
// fire on the request.
req.socket._hadError = true;
req.emit('error', createHangUpError());
}
req.emit('close');
}
req.emit('close');

// Too bad. That output wasn't getting written.
// This is pretty terrible that it doesn't raise an error.
Expand Down
71 changes: 46 additions & 25 deletions test/parallel/test-http-response-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,50 @@
const common = require('../common');
const http = require('http');

const server = http.createServer(common.mustCall(function(req, res) {
res.writeHead(200);
res.write('a');
{
const server = http.createServer(
common.mustCall((req, res) => {
res.writeHead(200);
res.write('a');
})
);
server.listen(
0,
common.mustCall(() => {
http.get(
{ port: server.address().port },
common.mustCall((res) => {
res.on('data', common.mustCall(() => {
res.destroy();
}));
res.on('close', common.mustCall(() => {
server.close();
}));
})
);
})
);
}

req.on('close', common.mustCall(function() {
console.error('request aborted');
}));
res.on('close', common.mustCall(function() {
console.error('response aborted');
}));
}));
server.listen(0);

server.on('listening', function() {
console.error('make req');
http.get({
port: this.address().port
}, function(res) {
console.error('got res');
res.on('data', function(data) {
console.error('destroy res');
res.destroy();
server.close();
});
});
});
{
const server = http.createServer(
common.mustCall((req, res) => {
res.writeHead(200);
res.end('a');
})
);
server.listen(
0,
common.mustCall(() => {
http.get(
{ port: server.address().port },
common.mustCall((res) => {
res.on('close', common.mustCall(() => {
server.close();
}));
res.resume();
})
);
})
);
}