Skip to content
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
18 changes: 18 additions & 0 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,15 @@ changes:
The `request.aborted` property will be `true` if the request has
been aborted.

### request.closed
<!-- YAML
added: CHANGEME
-->

* {boolean}

The `request.closed` property indicates whether the request has been closed.

### request.connection
<!-- YAML
added: v0.3.0
Expand Down Expand Up @@ -1130,6 +1139,15 @@ response.end();
Attempting to set a header field name or value that contains invalid characters
will result in a [`TypeError`][] being thrown.

### response.closed
<!-- YAML
added: CHANGEME
-->

* {boolean}

The `response.closed` property indicates whether the response has been closed.

### response.connection
<!-- YAML
added: v0.3.0
Expand Down
16 changes: 16 additions & 0 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ function ClientRequest(input, options, cb) {
this._ended = false;
this.res = null;
this.aborted = false;
this.closed = false;
this.timeoutCb = null;
this.upgradeOrConnect = false;
this.parser = null;
Expand Down Expand Up @@ -297,6 +298,16 @@ function ClientRequest(input, options, cb) {
Object.setPrototypeOf(ClientRequest.prototype, OutgoingMessage.prototype);
Object.setPrototypeOf(ClientRequest, OutgoingMessage);

Object.defineProperty(ClientRequest.prototype, 'complete', {
get() {
return Boolean(
this.finished ||
this.aborted ||
this.closed
);
}
});

ClientRequest.prototype._finish = function _finish() {
DTRACE_HTTP_CLIENT_REQUEST(this, this.connection);
OutgoingMessage.prototype._finish.call(this);
Expand Down Expand Up @@ -358,13 +369,16 @@ function socketCloseListener() {
res.aborted = true;
res.emit('aborted');
}
req.closed = true;
req.emit('close');
if (res.readable) {
res.on('end', function() {
this.closed = true;
this.emit('close');
});
res.push(null);
} else {
res.closed = true;
res.emit('close');
}
} else {
Expand All @@ -375,6 +389,7 @@ function socketCloseListener() {
req.socket._hadError = true;
req.emit('error', connResetException('socket hang up'));
}
req.closed = true;
req.emit('close');
}

Expand Down Expand Up @@ -487,6 +502,7 @@ function socketOnData(d) {
socket.readableFlowing = null;

req.emit(eventName, res, socket, bodyHead);
req.closed = true;
req.emit('close');
} else {
// Requested Upgrade or used CONNECT method, but have no handler.
Expand Down
11 changes: 11 additions & 0 deletions lib/_http_incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function IncomingMessage(socket) {
this.readable = true;

this.aborted = false;
this.closed = false;

this.upgrade = null;

Expand All @@ -76,6 +77,16 @@ function IncomingMessage(socket) {
Object.setPrototypeOf(IncomingMessage.prototype, Stream.Readable.prototype);
Object.setPrototypeOf(IncomingMessage, Stream.Readable);

Object.defineProperty(IncomingMessage.prototype, 'complete', {
get() {
return Boolean(
this.finished ||
this.aborted ||
this.closed
);
}
});

IncomingMessage.prototype.setTimeout = function setTimeout(msecs, callback) {
if (callback)
this.on('timeout', callback);
Expand Down
9 changes: 9 additions & 0 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function OutgoingMessage() {
this._trailer = '';

this.finished = false;
this.closed = false;
this._headerSent = false;
this[kIsCorked] = false;

Expand All @@ -109,6 +110,14 @@ function OutgoingMessage() {
Object.setPrototypeOf(OutgoingMessage.prototype, Stream.prototype);
Object.setPrototypeOf(OutgoingMessage, Stream);

Object.defineProperty(OutgoingMessage.prototype, 'complete', {
get() {
return Boolean(
this.finished ||
this.closed
);
}
});

Object.defineProperty(OutgoingMessage.prototype, '_headers', {
get: internalUtil.deprecate(function() {
Expand Down
8 changes: 7 additions & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ function onServerResponseClose() {
// Ergo, we need to deal with stale 'close' events and handle the case
// where the ServerResponse object has already been deconstructed.
// Fortunately, that requires only a single if check. :-)
if (this._httpMessage) this._httpMessage.emit('close');
if (this._httpMessage) {
this._httpMessage.closed = true;
this._httpMessage.emit('close');
}
}

ServerResponse.prototype.assignSocket = function assignSocket(socket) {
Expand Down Expand Up @@ -468,6 +471,7 @@ function abortIncoming(incoming) {
while (incoming.length) {
var req = incoming.shift();
req.aborted = true;
req.closed = true;
req.emit('aborted');
req.emit('close');
}
Expand Down Expand Up @@ -609,6 +613,7 @@ function resOnFinish(req, res, socket, state, server) {
req._dump();

res.detachSocket(socket);
req.closed = true;
req.emit('close');
process.nextTick(emitCloseNT, res);

Expand All @@ -633,6 +638,7 @@ function resOnFinish(req, res, socket, state, server) {
}

function emitCloseNT(self) {
self.closed = true;
self.emit('close');
}

Expand Down
17 changes: 16 additions & 1 deletion lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,17 @@ class Http2ServerRequest extends Readable {
this.on('resume', onRequestResume);
}

get closed() {
return this[kState].closed;
}

get aborted() {
return this[kAborted];
}

get complete() {
return this._readableState.ended ||
return this[kAborted] ||
this._readableState.ended ||
this[kState].closed ||
this[kStream].destroyed;
}
Expand Down Expand Up @@ -446,6 +451,16 @@ class Http2ServerResponse extends Stream {
stream.on('timeout', onStreamTimeout(kResponse));
}

get closed() {
return this[kState].closed;
}

get complete() {
return this._writableState.ended ||
this[kState].closed ||
this[kStream].destroyed;
}

// User land modules such as finalhandler just check truthiness of this
// but if someone is actually trying to use this for more than that
// then we simply can't support such use cases
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-http-client-close-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ server.listen(0, common.mustCall(() => {

req.on('close', common.mustCall(() => {
assert.strictEqual(errorEmitted, true);
assert.strictEqual(req.closed, true);
server.close();
}));

Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-http-connect-req-res.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ server.listen(0, common.mustCall(function() {
path: 'example.com:443'
}, common.mustNotCall());

req.on('close', common.mustCall());
req.on('close', common.mustCall(() => {
assert.strictEqual(req.closed, true);
}));

req.on('connect', common.mustCall(function(res, socket, firstBodyChunk) {
console.error('Client got CONNECT request');
Expand Down
9 changes: 3 additions & 6 deletions test/parallel/test-http-req-res-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ const http = require('http');
const assert = require('assert');

const server = http.Server(common.mustCall((req, res) => {
let resClosed = false;

res.end();
res.on('finish', common.mustCall(() => {
assert.strictEqual(resClosed, false);
}));
res.on('finish', common.mustCall());
res.on('close', common.mustCall(() => {
resClosed = true;
assert.strictEqual(res.closed, true);
}));
req.on('close', common.mustCall(() => {
assert.strictEqual(req.closed, true);
assert.strictEqual(req._readableState.ended, true);
}));
res.socket.on('close', () => server.close());
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-http-response-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'use strict';
const common = require('../common');
const http = require('http');
const assert = require('assert');

{
const server = http.createServer(
Expand All @@ -40,6 +41,7 @@ const http = require('http');
res.destroy();
}));
res.on('close', common.mustCall(() => {
assert.strictEqual(res.closed, true);
server.close();
}));
})
Expand Down