Browse Source

prevent duplicates of different casing in `opts.headers`

also make sure the user supplied object is never modified by us
http2
Sindre Sorhus 10 years ago
parent
commit
d7737c7490
  1. 28
      index.js

28
index.js

@ -3,7 +3,7 @@ var http = require('http');
var https = require('https');
var urlLib = require('url');
var zlib = require('zlib');
var assign = require('object-assign');
var objectAssign = require('object-assign');
var agent = require('infinity-agent');
var duplexify = require('duplexify');
var isStream = require('is-stream');
@ -11,6 +11,22 @@ var read = require('read-all-stream');
var timeout = require('timed-out');
var prependHttp = require('prepend-http');
// prevent duplicates of different casing
function assignHeaders(val) {
val = val || {};
var ret = {
'user-agent': 'https://github.com/sindresorhus/got',
'accept-encoding': 'gzip,deflate'
};
Object.keys(val).forEach(function (el) {
ret[el.toLowerCase()] = val[el];
});
return ret;
}
function got(url, opts, cb) {
if (typeof opts === 'function') {
cb = opts;
@ -19,6 +35,9 @@ function got(url, opts, cb) {
opts = {};
}
opts = objectAssign({}, opts);
opts.headers = assignHeaders(opts.headers);
var encoding = opts.encoding;
var body = opts.body;
var proxy;
@ -42,15 +61,10 @@ function got(url, opts, cb) {
};
}
opts.headers = assign({
'user-agent': 'https://github.com/sindresorhus/got',
'accept-encoding': 'gzip,deflate'
}, opts.headers || {});
function get(url, opts, cb) {
var parsedUrl = urlLib.parse(prependHttp(url));
var fn = parsedUrl.protocol === 'https:' ? https : http;
var arg = assign({}, parsedUrl, opts);
var arg = objectAssign({}, parsedUrl, opts);
// TODO: remove this when Node 0.10 will be deprecated
if (arg.agent === undefined) {

Loading…
Cancel
Save