Skip to content

Commit 1edd7f6

Browse files
ronagBethGriggs
authored andcommitted
http: added aborted property to request
Backport-PR-URL: #22850 PR-URL: #20094 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 98ed30f commit 1edd7f6

8 files changed

+89
-2
lines changed

doc/api/http.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,7 @@ following additional events, methods, and properties.
14011401
added: v0.3.8
14021402
-->
14031403

1404-
Emitted when the request has been aborted and the network socket has closed.
1404+
Emitted when the request has been aborted.
14051405

14061406
### Event: 'close'
14071407
<!-- YAML
@@ -1411,6 +1411,16 @@ added: v0.4.2
14111411
Indicates that the underlying connection was closed.
14121412
Just like `'end'`, this event occurs only once per response.
14131413

1414+
### message.aborted
1415+
<!-- YAML
1416+
added: REPLACEME
1417+
-->
1418+
1419+
* {boolean}
1420+
1421+
The `message.aborted` property will be `true` if the request has
1422+
been aborted.
1423+
14141424
### message.destroy([error])
14151425
<!-- YAML
14161426
added: v0.3.0

doc/api/http2.md

+10
Original file line numberDiff line numberDiff line change
@@ -2422,6 +2422,16 @@ added: v8.4.0
24222422
Indicates that the underlying [`Http2Stream`][] was closed.
24232423
Just like `'end'`, this event occurs only once per response.
24242424

2425+
#### request.aborted
2426+
<!-- YAML
2427+
added: REPLACEME
2428+
-->
2429+
2430+
* {boolean}
2431+
2432+
The `request.aborted` property will be `true` if the request has
2433+
been aborted.
2434+
24252435
#### request.destroy([error])
24262436
<!-- YAML
24272437
added: v8.4.0

lib/_http_client.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ ClientRequest.prototype.abort = function abort() {
299299
if (!this.aborted) {
300300
process.nextTick(emitAbortNT.bind(this));
301301
}
302+
302303
// Mark as aborting so we can avoid sending queued request data
303304
// This is used as a truthy flag elsewhere. The use of Date.now is for
304305
// debugging purposes only.
@@ -350,7 +351,10 @@ function socketCloseListener() {
350351
req.emit('close');
351352
if (req.res && req.res.readable) {
352353
// Socket closed before we emitted 'end' below.
353-
if (!req.res.complete) req.res.emit('aborted');
354+
if (!req.res.complete) {
355+
req.res.aborted = true;
356+
req.res.emit('aborted');
357+
}
354358
var res = req.res;
355359
res.on('end', function() {
356360
res.emit('close');

lib/_http_incoming.js

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ function IncomingMessage(socket) {
5959

6060
this.readable = true;
6161

62+
this.aborted = false;
63+
6264
this.upgrade = null;
6365

6466
// request (server) only

lib/_http_server.js

+1
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ function socketOnClose(socket, state) {
436436
function abortIncoming(incoming) {
437437
while (incoming.length) {
438438
var req = incoming.shift();
439+
req.aborted = true;
439440
req.emit('aborted');
440441
req.emit('close');
441442
}

lib/internal/http2/compat.js

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const kTrailers = Symbol('trailers');
1919
const kRawTrailers = Symbol('rawTrailers');
2020
const kProxySocket = Symbol('proxySocket');
2121
const kSetHeader = Symbol('setHeader');
22+
const kAborted = Symbol('aborted');
2223

2324
const {
2425
HTTP2_HEADER_AUTHORITY,
@@ -125,6 +126,7 @@ function onStreamDrain() {
125126
function onStreamAbortedRequest() {
126127
const request = this[kRequest];
127128
if (request !== undefined && request[kState].closed === false) {
129+
request[kAborted] = true;
128130
request.emit('aborted');
129131
}
130132
}
@@ -221,6 +223,7 @@ class Http2ServerRequest extends Readable {
221223
this[kTrailers] = {};
222224
this[kRawTrailers] = [];
223225
this[kStream] = stream;
226+
this[kAborted] = false;
224227
stream[kProxySocket] = null;
225228
stream[kRequest] = this;
226229

@@ -236,6 +239,10 @@ class Http2ServerRequest extends Readable {
236239
this.on('resume', onRequestResume);
237240
}
238241

242+
get aborted() {
243+
return this[kAborted];
244+
}
245+
239246
get complete() {
240247
return this._readableState.ended ||
241248
this[kState].closed ||

test/parallel/test-http-aborted.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const http = require('http');
5+
const assert = require('assert');
6+
7+
const server = http.createServer(common.mustCall(function(req, res) {
8+
req.on('aborted', common.mustCall(function() {
9+
assert.strictEqual(this.aborted, true);
10+
server.close();
11+
}));
12+
assert.strictEqual(req.aborted, false);
13+
res.write('hello');
14+
}));
15+
16+
server.listen(0, common.mustCall(() => {
17+
const req = http.get({
18+
port: server.address().port,
19+
headers: { connection: 'keep-alive' }
20+
}, common.mustCall((res) => {
21+
res.on('aborted', common.mustCall(() => {
22+
assert.strictEqual(res.aborted, true);
23+
}));
24+
req.abort();
25+
}));
26+
}));
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
const h2 = require('http2');
7+
const assert = require('assert');
8+
9+
10+
const server = h2.createServer(common.mustCall(function(req, res) {
11+
req.on('aborted', common.mustCall(function() {
12+
assert.strictEqual(this.aborted, true);
13+
}));
14+
assert.strictEqual(req.aborted, false);
15+
res.write('hello');
16+
server.close();
17+
}));
18+
19+
server.listen(0, common.mustCall(function() {
20+
const url = `http://localhost:${server.address().port}`;
21+
const client = h2.connect(url, common.mustCall(() => {
22+
const request = client.request();
23+
request.on('data', common.mustCall((chunk) => {
24+
client.destroy();
25+
}));
26+
}));
27+
}));

0 commit comments

Comments
 (0)