From d332600149314f7ef9e7bb44ef8ef2b5d8270420 Mon Sep 17 00:00:00 2001 From: Nicholas O'Leary Date: Tue, 1 Oct 2013 17:03:17 +0100 Subject: [PATCH 1/2] Add new http request node --- nodes/io/21-httpin.html | 91 ++++++++++++++++++++++++++++++++++++---- nodes/io/21-httpin.js | 64 +++++++++++++++++++++++++--- nodes/io/90-httpget.html | 2 + 3 files changed, 145 insertions(+), 12 deletions(-) diff --git a/nodes/io/21-httpin.html b/nodes/io/21-httpin.html index 3e438f6ec..6bf97615c 100644 --- a/nodes/io/21-httpin.html +++ b/nodes/io/21-httpin.html @@ -76,7 +76,7 @@ outputs:1, icon: "white-globe.png", label: function() { - return this.name||"["+this.method+"] "+this.url; + return this.name||(this.url?("["+this.method+"] "+this.url):"http"); }, labelStyle: function() { return this.name?"node_label_italic":""; @@ -85,14 +85,19 @@ + + + + + + + + + + + + + + diff --git a/nodes/io/21-httpin.js b/nodes/io/21-httpin.js index 95a57b5cd..c3e3a027b 100644 --- a/nodes/io/21-httpin.js +++ b/nodes/io/21-httpin.js @@ -16,6 +16,10 @@ var RED = require("../../red/red"); var util = require("util"); +var http = require("http"); +var https = require("https"); +var urllib = require("url"); +var bodyParser = require("express").bodyParser(); function HTTPIn(n) { RED.nodes.createNode(this,n); @@ -24,14 +28,15 @@ function HTTPIn(n) { var node = this; this.callback = function(req,res) { + console.log(arguments.length); node.send({req:req,res:res}); } if (this.method == "get") { RED.app.get(this.url,this.callback); } else if (this.method == "post") { - RED.app.post(this.url,this.callback); + RED.app.post(this.url,bodyParser,this.callback); } else if (this.method == "put") { - RED.app.put(this.url,this.callback); + RED.app.put(this.url,bodyParser,this.callback); } else if (this.method == "delete") { RED.app.delete(this.url,this.callback); } @@ -41,7 +46,7 @@ function HTTPIn(n) { for (var i in routes) { if (routes[i].path == this.url) { routes.splice(i,1); - break; + //break; } } }); @@ -58,10 +63,59 @@ function HTTPOut(n) { if (msg.headers) { res.set(msg.headers); } - var rc = msg.rc || 200; - msg.res.send(rc,msg.payload); + var statusCode = msg.statusCode || 200; + msg.res.send(statusCode,msg.payload); } }); } RED.nodes.registerType("http response",HTTPOut); + +function HTTPRequest(n) { + RED.nodes.createNode(this,n); + var url = n.url; + var method = n.method || "GET"; + var httplib = (/^https/.test(url))?https:http; + var node = this; + this.on("input",function(msg) { + + var opts = urllib.parse(msg.url||url); + opts.method = msg.method||method; + if (msg.headers) { + opts.header = headers; + } + var req = httplib.request(opts,function(res) { + res.setEncoding('utf8'); + var message = { + statusCode: res.statusCode, + headers: res.headers, + payload: "" + }; + res.on('data',function(chunk) { + message.payload += chunk; + }); + res.on('end',function() { + node.send(message); + }); + }); + req.on('error',function(err) { + msg.payload = err.toString(); + msg.statusCode = err.code; + node.send(msg); + }); + if (msg.payload) { + if (typeof msg.payload === "string" || Buffer.isBuffer(msg.payload)) { + req.write(msg.payload); + } else if (typeof msg.payload == "number") { + req.write(msg.payload+""); + } else { + req.write(JSON.stringify(msg.payload)); + } + } + req.end(); + + + }); +} + +RED.nodes.registerType("http request",HTTPRequest); diff --git a/nodes/io/90-httpget.html b/nodes/io/90-httpget.html index 65be8202f..643c35c31 100644 --- a/nodes/io/90-httpget.html +++ b/nodes/io/90-httpget.html @@ -15,6 +15,8 @@ --> diff --git a/nodes/social/57-prowl.js b/nodes/social/57-prowl.js index 4874eaa49..675fead36 100644 --- a/nodes/social/57-prowl.js +++ b/nodes/social/57-prowl.js @@ -16,17 +16,23 @@ var RED = require("../../red/red"); var Prowl = require('node-prowl'); +var util = require('util'); -// pushkey.js just needs to be like (with the quotes) -// module.exports = {prowl:'My-API-KEY'} +// Either add a line like this to settings.js +// prowl: {prowlkey:'My-API-KEY'}, +// or create pushkey.js in dir ABOVE node-red, it just needs to be like +// module.exports = {prowlkey:'My-API-KEY'} try { var pushkey = require("../../settings").prowl || require("../../../pushkey.js"); -} catch(err) { - throw new Error("Failed to load Prowl credentials"); +} +catch(err) { + util.log("[57-prowl.js] Error: Failed to load Prowl credentials"); } -var prowl = new Prowl(pushkey.prowl); +if (pushkey) { + var prowl = new Prowl(pushkey.prowlkey); +} function ProwlNode(n) { RED.nodes.createNode(this,n); @@ -41,14 +47,19 @@ function ProwlNode(n) { if (typeof(msg.payload) == 'object') { msg.payload = JSON.stringify(msg.payload); } - try { - prowl.push(msg.payload, titl, { priority: pri }, function( err, remaining ){ - if ( err ) node.error(err); - node.log( remaining + ' calls to Prowl api during current hour.' ); - }); + if (pushkey) { + try { + prowl.push(msg.payload, titl, { priority: pri }, function(err, remaining) { + if (err) node.error(err); + node.log( remaining + ' calls to Prowl api during current hour.' ); + }); + } + catch (err) { + node.error(err); + } } - catch (err) { - node.error(err); + else { + node.warn("Prowl credentials not set/found. See node info."); } }); } diff --git a/nodes/social/57-pushbullet.html b/nodes/social/57-pushbullet.html index 1d082d92e..320202970 100644 --- a/nodes/social/57-pushbullet.html +++ b/nodes/social/57-pushbullet.html @@ -16,13 +16,9 @@