Skip to content

Commit

Permalink
fix: don't change args.headers (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Oct 20, 2017
1 parent b798546 commit ac9bc64
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
23 changes: 14 additions & 9 deletions lib/urllib.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var querystring = require('querystring');
var zlib = require('zlib');
var ua = require('default-user-agent');
var digestAuthHeader = require('digest-header');
var util = require('util');
var ms = require('humanize-ms');
var statuses = require('statuses');
var contentTypeParser = require('content-type');
Expand Down Expand Up @@ -247,13 +246,18 @@ exports.requestWithCallback = function requestWithCallback(url, args, callback)
method: method,
port: port,
agent: agent,
headers: args.headers || {},
headers: {},
// default is dns.lookup
// https://github.com/nodejs/node/blob/master/lib/net.js#L986
// custom dnslookup require node >= 4.0.0
// https://github.com/nodejs/node/blob/archived-io.js-v0.12/lib/net.js#L952
lookup: args.lookup,
};
if (args.headers) {
for (var k in args.headers) {
options.headers[k] = args.headers[k];
}
}

var sslNames = [
'pfx',
Expand Down Expand Up @@ -397,16 +401,16 @@ exports.requestWithCallback = function requestWithCallback(url, args, callback)

// handle digest auth
if (statusCode === 401 && headers['www-authenticate']
&& (!args.headers || !args.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);
args.headers = args.headers || {};
args.headers.Authorization = digestAuthHeader(options.method, options.path, authenticate, args.digestAuth);
debug('Request#%d %s: auth with digest header: %s', reqId, url, args.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']) {
args.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 @@ -487,8 +491,9 @@ exports.requestWithCallback = function requestWithCallback(url, args, callback)
// make sure timer stop
cancelResponseTimer();
// should clean up headers.Host on `location: http://other-domain/url`
if (args.headers && args.headers.Host && PROTO_RE.test(location)) {
args.headers.Host = null;
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.
var cb = callback;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"intelli-espower-loader": "^1.0.1",
"istanbul": "*",
"jshint": "*",
"mocha": "*",
"mocha": "3",
"muk": "^0.4.0",
"pedding": "^1.1.0",
"power-assert": "^1.4.2",
Expand All @@ -67,4 +67,4 @@
"node": ">= 0.10.0"
},
"license": "MIT"
}
}
4 changes: 4 additions & 0 deletions test/urllib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,17 @@ describe('test/urllib.test.js', function () {
});

it('should request https success', function (done) {
var headers = {};
urllib.request(config.npmRegistry + '/pedding/*', {
timeout: 25000,
headers: headers,
},
function (err, data, res) {
assert(!err);
assert(Buffer.isBuffer(data));
assert(res.statusCode === 200);
// don't touch headers
assert.deepEqual(headers, {});
done();
});
});
Expand Down

0 comments on commit ac9bc64

Please sign in to comment.