Skip to content

Commit

Permalink
lowercase all manually specified headers
Browse files Browse the repository at this point in the history
- clearer code when detecting if a custom 'accept-encoding' header is being used.

- allows any casing of `Host`/`host`/`HoSt` header to be removed upon redirect, making the fix to #32 more robust
  • Loading branch information
feross committed Apr 24, 2018
1 parent 44a7e43 commit 922b986
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function simpleGet (opts, cb) {
cb = once(cb)

opts.headers = Object.assign({}, opts.headers)
parseHeaders(opts.headers)

if (opts.url) parseOptsUrl(opts)
if (opts.maxRedirects == null) opts.maxRedirects = 10
Expand All @@ -33,12 +34,8 @@ function simpleGet (opts, cb) {
if (opts.json) opts.headers['content-type'] = 'application/json'
}

// 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'
if (opts.json) opts.headers.accept = 'application/json'
if (!opts.headers['accept-encoding']) opts.headers['accept-encoding'] = 'gzip, deflate' // Prefer gzip

var protocol = opts.protocol === 'https:' ? https : http // Support http/https urls
var req = protocol.request(opts, function (res) {
Expand Down Expand Up @@ -98,6 +95,15 @@ simpleGet.concat = function (opts, cb) {
}
})

function parseHeaders (headers) {
Object.keys(headers).forEach(function (h) {
if (h.toLowerCase() !== h) {
headers[h.toLowerCase()] = headers[h]
delete headers[h]
}
})
}

function parseOptsUrl (opts) {
var loc = url.parse(opts.url)
if (loc.hostname) opts.hostname = loc.hostname
Expand Down

0 comments on commit 922b986

Please sign in to comment.