Add cookie handling to HTTP In and HTTP Response nodes

This commit is contained in:
Nick O'Leary
2016-06-08 11:09:18 +01:00
parent ef2f71859c
commit 5f6a0141f0
3 changed files with 49 additions and 4 deletions

View File

@@ -17,6 +17,7 @@
module.exports = function(RED) {
"use strict";
var bodyParser = require("body-parser");
var cookieParser = require("cookie-parser");
var getBody = require('raw-body');
var cors = require('cors');
var jsonParser = bodyParser.json();
@@ -223,13 +224,13 @@ module.exports = function(RED) {
}
if (this.method == "get") {
RED.httpNode.get(this.url,httpMiddleware,corsHandler,metricsHandler,this.callback,this.errorHandler);
RED.httpNode.get(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,this.callback,this.errorHandler);
} else if (this.method == "post") {
RED.httpNode.post(this.url,httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler);
RED.httpNode.post(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler);
} else if (this.method == "put") {
RED.httpNode.put(this.url,httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler);
RED.httpNode.put(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler);
} else if (this.method == "delete") {
RED.httpNode.delete(this.url,httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler);
RED.httpNode.delete(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler);
}
this.on("close",function() {
@@ -255,6 +256,23 @@ module.exports = function(RED) {
if (msg.headers) {
msg.res._res.set(msg.headers);
}
if (msg.cookies) {
for (var name in msg.cookies) {
if (msg.cookies.hasOwnProperty(name)) {
if (msg.cookies[name] === null || msg.cookies[name].value === null) {
if (msg.cookies[name]!==null) {
msg.res._res.clearCookie('name',msg.cookies[name]);
} else {
msg.res._res.clearCookie('name');
}
} else if (typeof msg.cookies[name] === 'object') {
msg.res._res.cookie(name,msg.cookies[name].value,msg.cookies[name]);
} else {
msg.res._res.cookie(name,msg.cookies[name]);
}
}
}
}
var statusCode = msg.statusCode || 200;
if (typeof msg.payload == "object" && !Buffer.isBuffer(msg.payload)) {
msg.res._res.status(statusCode).jsonp(msg.payload);