Skip to content
Open
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
23 changes: 23 additions & 0 deletions lib/_http_incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ const {

const { Readable, finished } = require('stream');

const { AbortController } = require('internal/abort_controller');

const kHeaders = Symbol('kHeaders');
const kHeadersDistinct = Symbol('kHeadersDistinct');
const kHeadersCount = Symbol('kHeadersCount');
const kTrailers = Symbol('kTrailers');
const kTrailersDistinct = Symbol('kTrailersDistinct');
const kTrailersCount = Symbol('kTrailersCount');
const kAbortController = Symbol('kAbortController');

function readStart(socket) {
if (socket && !socket._paused && socket.readable)
Expand Down Expand Up @@ -90,6 +93,7 @@ function IncomingMessage(socket) {
// Flag for when we decide that this message cannot possibly be
// read by the user, so there's no point continuing to handle it.
this._dumped = false;
this[kAbortController] = null;
}
ObjectSetPrototypeOf(IncomingMessage.prototype, Readable.prototype);
ObjectSetPrototypeOf(IncomingMessage, Readable);
Expand Down Expand Up @@ -184,6 +188,25 @@ ObjectDefineProperty(IncomingMessage.prototype, 'trailersDistinct', {
},
});

ObjectDefineProperty(IncomingMessage.prototype, 'signal', {
__proto__: null,
configurable: true,
get: function() {
if (this[kAbortController] === null) {
const ac = new AbortController();
this[kAbortController] = ac;
if (this.destroyed) {
ac.abort();
} else {
this.once('close', function() {
ac.abort();
});
}
}
return this[kAbortController].signal;
},
});

IncomingMessage.prototype.setTimeout = function setTimeout(msecs, callback) {
if (callback)
this.on('timeout', callback);
Expand Down
85 changes: 85 additions & 0 deletions test/parallel/test-http-request-signal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');

// Test 1: req.signal is an AbortSignal and aborts on 'close'
{
const server = http.createServer(common.mustCall((req, res) => {
assert.ok(req.signal instanceof AbortSignal);
assert.strictEqual(req.signal.aborted, false);
req.signal.onabort = common.mustCall(() => {
assert.strictEqual(req.signal.aborted, true);
});
res.destroy();
}));
server.listen(0, common.mustCall(() => {
http.get({ port: server.address().port }, () => {}).on('error', () => {
server.close();
});
}));
}

// Test 2: req.signal is aborted if accessed after destroy
{
const req = new http.IncomingMessage(null);
req.destroy();
assert.strictEqual(req.signal.aborted, true);
}

// Test 3: Multiple accesses return the same signal
{
const req = new http.IncomingMessage(null);
assert.strictEqual(req.signal, req.signal);
}


// Test 4: res.signal on a client-side http.request() response (IncomingMessage).
{
const server = http.createServer(common.mustCall((req, res) => {
res.writeHead(200);
res.write('partial');
}));

server.listen(0, common.mustCall(() => {
const clientReq = http.request(
{ port: server.address().port },
common.mustCall((res) => {
assert.ok(res.signal instanceof AbortSignal);
assert.strictEqual(res.signal.aborted, false);

res.signal.onabort = common.mustCall(() => {
assert.strictEqual(res.signal.aborted, true);
server.close();
});
clientReq.destroy();
}),
);
clientReq.on('error', () => {});
clientReq.end();
}));
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the most important case here isn't covered: server receives a request & doesn't end the response (stays pending) => client aborts the request while it's incomplete (so the connection closes) => server should see req.signal.aborted == true. That's the primary end goal, allowing us to easily detect on the server side when the client cancelled the requests.

The two full-flow tests here (1 and 5) only cover serverRes.destroy() => serverReq.signal.aborted and clientReq.destroy() => clientRes.signal.aborted.

Looks like it should work, but we need to test to make sure we actually cover that integration.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks,I have added the test in the latest commit


// Test 5: Client cancels a pending request.
{
const server = http.createServer(common.mustCall((req, res) => {
req.signal.onabort = common.mustCall(() => {
assert.strictEqual(req.signal.aborted, true);
server.close();
});
res.flushHeaders();
}));

server.listen(0, common.mustCall(() => {
const clientReq = http.request(
{ port: server.address().port },
common.mustCall((res) => {
res.on('error', () => {});
clientReq.destroy();
}),
);
clientReq.on('error', () => {});
clientReq.end();
}));
}
Loading