From 381814d9a7085064d3a0e765afd940a1f12b12ec Mon Sep 17 00:00:00 2001 From: Steve-Mcl Date: Mon, 7 Mar 2022 15:55:48 +0000 Subject: [PATCH 1/8] Add missing HTTP methods --- .../@node-red/nodes/core/network/21-httpin.html | 4 ++++ .../@node-red/nodes/core/network/21-httpin.js | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/node_modules/@node-red/nodes/core/network/21-httpin.html b/packages/node_modules/@node-red/nodes/core/network/21-httpin.html index 450bd32cc..cb652f303 100644 --- a/packages/node_modules/@node-red/nodes/core/network/21-httpin.html +++ b/packages/node_modules/@node-red/nodes/core/network/21-httpin.html @@ -23,6 +23,10 @@ + + + +
diff --git a/packages/node_modules/@node-red/nodes/core/network/21-httpin.js b/packages/node_modules/@node-red/nodes/core/network/21-httpin.js index b458a459c..50bcf6eed 100644 --- a/packages/node_modules/@node-red/nodes/core/network/21-httpin.js +++ b/packages/node_modules/@node-red/nodes/core/network/21-httpin.js @@ -171,7 +171,9 @@ module.exports = function(RED) { if (RED.settings.httpNodeCors) { corsHandler = cors(RED.settings.httpNodeCors); - RED.httpNode.options("*",corsHandler); + RED.httpNode.options("*", function(req,res,next) { + corsHandler(req,res,next); + }); } function HTTPIn(n) { @@ -261,6 +263,16 @@ module.exports = function(RED) { RED.httpNode.patch(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler); } else if (this.method == "delete") { RED.httpNode.delete(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler); + } else if (this.method == "head") { + RED.httpNode.head(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,this.callback,this.errorHandler); + } else if (this.method == "options") { + RED.httpNode.options(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,function name(req,res,next) { + this.callback(req,res,next) + },this.errorHandler); + } else if (this.method == "trace") { + RED.httpNode.trace(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,this.callback,this.errorHandler); + } else if (this.method == "connect") { + RED.httpNode.connect(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,this.callback,this.errorHandler); } this.on("close",function() { From 8366908939b63b0faf279050fd0d3623b230e3c4 Mon Sep 17 00:00:00 2001 From: Steve-Mcl Date: Tue, 8 Mar 2022 23:36:33 +0000 Subject: [PATCH 2/8] handle options per route --- .../@node-red/nodes/core/network/21-httpin.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/network/21-httpin.js b/packages/node_modules/@node-red/nodes/core/network/21-httpin.js index 50bcf6eed..94e5c8e1c 100644 --- a/packages/node_modules/@node-red/nodes/core/network/21-httpin.js +++ b/packages/node_modules/@node-red/nodes/core/network/21-httpin.js @@ -172,6 +172,12 @@ module.exports = function(RED) { if (RED.settings.httpNodeCors) { corsHandler = cors(RED.settings.httpNodeCors); RED.httpNode.options("*", function(req,res,next) { + //see if any routes for this path exist & call next() otherwise call corsHandler + const routes = RED.httpNode._router.stack.filter(e => e.route && e.route.path == req.path && e.route.methods.options === true); + if(routes.length > 0) { + next(); + return + } corsHandler(req,res,next); }); } @@ -266,9 +272,7 @@ module.exports = function(RED) { } else if (this.method == "head") { RED.httpNode.head(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,this.callback,this.errorHandler); } else if (this.method == "options") { - RED.httpNode.options(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,function name(req,res,next) { - this.callback(req,res,next) - },this.errorHandler); + RED.httpNode.options(this.url,cookieParser(),httpMiddleware,metricsHandler,this.callback,this.errorHandler); } else if (this.method == "trace") { RED.httpNode.trace(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,this.callback,this.errorHandler); } else if (this.method == "connect") { From 0870c2ce80f042270c592927cd7a98e1eb44538d Mon Sep 17 00:00:00 2001 From: Steve-Mcl Date: Thu, 10 Mar 2022 07:55:27 +0000 Subject: [PATCH 3/8] trace must set header Content-Type 'message/http' --- .../node_modules/@node-red/nodes/core/network/21-httpin.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/node_modules/@node-red/nodes/core/network/21-httpin.js b/packages/node_modules/@node-red/nodes/core/network/21-httpin.js index 94e5c8e1c..98bce6e26 100644 --- a/packages/node_modules/@node-red/nodes/core/network/21-httpin.js +++ b/packages/node_modules/@node-red/nodes/core/network/21-httpin.js @@ -212,6 +212,9 @@ module.exports = function(RED) { node.send({_msgid:msgid,req:req,res:createResponseWrapper(node,res),payload:req.body}); } else if (node.method == "get") { node.send({_msgid:msgid,req:req,res:createResponseWrapper(node,res),payload:req.query}); + } else if (node.method == "trace") { + res.set('Content-Type', 'message/http') + node.send({_msgid:msgid,req:req,res:createResponseWrapper(node,res)}); } else { node.send({_msgid:msgid,req:req,res:createResponseWrapper(node,res)}); } From 4e58dd145f160a78102b8c9195fba35257272339 Mon Sep 17 00:00:00 2001 From: Steve-Mcl Date: Thu, 10 Mar 2022 07:56:43 +0000 Subject: [PATCH 4/8] remove CONNECT method... - not usefull at this time --- .../@node-red/nodes/core/network/21-httpin.html | 1 - .../@node-red/nodes/core/network/21-httpin.js | 8 +------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/network/21-httpin.html b/packages/node_modules/@node-red/nodes/core/network/21-httpin.html index cb652f303..207c24f18 100644 --- a/packages/node_modules/@node-red/nodes/core/network/21-httpin.html +++ b/packages/node_modules/@node-red/nodes/core/network/21-httpin.html @@ -26,7 +26,6 @@ -
diff --git a/packages/node_modules/@node-red/nodes/core/network/21-httpin.js b/packages/node_modules/@node-red/nodes/core/network/21-httpin.js index 98bce6e26..b782bb42a 100644 --- a/packages/node_modules/@node-red/nodes/core/network/21-httpin.js +++ b/packages/node_modules/@node-red/nodes/core/network/21-httpin.js @@ -272,14 +272,8 @@ module.exports = function(RED) { RED.httpNode.patch(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler); } else if (this.method == "delete") { RED.httpNode.delete(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler); - } else if (this.method == "head") { - RED.httpNode.head(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,this.callback,this.errorHandler); - } else if (this.method == "options") { - RED.httpNode.options(this.url,cookieParser(),httpMiddleware,metricsHandler,this.callback,this.errorHandler); - } else if (this.method == "trace") { + } else if (this.method == "trace") { // https://httpwg.org/specs/rfc7231.html#rfc.section.4.3.8 RED.httpNode.trace(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,this.callback,this.errorHandler); - } else if (this.method == "connect") { - RED.httpNode.connect(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,this.callback,this.errorHandler); } this.on("close",function() { From af899b9fc37691e066a58abe5e923b148d010258 Mon Sep 17 00:00:00 2001 From: Steve-Mcl Date: Thu, 10 Mar 2022 07:58:38 +0000 Subject: [PATCH 5/8] TRACE should return orig msg in body... - https://httpwg.org/specs/rfc7231.html#rfc.section.4.3.8 --- .../@node-red/nodes/core/network/21-httpin.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/network/21-httpin.js b/packages/node_modules/@node-red/nodes/core/network/21-httpin.js index b782bb42a..1034012ec 100644 --- a/packages/node_modules/@node-red/nodes/core/network/21-httpin.js +++ b/packages/node_modules/@node-red/nodes/core/network/21-httpin.js @@ -205,18 +205,24 @@ module.exports = function(RED) { res.sendStatus(500); }; - this.callback = function(req,res) { - var msgid = RED.util.generateId(); + this.callback = function (req, res, next) { + const msgid = RED.util.generateId(); + const resWrap = createResponseWrapper(node, res); res._msgid = msgid; if (node.method.match(/^(post|delete|put|options|patch)$/)) { - node.send({_msgid:msgid,req:req,res:createResponseWrapper(node,res),payload:req.body}); + node.send({ _msgid: msgid, req: req, res: resWrap, payload: req.body }); } else if (node.method == "get") { - node.send({_msgid:msgid,req:req,res:createResponseWrapper(node,res),payload:req.query}); + node.send({ _msgid: msgid, req: req, res: resWrap, payload: req.query }); } else if (node.method == "trace") { + // https://httpwg.org/specs/rfc7231.html#rfc.section.4.3.8 res.set('Content-Type', 'message/http') - node.send({_msgid:msgid,req:req,res:createResponseWrapper(node,res)}); + // Add REQ string to body (e.g. TRACE / HTTP/1.1) + let pl = `TRACE ${req.path} ${req.protocol.toUpperCase()}/${req.httpVersion}\n`; + // Add REQ headers to body + pl += Object.entries(req.headers).map(e => e.join(": ")).join("\n"); + node.send({ _msgid: msgid, req: req, res: resWrap, payload: pl }); } else { - node.send({_msgid:msgid,req:req,res:createResponseWrapper(node,res)}); + node.send({ _msgid: msgid, req: req, res: resWrap }); } }; From f49b40cc3a1d504c7d5e31f0a4e4f8faf5f75b41 Mon Sep 17 00:00:00 2001 From: Steve-Mcl Date: Thu, 10 Mar 2022 08:01:17 +0000 Subject: [PATCH 6/8] Fix sub-path OPTIONS routes not called --- .../@node-red/nodes/core/network/21-httpin.js | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/network/21-httpin.js b/packages/node_modules/@node-red/nodes/core/network/21-httpin.js index 1034012ec..1986970a1 100644 --- a/packages/node_modules/@node-red/nodes/core/network/21-httpin.js +++ b/packages/node_modules/@node-red/nodes/core/network/21-httpin.js @@ -171,17 +171,18 @@ module.exports = function(RED) { if (RED.settings.httpNodeCors) { corsHandler = cors(RED.settings.httpNodeCors); - RED.httpNode.options("*", function(req,res,next) { - //see if any routes for this path exist & call next() otherwise call corsHandler - const routes = RED.httpNode._router.stack.filter(e => e.route && e.route.path == req.path && e.route.methods.options === true); - if(routes.length > 0) { - next(); - return - } - corsHandler(req,res,next); - }); } + RED.httpNode.options("*", function(req,res,next) { + //see if any routes for this path exist & call next() otherwise call corsHandler + const routes = RED.httpNode._router.stack.filter(e => e.route && e.route.path == req.path && e.route.methods.options === true); + if(routes.length > 0) { + next(); + return + } + corsHandler(req,res,next); + }); + function HTTPIn(n) { RED.nodes.createNode(this,n); if (RED.settings.httpNodeRoot !== false) { @@ -278,6 +279,8 @@ module.exports = function(RED) { RED.httpNode.patch(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler); } else if (this.method == "delete") { RED.httpNode.delete(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler); + } else if (this.method == "options") { // https://httpwg.org/specs/rfc7231.html#rfc.section.4.3.7 + RED.httpNode.options(this.url,cookieParser(),httpMiddleware,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler); } else if (this.method == "trace") { // https://httpwg.org/specs/rfc7231.html#rfc.section.4.3.8 RED.httpNode.trace(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,this.callback,this.errorHandler); } From c967f3526fdf9450f1df873eef3a89ad24064cd0 Mon Sep 17 00:00:00 2001 From: Steve-Mcl Date: Thu, 10 Mar 2022 08:01:32 +0000 Subject: [PATCH 7/8] Add HEAD method --- packages/node_modules/@node-red/nodes/core/network/21-httpin.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/node_modules/@node-red/nodes/core/network/21-httpin.js b/packages/node_modules/@node-red/nodes/core/network/21-httpin.js index 1986970a1..09f13d844 100644 --- a/packages/node_modules/@node-red/nodes/core/network/21-httpin.js +++ b/packages/node_modules/@node-red/nodes/core/network/21-httpin.js @@ -271,6 +271,8 @@ module.exports = function(RED) { if (this.method == "get") { RED.httpNode.get(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,this.callback,this.errorHandler); + } else if (this.method == "head") { // https://httpwg.org/specs/rfc7231.html#rfc.section.4.3.2 + RED.httpNode.head(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,this.callback,this.errorHandler); } else if (this.method == "post") { RED.httpNode.post(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,multipartParser,rawBodyParser,this.callback,this.errorHandler); } else if (this.method == "put") { From 64b4ecbcaa6f2dda7f2147f6c29d8243be672eea Mon Sep 17 00:00:00 2001 From: Steve-Mcl Date: Thu, 10 Mar 2022 19:00:27 +0000 Subject: [PATCH 8/8] update html help --- .../nodes/locales/en-US/network/21-httpin.html | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/locales/en-US/network/21-httpin.html b/packages/node_modules/@node-red/nodes/locales/en-US/network/21-httpin.html index d3f1984c4..ef38c8429 100644 --- a/packages/node_modules/@node-red/nodes/locales/en-US/network/21-httpin.html +++ b/packages/node_modules/@node-red/nodes/locales/en-US/network/21-httpin.html @@ -19,8 +19,18 @@

Outputs

payload
-
For a GET request, contains an object of any query string parameters. - Otherwise, contains the body of the HTTP request.
+
+
    +
  • GET - payload contains an object of any query string parameters passed in the HTTP Request
  • +
  • POST - payload contains the body of the HTTP Request
  • +
  • PUT - payload contains the body of the HTTP Request
  • +
  • DELETE - payload contains the body of the HTTP Request
  • +
  • PATCH - payload contains the body of the HTTP Request
  • +
  • HEAD - payload the HEAD method has no payload
  • +
  • OPTIONS - payload contains the body of the HTTP Request
  • +
  • TRACE - payload contains the details of the HTTP Request in the body
  • +
+
reqobject
An HTTP request object. This object contains multiple properties that provide information about the request.