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

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

@ -81,7 +81,33 @@
<li><code>statusCode</code>, if set, is used as the response status code (default: 200)</li> <li><code>statusCode</code>, if set, is used as the response status code (default: 200)</li>
<li><code>headers</code>, if set, should be an object containing field/value <li><code>headers</code>, if set, should be an object containing field/value
pairs to be added as response headers.</li> pairs to be added as response headers.</li>
<li><code>cookies</code>, if set, can be used to set or delete cookies.
</ul> </ul>
<h3>Cookie handling</h3>
<p>The <code>cookies</code> property must be an object of name/value pairs.
The value can be either a string to set the value of the cookie with default
options, or it can be an object of options.<p>
<p>The following example sets two cookies - one called <code>name</code> with
a value of <code>nick</code>, the other called <code>session</code> with a
value of <code>1234</code> and an expiry set to 15 minutes.</p>
<pre>
msg.cookies = {
name: 'nick',
session: {
value: '1234',
maxAge: 900000
}
}</pre>
<p>The valid options include:</p>
<ul>
<li><code>domain</code> - (String) domain name for the cookie</li>
<li><code>expires</code> - (Date) expiry date in GMT. If not specified or set to 0, creates a session cookie</li>
<li><code>maxAge</code> - (String) expiry date as relative to the current time in milliseconds</li>
<li><code>path</code> - (String) path for the cookie. Defaults to /</li>
<li><code>value</code> - (String) the value to use for the cookie</li>
</ul>
<p>To delete a cookie, set its <code>value</code> to <code>null</code>.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">

View File

@ -17,6 +17,7 @@
module.exports = function(RED) { module.exports = function(RED) {
"use strict"; "use strict";
var bodyParser = require("body-parser"); var bodyParser = require("body-parser");
var cookieParser = require("cookie-parser");
var getBody = require('raw-body'); var getBody = require('raw-body');
var cors = require('cors'); var cors = require('cors');
var jsonParser = bodyParser.json(); var jsonParser = bodyParser.json();
@ -223,13 +224,13 @@ module.exports = function(RED) {
} }
if (this.method == "get") { 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") { } 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") { } 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") { } 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() { this.on("close",function() {
@ -255,6 +256,23 @@ module.exports = function(RED) {
if (msg.headers) { if (msg.headers) {
msg.res._res.set(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; var statusCode = msg.statusCode || 200;
if (typeof msg.payload == "object" && !Buffer.isBuffer(msg.payload)) { if (typeof msg.payload == "object" && !Buffer.isBuffer(msg.payload)) {
msg.res._res.status(statusCode).jsonp(msg.payload); msg.res._res.status(statusCode).jsonp(msg.payload);

View File

@ -30,6 +30,7 @@
"body-parser": "1.15.0", "body-parser": "1.15.0",
"cheerio":"0.19.0", "cheerio":"0.19.0",
"clone": "1.0.2", "clone": "1.0.2",
"cookie-parser": "1.4.3",
"cors":"2.7.1", "cors":"2.7.1",
"cron":"1.1.0", "cron":"1.1.0",
"express": "4.13.4", "express": "4.13.4",