Skip to content

Commit 1385ffc

Browse files
ronagMylesBorins
authored andcommitted
http: added aborted property to request
PR-URL: #20094 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 375994f commit 1385ffc

8 files changed

Lines changed: 89 additions & 2 deletions

File tree

‎doc/api/http.md‎

Lines changed: 11 additions & 1 deletion
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‎

Lines changed: 10 additions & 0 deletions
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‎

Lines changed: 5 additions & 1 deletion
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
varparser=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
varres=req.res;
335339
res.on('end',function(){
336340
res.emit('close');

‎lib/_http_incoming.js‎

Lines changed: 2 additions & 0 deletions
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‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ function socketOnClose(socket, state) {
440440
functionabortIncoming(incoming){
441441
while(incoming.length){
442442
varreq=incoming.shift();
443+
req.aborted=true;
443444
req.emit('aborted');
444445
req.emit('close');
445446
}

‎lib/internal/http2/compat.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const kTrailers = Symbol('trailers');
3131
constkRawTrailers=Symbol('rawTrailers');
3232
constkProxySocket=Symbol('proxySocket');
3333
constkSetHeader=Symbol('setHeader');
34+
constkAborted=Symbol('aborted');
3435

3536
const{
3637
HTTP2_HEADER_AUTHORITY,
@@ -137,6 +138,7 @@ function onStreamDrain() {
137138
functiononStreamAbortedRequest(){
138139
constrequest=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+
getaborted(){
255+
returnthis[kAborted];
256+
}
257+
251258
getcomplete(){
252259
returnthis._readableState.ended||
253260
this[kState].closed||

‎test/parallel/test-http-aborted.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
consthttp=require('http');
5+
constassert=require('assert');
6+
7+
constserver=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+
constreq=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+
}));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
if(!common.hasCrypto)
5+
common.skip('missing crypto');
6+
consth2=require('http2');
7+
constassert=require('assert');
8+
9+
10+
constserver=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+
consturl=`http://localhost:${server.address().port}`;
21+
constclient=h2.connect(url,common.mustCall(()=>{
22+
constrequest=client.request();
23+
request.on('data',common.mustCall((chunk)=>{
24+
client.destroy();
25+
}));
26+
}));
27+
}));

0 commit comments

Comments
 (0)