Skip to content

Commit

Permalink
feat: change header to lowercase (#337)
Browse files Browse the repository at this point in the history
set all headers key to lowercase
  • Loading branch information
atian25 committed May 15, 2020
1 parent 175ad2b commit 47c21bd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
41 changes: 20 additions & 21 deletions lib/urllib.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ function requestWithCallback(url, args, callback) {
var names = utility.getOwnEnumerables(args.headers, true);
for (var i = 0; i < names.length; i++) {
var name = names[i];
options.headers[name] = args.headers[name];
options.headers[name.toLowerCase()] = args.headers[name];
}
}

Expand Down Expand Up @@ -387,7 +387,7 @@ function requestWithCallback(url, args, callback) {
var formHeaderNames = utility.getOwnEnumerables(formHeaders, true);
for (var i = 0; i < formHeaderNames.length; i++) {
var name = formHeaderNames[i];
options.headers[name] = formHeaders[name];
options.headers[name.toLowerCase()] = formHeaders[name];
}
debug('set multipart headers: %j, method: %s', formHeaders, options.method);
args.stream = form;
Expand All @@ -400,15 +400,15 @@ function requestWithCallback(url, args, callback) {
// read: GET, HEAD, use query string
body = args.nestedQuerystring ? qs.stringify(body) : querystring.stringify(body);
} else {
var contentType = options.headers['Content-Type'] || options.headers['content-type'];
var contentType = options.headers['content-type'];
// auto add application/x-www-form-urlencoded when using urlencode form request
if (!contentType) {
if (args.contentType === 'json') {
contentType = 'application/json';
} else {
contentType = 'application/x-www-form-urlencoded';
}
options.headers['Content-Type'] = contentType;
options.headers['content-type'] = contentType;
}

if (parseContentType(contentType).type === 'application/json') {
Expand All @@ -434,13 +434,15 @@ function requestWithCallback(url, args, callback) {
if (!Buffer.isBuffer(body)) {
length = Buffer.byteLength(body);
}
requestSize = options.headers['Content-Length'] = length;
requestSize = length;

options.headers['content-length'] = length.toString();
}
}

if (args.dataType === 'json') {
if (!options.headers.Accept && !options.headers.accept) {
options.headers.Accept = 'application/json';
if (!options.headers.accept) {
options.headers.accept = 'application/json';
}
}

Expand Down Expand Up @@ -517,14 +519,14 @@ function requestWithCallback(url, args, callback) {

// handle digest auth
if (statusCode === 401 && headers['www-authenticate']
&& !options.headers.Authorization && args.digestAuth) {
&& !options.headers.authorization && args.digestAuth) {
var authenticate = headers['www-authenticate'];
if (authenticate.indexOf('Digest ') >= 0) {
debug('Request#%d %s: got digest auth header WWW-Authenticate: %s', reqId, url, authenticate);
options.headers.Authorization = digestAuthHeader(options.method, options.path, authenticate, args.digestAuth);
debug('Request#%d %s: auth with digest header: %s', reqId, url, options.headers.Authorization);
options.headers.authorization = digestAuthHeader(options.method, options.path, authenticate, args.digestAuth);
debug('Request#%d %s: auth with digest header: %s', reqId, url, options.headers.authorization);
if (res.headers['set-cookie']) {
options.headers.Cookie = res.headers['set-cookie'].join(';');
options.headers.cookie = res.headers['set-cookie'].join(';');
}
args.headers = options.headers;
return exports.requestWithCallback(url, args, cb);
Expand Down Expand Up @@ -639,9 +641,9 @@ function requestWithCallback(url, args, callback) {
debug('Request#%d %s: `redirected` from %s to %s', reqId, options.path, url, newUrl);
// make sure timer stop
cancelResponseTimer();
// should clean up headers.Host on `location: http://other-domain/url`
if (options.headers.Host && PROTO_RE.test(location)) {
options.headers.Host = null;
// should clean up headers.host on `location: http://other-domain/url`
if (options.headers.host && PROTO_RE.test(location)) {
options.headers.host = null;
args.headers = options.headers;
}
// avoid done will be execute in the future change.
Expand All @@ -665,23 +667,20 @@ function requestWithCallback(url, args, callback) {
if (options.headers['user-agent']) {
delete options.headers['user-agent'];
}
if (options.headers['User-Agent']) {
delete options.headers['User-Agent'];
}
} else {
// need to set user-agent
var hasAgentHeader = options.headers['User-Agent'] || options.headers['user-agent'];
var hasAgentHeader = options.headers['user-agent'];
if (!hasAgentHeader) {
options.headers['User-Agent'] = USER_AGENT;
options.headers['user-agent'] = USER_AGENT;
}
}

if (args.gzip) {
var isAcceptEncodingNull = (args.headers && (args.headers['Accept-Encoding'] === null || args.headers['accept-encoding'] === null));
if (!isAcceptEncodingNull) {
var hasAcceptEncodingHeader = options.headers['Accept-Encoding'] || options.headers['accept-encoding'];
var hasAcceptEncodingHeader = options.headers['accept-encoding'];
if (!hasAcceptEncodingHeader) {
options.headers['Accept-Encoding'] = 'gzip, deflate';
options.headers['accept-encoding'] = 'gzip, deflate';
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions test/urllib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,27 @@ describe('test/urllib.test.js', function () {
});
});

it('should only set one content-length', function (done) {
urllib.request(host + '/headers', {
method: 'post',
dataType: 'json',
headers: {
'content-length': '10',
},
data: {
foo: 'bar11111111111111',
},
beforeRequest(options) {
assert(options.headers['content-length'] === '21');
assert(!options.headers['Content-Length']);
}
}, function (err, data, res) {
assert(!err);
assert(res.statusCode === 200);
done();
});
});

describe('mock sockets full', function () {
var agent = new http.Agent({
maxSockets: 1,
Expand Down

0 comments on commit 47c21bd

Please sign in to comment.