simple-get
Advanced tools
Comparing version 2.7.1 to 2.8.0
84
index.js
@@ -13,44 +13,60 @@ module.exports = simpleGet | ||
opts = typeof opts === 'string' ? {url: opts} : Object.assign({}, opts) | ||
opts.headers = Object.assign({}, opts.headers) | ||
cb = once(cb) | ||
if (opts.url) parseOptsUrl(opts) | ||
opts.headers = Object.assign({}, opts.headers) | ||
Object.keys(opts.headers).forEach(function (h) { | ||
if (h.toLowerCase() !== h) { | ||
opts.headers[h.toLowerCase()] = opts.headers[h] | ||
delete opts.headers[h] | ||
} | ||
}) | ||
if (opts.url) { | ||
var loc = url.parse(opts.url) | ||
if (loc.hostname) opts.hostname = loc.hostname | ||
if (loc.port) opts.port = loc.port | ||
if (loc.protocol) opts.protocol = loc.protocol | ||
if (loc.auth) opts.auth = loc.auth | ||
opts.path = loc.path | ||
delete opts.url | ||
} | ||
if (opts.maxRedirects == null) opts.maxRedirects = 10 | ||
if (opts.method) opts.method = opts.method.toUpperCase() | ||
var body | ||
if (opts.form) body = typeof opts.form === 'string' ? opts.form : querystring.stringify(opts.form) | ||
if (opts.body) body = opts.json && !isStream(opts.body) ? JSON.stringify(opts.body) : opts.body | ||
if (opts.body) { | ||
body = opts.json && !isStream(opts.body) ? JSON.stringify(opts.body) : opts.body | ||
} else if (opts.form) { | ||
body = typeof opts.form === 'string' ? opts.form : querystring.stringify(opts.form) | ||
opts.headers['content-type'] = 'application/x-www-form-urlencoded' | ||
} | ||
delete opts.body; delete opts.form | ||
if (body) { | ||
if (!opts.method) opts.method = 'POST' | ||
if (!isStream(body)) opts.headers['content-length'] = Buffer.byteLength(body) | ||
if (opts.json) opts.headers['content-type'] = 'application/json' | ||
} | ||
if (opts.json) opts.headers.accept = 'application/json' | ||
if (opts.json && body) opts.headers['content-type'] = 'application/json' | ||
if (opts.form) opts.headers['content-type'] = 'application/x-www-form-urlencoded' | ||
if (body && !isStream(body)) opts.headers['content-length'] = Buffer.byteLength(body) | ||
delete opts.body | ||
delete opts.form | ||
if (!opts.headers['accept-encoding']) opts.headers['accept-encoding'] = 'gzip, deflate' // Prefer gzip | ||
if (body && !opts.method) opts.method = 'POST' | ||
if (opts.method) opts.method = opts.method.toUpperCase() | ||
// Request gzip/deflate | ||
var customAcceptEncoding = Object.keys(opts.headers).some(function (h) { | ||
return h.toLowerCase() === 'accept-encoding' | ||
}) | ||
if (!customAcceptEncoding) opts.headers['accept-encoding'] = 'gzip, deflate' | ||
// Support http/https urls | ||
var protocol = opts.protocol === 'https:' ? https : http | ||
var protocol = opts.protocol === 'https:' ? https : http // Support http/https urls | ||
var req = protocol.request(opts, function (res) { | ||
// Follow 3xx redirects | ||
if (res.statusCode >= 300 && res.statusCode < 400 && 'location' in res.headers) { | ||
opts.url = res.headers.location | ||
delete opts.headers.host | ||
opts.url = res.headers.location // Follow 3xx redirects | ||
delete opts.headers.host // Discard `host` header on redirect (see #32) | ||
res.resume() // Discard response | ||
if (opts.maxRedirects > 0) { | ||
opts.maxRedirects -= 1 | ||
simpleGet(opts, cb) | ||
} else { | ||
cb(new Error('too many redirects')) | ||
if ((res.statusCode === 301 || res.statusCode === 302) && opts.method === 'POST') { | ||
opts.method = 'GET' // On 301/302 redirect, change POST to GET (see #35) | ||
delete opts.headers['content-length'] | ||
delete opts.headers['content-type'] | ||
} | ||
return | ||
if (opts.maxRedirects === 0) return cb(new Error('too many redirects')) | ||
opts.maxRedirects -= 1 | ||
return simpleGet(opts, cb) | ||
} | ||
@@ -98,12 +114,2 @@ | ||
function parseOptsUrl (opts) { | ||
var loc = url.parse(opts.url) | ||
if (loc.hostname) opts.hostname = loc.hostname | ||
if (loc.port) opts.port = loc.port | ||
if (loc.protocol) opts.protocol = loc.protocol | ||
if (loc.auth) opts.auth = loc.auth | ||
opts.path = loc.path | ||
delete opts.url | ||
} | ||
function isStream (obj) { return typeof obj.pipe === 'function' } |
{ | ||
"name": "simple-get", | ||
"description": "Simplest way to make http get requests. Supports HTTPS, redirects, gzip/deflate, streams in < 100 lines.", | ||
"version": "2.7.1", | ||
"version": "2.8.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Feross Aboukhadijeh", |
@@ -25,3 +25,3 @@ # simple-get [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url] | ||
All this in < 100 lines of code. | ||
All this in < 120 lines of code. | ||
@@ -28,0 +28,0 @@ ## install |
@@ -133,1 +133,41 @@ var concat = require('simple-concat') | ||
}) | ||
test('rewrite POST redirects to GET', function (t) { | ||
t.plan(8) | ||
var redirected = false | ||
var server = http.createServer(function (req, res) { | ||
if (redirected) { | ||
t.equal(req.url, '/getthis') | ||
t.equal(req.method, 'GET') | ||
t.notOk(req.headers['content-length']) | ||
res.statusCode = 200 | ||
req.pipe(res) | ||
} else { | ||
t.equal(req.method, 'POST') | ||
redirected = true | ||
res.statusCode = 301 | ||
res.setHeader('Location', '/getthis') | ||
res.end() | ||
} | ||
}) | ||
server.listen(0, function () { | ||
var port = server.address().port | ||
var opts = { | ||
method: 'POST', | ||
body: '123', | ||
url: 'http://localhost:' + port | ||
} | ||
get(opts, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), '') | ||
server.close() | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -194,3 +194,2 @@ var concat = require('simple-concat') | ||
port2 = server2.address().port | ||
console.log(port1, port2) | ||
get('http://localhost:' + port1 + '/path1', function (err, res) { | ||
@@ -239,3 +238,2 @@ t.error(err) | ||
port2 = server2.address().port | ||
console.log(port1, port2) | ||
get({ | ||
@@ -260,1 +258,49 @@ url: `http://localhost:${port1}/path1`, | ||
}) | ||
test('redirect should clear explicitly specified `Host` (note uppercase) header', function (t) { | ||
t.plan(8) | ||
var port1 = null | ||
var port2 = null | ||
var server1 = http.createServer(function (req, res) { | ||
t.equal(req.url, '/path1') | ||
t.equal(req.headers.host, `localhost:${port1}`) | ||
res.statusCode = 301 | ||
// Redirect from localhost:port1 to 127.0.0.1:port2 (different host and port!) | ||
res.setHeader('Location', 'http://127.0.0.1:' + port2 + '/path2') | ||
res.end() | ||
}) | ||
var server2 = http.createServer(function (req, res) { | ||
t.equal(req.url, '/path2') | ||
// Confirm that request was made with new host and port (127.0.0.1:port2), i.e. | ||
// that the explicitly specified `Host` header was cleared upon redirect. | ||
t.equal(req.headers.host, `127.0.0.1:${port2}`) | ||
res.statusCode = 200 | ||
res.end('response') | ||
}) | ||
server1.listen(0, function () { | ||
port1 = server1.address().port | ||
server2.listen(0, function () { | ||
port2 = server2.address().port | ||
get({ | ||
url: `http://localhost:${port1}/path1`, | ||
// Explicitly specify a `Host` header, so it won't be set automatically | ||
headers: { | ||
Host: `localhost:${port1}` | ||
} | ||
}, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
concat(res, function (err, data) { | ||
t.error(err) | ||
t.equal(data.toString(), 'response') | ||
server1.close() | ||
server2.close() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
35407
887
9