redirect and POST bugfixes
This commit is contained in:
parent
1a1713bea1
commit
33ab916082
71
index.js
71
index.js
|
@ -30,6 +30,7 @@ function mergeOrDelete(defaults, updates) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function toJSONifier(keys) {
|
function toJSONifier(keys) {
|
||||||
|
|
||||||
return function () {
|
return function () {
|
||||||
var obj = {};
|
var obj = {};
|
||||||
var me = this;
|
var me = this;
|
||||||
|
@ -50,7 +51,7 @@ function setDefaults(defs) {
|
||||||
defs = defs || {};
|
defs = defs || {};
|
||||||
|
|
||||||
function urequestHelper(opts, cb) {
|
function urequestHelper(opts, cb) {
|
||||||
debug("[urequest] processed options:");
|
debug("\n[urequest] processed options:");
|
||||||
debug(opts);
|
debug(opts);
|
||||||
|
|
||||||
function onResponse(resp) {
|
function onResponse(resp) {
|
||||||
|
@ -72,10 +73,10 @@ function setDefaults(defs) {
|
||||||
resp.request.headers = opts.headers;
|
resp.request.headers = opts.headers;
|
||||||
resp.request.toJSON = toJSONifier([ 'uri', 'method', 'headers' ]);
|
resp.request.toJSON = toJSONifier([ 'uri', 'method', 'headers' ]);
|
||||||
|
|
||||||
if (resp.headers.location && opts.followRedirect) {
|
if (followRedirect && resp.headers.location && -1 !== [ 301, 302 ].indexOf(resp.statusCode)) {
|
||||||
debug('Following redirect: ' + resp.headers.location);
|
debug('Following redirect: ' + resp.headers.location);
|
||||||
if (opts.followAllRedirects || -1 !== [ 301, 302 ].indexOf(resp.statusCode)) {
|
if ('GET' !== opts.method && !opts.followAllRedirects) {
|
||||||
followRedirect = true;
|
followRedirect = false;
|
||||||
}
|
}
|
||||||
if (opts._redirectCount >= opts.maxRedirects) {
|
if (opts._redirectCount >= opts.maxRedirects) {
|
||||||
followRedirect = false;
|
followRedirect = false;
|
||||||
|
@ -85,18 +86,18 @@ function setDefaults(defs) {
|
||||||
followRedirect = false;
|
followRedirect = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!opts.followOriginalHttpMethod) {
|
|
||||||
opts.method = 'GET';
|
|
||||||
opts.body = null;
|
|
||||||
}
|
|
||||||
if (followRedirect) {
|
if (followRedirect) {
|
||||||
|
if (!opts.followOriginalHttpMethod) {
|
||||||
|
opts.method = 'GET';
|
||||||
|
opts.body = null;
|
||||||
|
}
|
||||||
|
if (opts.removeRefererHeader && opts.headers) {
|
||||||
|
delete opts.headers.referer;
|
||||||
|
}
|
||||||
opts.url = resp.headers.location;
|
opts.url = resp.headers.location;
|
||||||
opts.uri = url.parse(opts.url);
|
opts.uri = url.parse(opts.url);
|
||||||
return urequestHelper(opts, cb);
|
return urequestHelper(opts, cb);
|
||||||
}
|
}
|
||||||
if (opts.removeRefererHeader && opts.headers) {
|
|
||||||
delete opts.headers.referer;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (null === opts.encoding) {
|
if (null === opts.encoding) {
|
||||||
resp._body = [];
|
resp._body = [];
|
||||||
|
@ -130,25 +131,51 @@ function setDefaults(defs) {
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
debug("\n[urequest] resp.toJSON():");
|
||||||
|
debug(resp.toJSON());
|
||||||
cb(null, resp, resp.body);
|
cb(null, resp, resp.body);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var req;
|
var req;
|
||||||
var finalOpts = {};
|
var finalOpts = {};
|
||||||
|
var _body;
|
||||||
|
|
||||||
|
if (opts.body) {
|
||||||
|
if (true === opts.json) {
|
||||||
|
_body = JSON.stringify(opts.body);
|
||||||
|
} else {
|
||||||
|
_body = opts.body;
|
||||||
|
}
|
||||||
|
} else if (opts.json && true !== opts.json) {
|
||||||
|
_body = JSON.stringify(opts.json);
|
||||||
|
}
|
||||||
|
if ('string' === typeof _body) {
|
||||||
|
_body = Buffer.from(_body);
|
||||||
|
}
|
||||||
|
|
||||||
Object.keys(opts.uri).forEach(function (key) {
|
Object.keys(opts.uri).forEach(function (key) {
|
||||||
finalOpts[key] = opts.uri[key];
|
finalOpts[key] = opts.uri[key];
|
||||||
});
|
});
|
||||||
finalOpts.method = opts.method;
|
finalOpts.method = opts.method;
|
||||||
finalOpts.headers = opts.headers;
|
finalOpts.headers = opts.headers;
|
||||||
|
if (_body) {
|
||||||
|
// Most APIs expect (or require) Content-Length except in the case of multipart uploads
|
||||||
|
// chunked is generally only well-supported downstream
|
||||||
|
//finalOpts.headers['Content-Length'] = _body.byteLength || _body.length;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO support unix sockets
|
// TODO support unix sockets
|
||||||
if ('https:' === finalOpts.protocol) {
|
if ('https:' === finalOpts.protocol) {
|
||||||
// https://nodejs.org/api/https.html#https_https_request_options_callback
|
// https://nodejs.org/api/https.html#https_https_request_options_callback
|
||||||
|
debug("\n[urequest] https.request(opts):");
|
||||||
|
debug(finalOpts);
|
||||||
req = https.request(finalOpts, onResponse);
|
req = https.request(finalOpts, onResponse);
|
||||||
} else if ('http:' === finalOpts.protocol) {
|
} else if ('http:' === finalOpts.protocol) {
|
||||||
// https://nodejs.org/api/http.html#http_http_request_options_callback
|
// https://nodejs.org/api/http.html#http_http_request_options_callback
|
||||||
|
debug("\n[urequest] http.request(opts):");
|
||||||
|
debug(finalOpts);
|
||||||
req = http.request(finalOpts, onResponse);
|
req = http.request(finalOpts, onResponse);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("unknown protocol: '" + opts.uri.protocol + "'");
|
throw new Error("unknown protocol: '" + opts.uri.protocol + "'");
|
||||||
|
@ -158,20 +185,20 @@ function setDefaults(defs) {
|
||||||
cb(e);
|
cb(e);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (opts.body) {
|
if (_body) {
|
||||||
if (true === opts.json) {
|
debug("\n[urequest] body");
|
||||||
req.write(JSON.stringify(opts.json));
|
debug(_body);
|
||||||
} else {
|
// used for chunked encoding
|
||||||
req.write(opts.body);
|
//req.write(_body);
|
||||||
}
|
// used for known content-length
|
||||||
} else if (opts.json && true !== opts.json) {
|
req.end(_body);
|
||||||
req.write(JSON.stringify(opts.json));
|
} else {
|
||||||
|
req.end();
|
||||||
}
|
}
|
||||||
req.end();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function urequest(opts, cb) {
|
function urequest(opts, cb) {
|
||||||
debug("[urequest] received options:");
|
debug("\n[urequest] received options:");
|
||||||
debug(opts);
|
debug(opts);
|
||||||
var reqOpts = {};
|
var reqOpts = {};
|
||||||
// request.js behavior:
|
// request.js behavior:
|
||||||
|
@ -201,7 +228,7 @@ function setDefaults(defs) {
|
||||||
//reqOpts.uri = url.parse(reqOpts.url);
|
//reqOpts.uri = url.parse(reqOpts.url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
reqOpts.method = opts.method || 'GET';
|
reqOpts.method = (opts.method || 'GET').toUpperCase();
|
||||||
reqOpts.headers = opts.headers || {};
|
reqOpts.headers = opts.headers || {};
|
||||||
if ((true === reqOpts.json && reqOpts.body) || reqOpts.json) {
|
if ((true === reqOpts.json && reqOpts.body) || reqOpts.json) {
|
||||||
reqOpts.headers['Content-Type'] = 'application/json';
|
reqOpts.headers['Content-Type'] = 'application/json';
|
||||||
|
|
Loading…
Reference in New Issue