From 81d0697ae4acb66d88b0f1cdd1aa503dbb0be3a3 Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Sat, 3 Jan 2015 13:01:28 +0500 Subject: [PATCH] Discard response before throw This will not leave hanging sockets for 2 minutes (if server does not provide `Connection: close` header). Downside of it - you will not get content of 3xx response, but almost always it does not contain anything. --- index.js | 4 ++-- test/test-redirects.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 446af8e..5e336c5 100644 --- a/index.js +++ b/index.js @@ -61,13 +61,13 @@ function got(url, opts, cb) { // redirect if (statusCode >= 300 && statusCode < 400 && 'location' in res.headers) { + res.resume(); // Discard response + if (++redirectCount > 10) { cb(new Error('Redirected 10 times. Aborting.'), undefined, res); return; } - res.resume(); // Discard response - get(urlLib.resolve(url, res.headers.location), opts, cb); return; } diff --git a/test/test-redirects.js b/test/test-redirects.js index c6cdc11..7fe18a2 100644 --- a/test/test-redirects.js +++ b/test/test-redirects.js @@ -54,7 +54,7 @@ tape('follows relative redirect', function (t) { }); tape('throws on endless redirect', function (t) { - got(s.url + '/endless', {agent: false}, function (err) { + got(s.url + '/endless', function (err) { t.ok(err, 'should get error'); t.equal(err.message, 'Redirected 10 times. Aborting.'); t.end();