Skip to content

Commit 1385ffc

Browse files
ronagMylesBorins
authored andcommitted
http: added aborted property to request
PR-URL: #20094 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 375994f commit 1385ffc

8 files changed

+89
-2
lines changed

doc/api/http.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ following additional events, methods, and properties.
14831483
added: v0.3.8
14841484
-->
14851485

1486-
Emitted when the request has been aborted and the network socket has closed.
1486+
Emitted when the request has been aborted.
14871487

14881488
### Event: 'close'
14891489
<!-- YAML
@@ -1493,6 +1493,16 @@ added: v0.4.2
14931493
Indicates that the underlying connection was closed.
14941494
Just like `'end'`, this event occurs only once per response.
14951495

1496+
### message.aborted
1497+
<!-- YAML
1498+
added: REPLACEME
1499+
-->
1500+
1501+
* {boolean}
1502+
1503+
The `message.aborted` property will be `true` if the request has
1504+
been aborted.
1505+
14961506
### message.destroy([error])
14971507
<!-- YAML
14981508
added: v0.3.0

doc/api/http2.md

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

2420+
#### request.aborted
2421+
<!-- YAML
2422+
added: REPLACEME
2423+
-->
2424+
2425+
* {boolean}
2426+
2427+
The `request.aborted` property will be `true` if the request has
2428+
been aborted.
2429+
24202430
#### request.destroy([error])
24212431
<!-- YAML
24222432
added: v8.4.0

lib/_http_client.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ ClientRequest.prototype.abort = function abort() {
279279
if (!this.aborted) {
280280
process.nextTick(emitAbortNT.bind(this));
281281
}
282+
282283
// Mark as aborting so we can avoid sending queued request data
283284
// This is used as a truthy flag elsewhere. The use of Date.now is for
284285
// debugging purposes only.
@@ -330,7 +331,10 @@ function socketCloseListener() {
330331
var parser = socket.parser;
331332
if (req.res && req.res.readable) {
332333
// Socket closed before we emitted 'end' below.
333-
if (!req.res.complete) req.res.emit('aborted');
334+
if (!req.res.complete) {
335+
req.res.aborted = true;
336+
req.res.emit('aborted');
337+
}
334338
var res = req.res;
335339
res.on('end', function() {
336340
res.emit('close');

lib/_http_incoming.js

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

5555
this.readable = true;
5656

57+
this.aborted = false;
58+
5759
this.upgrade = null;
5860

5961
// request (server) only

lib/_http_server.js

+1
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ function socketOnClose(socket, state) {
440440
function abortIncoming(incoming) {
441441
while (incoming.length) {
442442
var req = incoming.shift();
443+
req.aborted = true;
443444
req.emit('aborted');
444445
req.emit('close');
445446
}

lib/internal/http2/compat.js

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const kTrailers = Symbol('trailers');
3131
const kRawTrailers = Symbol('rawTrailers');
3232
const kProxySocket = Symbol('proxySocket');
3333
const kSetHeader = Symbol('setHeader');
34+
const kAborted = Symbol('aborted');
3435

3536
const {
3637
HTTP2_HEADER_AUTHORITY,
@@ -137,6 +138,7 @@ function onStreamDrain() {
137138
function onStreamAbortedRequest() {
138139
const request = this[kRequest];
139140
if (request !== undefined && request[kState].closed === false) {
141+
request[kAborted] = true;
140142
request.emit('aborted');
141143
}
142144
}
@@ -233,6 +235,7 @@ class Http2ServerRequest extends Readable {
233235
this[kTrailers] = {};
234236
this[kRawTrailers] = [];
235237
this[kStream] = stream;
238+
this[kAborted] = false;
236239
stream[kProxySocket] = null;
237240
stream[kRequest] = this;
238241

@@ -248,6 +251,10 @@ class Http2ServerRequest extends Readable {
248251
this.on('resume', onRequestResume);
249252
}
250253

254+
get aborted() {
255+
return this[kAborted];
256+
}
257+
251258
get complete() {
252259
return this._readableState.ended ||
253260
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)