From 9755a47ff2cc5c2f7de20e5208337bafe5237c31 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Tue, 9 Jun 2020 17:08:53 +0200 Subject: [PATCH] http: make response.setTimeout() work Fixes: https://github.com/nodejs/node/issues/33734 --- lib/_http_client.js | 20 ++++++++++++++----- .../test-http-client-response-timeout.js | 14 +++++++++++++ ...st-http-client-timeout-option-listeners.js | 4 ++-- 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 test/parallel/test-http-client-response-timeout.js diff --git a/lib/_http_client.js b/lib/_http_client.js index 9e2ebca8ee5ca3..dfd3b25ecde17d 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -533,8 +533,8 @@ function socketOnData(d) { socket.removeListener('end', socketOnEnd); socket.removeListener('drain', ondrain); - if (req.timeoutCb) - socket.removeListener('timeout', req.timeoutCb); + if (req.timeoutCb) socket.removeListener('timeout', req.timeoutCb); + socket.removeListener('timeout', responseOnTimeout); parser.finish(); freeParser(parser, req, socket); @@ -644,6 +644,7 @@ function parserOnIncomingClient(res, shouldKeepAlive) { // Add our listener first, so that we guarantee socket cleanup res.on('end', responseOnEnd); req.on('prefinish', requestOnPrefinish); + socket.on('timeout', responseOnTimeout); // If the user did not listen for the 'response' event, then they // can't possibly read the data, so we ._dump() it into the void @@ -692,15 +693,16 @@ function responseKeepAlive(req) { function responseOnEnd() { const req = this.req; + const socket = req.socket; - if (req.socket && req.timeoutCb) { - req.socket.removeListener('timeout', emitRequestTimeout); + if (socket) { + if (req.timeoutCb) socket.removeListener('timeout', emitRequestTimeout); + socket.removeListener('timeout', responseOnTimeout); } req._ended = true; if (!req.shouldKeepAlive) { - const socket = req.socket; if (socket.writable) { debug('AGENT socket.destroySoon()'); if (typeof socket.destroySoon === 'function') @@ -719,6 +721,14 @@ function responseOnEnd() { } } +function responseOnTimeout() { + const req = this._httpMessage; + if (!req) return; + const res = req.res; + if (!res) return; + res.emit('timeout'); +} + function requestOnPrefinish() { const req = this; diff --git a/test/parallel/test-http-client-response-timeout.js b/test/parallel/test-http-client-response-timeout.js new file mode 100644 index 00000000000000..7e44d83a831143 --- /dev/null +++ b/test/parallel/test-http-client-response-timeout.js @@ -0,0 +1,14 @@ +'use strict'; +const common = require('../common'); +const http = require('http'); + +const server = http.createServer((req, res) => res.flushHeaders()); + +server.listen(common.mustCall(() => { + const req = + http.get({ port: server.address().port }, common.mustCall((res) => { + res.on('timeout', common.mustCall(() => req.destroy())); + res.setTimeout(1); + server.close(); + })); +})); diff --git a/test/parallel/test-http-client-timeout-option-listeners.js b/test/parallel/test-http-client-timeout-option-listeners.js index dac89b5fd1a2bb..1122eaf79e46f8 100644 --- a/test/parallel/test-http-client-timeout-option-listeners.js +++ b/test/parallel/test-http-client-timeout-option-listeners.js @@ -24,9 +24,9 @@ const options = { server.listen(0, options.host, common.mustCall(() => { options.port = server.address().port; doRequest(common.mustCall((numListeners) => { - assert.strictEqual(numListeners, 2); + assert.strictEqual(numListeners, 3); doRequest(common.mustCall((numListeners) => { - assert.strictEqual(numListeners, 2); + assert.strictEqual(numListeners, 3); server.close(); agent.destroy(); }));