Fix to allow blank password for Basic Auth in httprequest node

This commit is contained in:
Dave C-J 2014-03-31 10:28:37 +01:00
parent a3fa6dada5
commit 1f6155f118
1 changed files with 35 additions and 47 deletions

View File

@ -30,9 +30,9 @@ function rawBodyParser(req, res, next) {
req.body = ""; req.body = "";
req._body = true; req._body = true;
getBody(req, { getBody(req, {
limit: '1mb', limit: '1mb',
length: req.headers['content-length'], length: req.headers['content-length'],
encoding: 'utf8' encoding: 'utf8'
}, function (err, buf) { }, function (err, buf) {
if (err) return next(err); if (err) return next(err);
req.body = buf; req.body = buf;
@ -49,12 +49,12 @@ function HTTPIn(n) {
this.method = n.method; this.method = n.method;
var node = this; var node = this;
this.errorHandler = function(err,req,res,next) { this.errorHandler = function(err,req,res,next) {
node.warn(err); node.warn(err);
res.send(500); res.send(500);
}; };
this.callback = function(req,res) { this.callback = function(req,res) {
if (node.method == "post") { if (node.method == "post") {
node.send({req:req,res:res,payload:req.body}); node.send({req:req,res:res,payload:req.body});
@ -64,14 +64,14 @@ function HTTPIn(n) {
node.send({req:req,res:res}); node.send({req:req,res:res});
} }
} }
var corsHandler = function(req,res,next) { next(); } var corsHandler = function(req,res,next) { next(); }
if (RED.settings.httpNodeCors) { if (RED.settings.httpNodeCors) {
corsHandler = cors(RED.settings.httpNodeCors); corsHandler = cors(RED.settings.httpNodeCors);
RED.httpNode.options(this.url,corsHandler); RED.httpNode.options(this.url,corsHandler);
} }
if (this.method == "get") { if (this.method == "get") {
RED.httpNode.get(this.url,corsHandler,this.callback,this.errorHandler); RED.httpNode.get(this.url,corsHandler,this.callback,this.errorHandler);
} else if (this.method == "post") { } else if (this.method == "post") {
@ -81,7 +81,7 @@ function HTTPIn(n) {
} else if (this.method == "delete") { } else if (this.method == "delete") {
RED.httpNode.delete(this.url,corsHandler,this.callback,errorHandler); RED.httpNode.delete(this.url,corsHandler,this.callback,errorHandler);
} }
this.on("close",function() { this.on("close",function() {
var routes = RED.httpNode.routes[this.method]; var routes = RED.httpNode.routes[this.method];
for (var i = 0; i<routes.length; i++) { for (var i = 0; i<routes.length; i++) {
@ -104,7 +104,6 @@ function HTTPIn(n) {
this.warn("Cannot create http-in node when httpNodeRoot set to false"); this.warn("Cannot create http-in node when httpNodeRoot set to false");
} }
} }
RED.nodes.registerType("http in",HTTPIn); RED.nodes.registerType("http in",HTTPIn);
@ -112,22 +111,21 @@ function HTTPOut(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
var node = this; var node = this;
this.on("input",function(msg) { this.on("input",function(msg) {
if (msg.res) { if (msg.res) {
if (msg.headers) { if (msg.headers) {
msg.res.set(msg.headers); msg.res.set(msg.headers);
}
var statusCode = msg.statusCode || 200;
if (typeof msg.payload == "object" && !Buffer.isBuffer(msg.payload)) {
msg.res.jsonp(statusCode,msg.payload);
} else {
msg.res.send(statusCode,msg.payload);
}
} else {
node.warn("No response object");
} }
var statusCode = msg.statusCode || 200;
if (typeof msg.payload == "object" && !Buffer.isBuffer(msg.payload)) {
msg.res.jsonp(statusCode,msg.payload);
} else {
msg.res.send(statusCode,msg.payload);
}
} else {
node.warn("No response object");
}
}); });
} }
RED.nodes.registerType("http response",HTTPOut); RED.nodes.registerType("http response",HTTPOut);
function HTTPRequest(n) { function HTTPRequest(n) {
@ -136,12 +134,7 @@ function HTTPRequest(n) {
var nodeMethod = n.method || "GET"; var nodeMethod = n.method || "GET";
var node = this; var node = this;
var credentials = RED.nodes.getCredentials(n.id); var credentials = RED.nodes.getCredentials(n.id);
if (credentials) {
this.username = credentials.user;
this.password = credentials.password;
}
this.on("input",function(msg) { this.on("input",function(msg) {
var url = msg.url||nodeUrl; var url = msg.url||nodeUrl;
var method = (msg.method||nodeMethod).toUpperCase(); var method = (msg.method||nodeMethod).toUpperCase();
var opts = urllib.parse(url); var opts = urllib.parse(url);
@ -150,24 +143,24 @@ function HTTPRequest(n) {
opts.headers = msg.headers; opts.headers = msg.headers;
} }
if (credentials) { if (credentials) {
opts.auth = credentials.user+":"+credentials.password; opts.auth = credentials.user+":"+(credentials.password||"");
} }
var req = ((/^https/.test(url))?https:http).request(opts,function(res) { var req = ((/^https/.test(url))?https:http).request(opts,function(res) {
res.setEncoding('utf8'); res.setEncoding('utf8');
msg.statusCode = res.statusCode; msg.statusCode = res.statusCode;
msg.headers = res.headers; msg.headers = res.headers;
msg.payload = ""; msg.payload = "";
res.on('data',function(chunk) { res.on('data',function(chunk) {
msg.payload += chunk; msg.payload += chunk;
}); });
res.on('end',function() { res.on('end',function() {
node.send(msg); node.send(msg);
}); });
}); });
req.on('error',function(err) { req.on('error',function(err) {
msg.payload = err.toString(); msg.payload = err.toString();
msg.statusCode = err.code; msg.statusCode = err.code;
node.send(msg); node.send(msg);
}); });
if (msg.payload && (method == "POST" || method == "PUT") ) { if (msg.payload && (method == "POST" || method == "PUT") ) {
if (typeof msg.payload === "string" || Buffer.isBuffer(msg.payload)) { if (typeof msg.payload === "string" || Buffer.isBuffer(msg.payload)) {
@ -179,11 +172,8 @@ function HTTPRequest(n) {
} }
} }
req.end(); req.end();
}); });
} }
RED.nodes.registerType("http request",HTTPRequest); RED.nodes.registerType("http request",HTTPRequest);
var querystring = require('querystring'); var querystring = require('querystring');
@ -224,5 +214,3 @@ RED.httpAdmin.post('/http-request/:id',function(req,res) {
res.send(200); res.send(200);
}); });
}); });