v1.3.4: bugfix keep separate headers and don't keep Content-Length on redirect

This commit is contained in:
AJ ONeal 2018-07-07 19:46:21 -06:00
parent 50704fdccc
commit 3defd84af4
2 changed files with 13 additions and 5 deletions

View File

@ -29,13 +29,17 @@ function mergeOrDelete(defaults, updates) {
return updates; return updates;
} }
function hasHeader(reqOpts, header) { function getHeaderName(reqOpts, header) {
var headers = {}; var headers = {};
Object.keys(reqOpts.headers).forEach(function (key) { Object.keys(reqOpts.headers).forEach(function (key) {
headers[key.toLowerCase()] = true; headers[key.toLowerCase()] = key;
}); });
// returns the key, which in erroneous cases could be an empty string
return headers[header.toLowerCase()]; return headers[header.toLowerCase()];
} }
function hasHeader(reqOpts, header) {
return 'undefined' !== typeof getHeaderName(reqOpts, header);
}
function toJSONifier(keys) { function toJSONifier(keys) {
@ -98,6 +102,8 @@ function setDefaults(defs) {
if (!opts.followOriginalHttpMethod) { if (!opts.followOriginalHttpMethod) {
opts.method = 'GET'; opts.method = 'GET';
opts.body = null; opts.body = null;
delete opts.headers[getHeaderName(opts.headers, 'Content-Length')];
delete opts.headers[getHeaderName(opts.headers, 'Transfer-Encoding')];
} }
if (opts.removeRefererHeader && opts.headers) { if (opts.removeRefererHeader && opts.headers) {
delete opts.headers.referer; delete opts.headers.referer;
@ -181,7 +187,7 @@ function setDefaults(defs) {
finalOpts[key] = opts.uri[key]; finalOpts[key] = opts.uri[key];
}); });
finalOpts.method = opts.method; finalOpts.method = opts.method;
finalOpts.headers = opts.headers; finalOpts.headers = JSON.parse(JSON.stringify(opts.headers));
if (_body) { if (_body) {
// Most APIs expect (or require) Content-Length except in the case of multipart uploads // Most APIs expect (or require) Content-Length except in the case of multipart uploads
// Transfer-Encoding: Chunked (the default) is generally only well-supported downstream // Transfer-Encoding: Chunked (the default) is generally only well-supported downstream
@ -353,7 +359,9 @@ function setDefaults(defs) {
} else { } else {
reqOpts.method = (reqOpts.method || 'GET').toUpperCase(); reqOpts.method = (reqOpts.method || 'GET').toUpperCase();
} }
reqOpts.headers = reqOpts.headers || {}; if (!reqOpts.headers) {
reqOpts.headers = {};
}
// crazy case for easier testing // crazy case for easier testing
if (!hasHeader(reqOpts, 'CoNTeNT-TyPe')) { if (!hasHeader(reqOpts, 'CoNTeNT-TyPe')) {

View File

@ -1,6 +1,6 @@
{ {
"name": "@coolaj86/urequest", "name": "@coolaj86/urequest",
"version": "1.3.3", "version": "1.3.4",
"description": "A lightweight drop-in replacement for request", "description": "A lightweight drop-in replacement for request",
"main": "index.js", "main": "index.js",
"files": [ "files": [