1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Better handle httprequest header capitalisation

This commit is contained in:
Dave Conway-Jones 2016-09-30 22:12:33 +01:00
parent 306825aa90
commit 2da9572a45

View File

@ -74,6 +74,8 @@ module.exports = function(RED) {
var opts = urllib.parse(url);
opts.method = method;
opts.headers = {};
var ctSet = "Content-Type"; // set default camel case
var clSet = "Content-Length";
if (msg.headers) {
for (var v in msg.headers) {
if (msg.headers.hasOwnProperty(v)) {
@ -83,6 +85,8 @@ module.exports = function(RED) {
// function. Otherwise leave them alone.
name = v;
}
else if (name === 'content-type') { ctSet = v; }
else { clSet = v; }
opts.headers[name] = msg.headers[v];
}
}
@ -103,18 +107,27 @@ module.exports = function(RED) {
} else {
payload = JSON.stringify(msg.payload);
if (opts.headers['content-type'] == null) {
opts.headers['content-type'] = "application/json";
opts.headers[ctSet] = "application/json";
}
}
}
if (opts.headers['content-length'] == null) {
if (Buffer.isBuffer(payload)) {
opts.headers['content-length'] = payload.length;
opts.headers[clSet] = payload.length;
} else {
opts.headers['content-length'] = Buffer.byteLength(payload);
opts.headers[clSet] = Buffer.byteLength(payload);
}
}
}
// revert to user supplied Capitalisation if needed.
if (opts.headers.hasOwnProperty('content-type') && (ctSet !== 'content-type')) {
opts.headers[ctSet] = opts.headers['content-type'];
delete opts.headers['content-type'];
}
if (opts.headers.hasOwnProperty('content-length') && (clSet !== 'content-length')) {
opts.headers[clSet] = opts.headers['content-length'];
delete opts.headers['content-length'];
}
var urltotest = url;
var noproxy;
if (noprox) {
@ -135,7 +148,6 @@ module.exports = function(RED) {
opts.path = opts.pathname = path;
opts.headers = heads;
opts.method = method;
//console.log(opts);
urltotest = match[0];
}
else { node.warn("Bad proxy url: "+process.env.http_proxy); }