From 3defd84af4bd3cb473a6ddd79bcb0894bafcd297 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 7 Jul 2018 19:46:21 -0600 Subject: [PATCH] v1.3.4: bugfix keep separate headers and don't keep Content-Length on redirect --- index.js | 16 ++++++++++++---- package.json | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 73a2f84..e2577e7 100644 --- a/index.js +++ b/index.js @@ -29,13 +29,17 @@ function mergeOrDelete(defaults, updates) { return updates; } -function hasHeader(reqOpts, header) { +function getHeaderName(reqOpts, header) { var headers = {}; 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()]; } +function hasHeader(reqOpts, header) { + return 'undefined' !== typeof getHeaderName(reqOpts, header); +} function toJSONifier(keys) { @@ -98,6 +102,8 @@ function setDefaults(defs) { if (!opts.followOriginalHttpMethod) { opts.method = 'GET'; opts.body = null; + delete opts.headers[getHeaderName(opts.headers, 'Content-Length')]; + delete opts.headers[getHeaderName(opts.headers, 'Transfer-Encoding')]; } if (opts.removeRefererHeader && opts.headers) { delete opts.headers.referer; @@ -181,7 +187,7 @@ function setDefaults(defs) { finalOpts[key] = opts.uri[key]; }); finalOpts.method = opts.method; - finalOpts.headers = opts.headers; + finalOpts.headers = JSON.parse(JSON.stringify(opts.headers)); if (_body) { // 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 @@ -353,7 +359,9 @@ function setDefaults(defs) { } else { reqOpts.method = (reqOpts.method || 'GET').toUpperCase(); } - reqOpts.headers = reqOpts.headers || {}; + if (!reqOpts.headers) { + reqOpts.headers = {}; + } // crazy case for easier testing if (!hasHeader(reqOpts, 'CoNTeNT-TyPe')) { diff --git a/package.json b/package.json index 6ae2def..a5364af 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@coolaj86/urequest", - "version": "1.3.3", + "version": "1.3.4", "description": "A lightweight drop-in replacement for request", "main": "index.js", "files": [