Merge 3a29330021d3f11cb3df403168783deff3c179af into bb01f26f068b8e083e35fdf19dc9b36f8cf67068

This commit is contained in:
Debadutta Panda 2025-02-14 13:50:17 +05:30 committed by GitHub
commit 13c3711c80
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 171 additions and 36 deletions

View File

@ -25,11 +25,22 @@
<option value="patch">PATCH</option>
</select>
</div>
<div class="form-row form-row-http-in-upload hide">
<div id="form-reqBody-http-in-controller" class="form-row hide" style="display: flex;">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-upload" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-upload" style="width: 70%;" data-i18n="httpin.label.upload"></label>
<div style="display: flex; margin-left: 5px; flex-direction: column-reverse; gap:35%;">
<div id="form-row-http-in-upload" class="hide" style="display: flex;">
<input type="checkbox" id="node-input-upload" style="margin-right: 5%; margin-bottom: 5%;">
<label for="node-input-upload" style="text-wrap: nowrap;" data-i18n="httpin.label.upload"></label>
</div>
<div id="form-row-http-in-rawdata" class="hide" style="display: flex;">
<input type="checkbox" id="node-input-includeRawBody" style="margin-right: 5%; margin-bottom: 5%;">
<label for="node-input-includeRawBody" style="text-wrap: nowrap;" data-i18n="httpin.label.rawBody"></label>
</div>
</div>
</div>
<div class="form-row">
<label for="node-input-url"><i class="fa fa-globe"></i> <span data-i18n="httpin.label.url"></span></label>
<input id="node-input-url" type="text" placeholder="/url">
@ -74,6 +85,7 @@
label:RED._("node-red:httpin.label.url")},
method: {value:"get",required:true},
upload: {value:false},
includeRawBody: {value:false},
swaggerDoc: {type:"swagger-doc", required:false}
},
inputs:0,
@ -115,16 +127,22 @@
$('.row-swagger-doc').hide();
}
$("#node-input-method").on("change", function() {
if ($(this).val() === "post") {
$(".form-row-http-in-upload").show();
var method = $(this).val();
if(["post", "put", "patch","delete"].includes(method)){
$("#form-reqBody-http-in-controller").show();
$("#form-row-http-in-rawdata").show();
if (method === "post") {
$("#form-row-http-in-upload").show();
} else {
$("#form-row-http-in-upload").hide();
}
} else {
$(".form-row-http-in-upload").hide();
$("#form-row-http-in-rawdata").hide();
$("#form-row-http-in-upload").hide();
$("#form-reqBody-http-in-controller").hide();
}
}).change();
}
});
var headerTypes = [
{value:"content-type",label:"Content-Type",hasValue: false},

View File

@ -16,6 +16,7 @@
module.exports = function(RED) {
"use strict";
var rootApp;
var bodyParser = require("body-parser");
var multer = require("multer");
var cookieParser = require("cookie-parser");
@ -26,16 +27,21 @@ module.exports = function(RED) {
var mediaTyper = require('media-typer');
var isUtf8 = require('is-utf8');
var hashSum = require("hash-sum");
var PassThrough = require('stream').PassThrough;
var rawDataRoutes = new Set();
function rawBodyParser(req, res, next) {
if (req.skipRawBodyParser) { next(); } // don't parse this if told to skip
if (req._body) { return next(); }
req.body = "";
req._body = true;
var isText = true;
var checkUTF = false;
/**
* This middleware parses the raw body if the user enables it.
* @param {import('express').Request} req
* @param {import('express').Response} _res
* @param {import('express').NextFunction} next
* @returns
*/
function rawBodyParser(req, _res, next) {
if (!req._nodeRedReqStream) {
return next();
}
var isText = true, checkUTF = false;
if (req.headers['content-type']) {
var contentType = typer.parse(req.headers['content-type'])
if (contentType.type) {
@ -51,27 +57,115 @@ module.exports = function(RED) {
&& (parsedType.subtype !== "x-protobuf")) {
checkUTF = true;
} else {
// application/octet-stream or application/cbor
isText = false;
}
}
}
getBody(req, {
getBody(req._nodeRedReqStream, {
length: req.headers['content-length'],
encoding: isText ? "utf8" : null
}, function (err, buf) {
if (err) { return next(err); }
if (err) {
return next(err);
}
if (!isText && checkUTF && isUtf8(buf)) {
buf = buf.toString()
}
req.body = buf;
next();
Object.defineProperty(req, "rawRequestBody", {
value: buf,
enumerable: true
});
return next();
});
}
var corsSetup = false;
/**
* This method retrieves the root app by traversing the parent hierarchy.
* @param {import('express').Application} app
* @returns {import('express').Application}
*/
function getRootApp(app) {
if (typeof app.parent === 'undefined') {
return app;
}
return getRootApp(app.parent);
}
/**
* It provide the unique route key
* @param {{method: string, url: string}} obj
* @returns
*/
function getRouteKey(obj) {
var method = obj.method.toUpperCase();
// Normalize the URL by replacing double slashes with a single slash and removing the trailing slash
var url = obj.url.replace(/\/{2,}/g, '/').replace(/\/$/, '');
return `${method}:${url}`;
}
/**
* This middleware is for clone the request stream
* @param {import('express').Request} req
* @param {import('express').Response} _res
* @param {import('express').NextFunction} next
* @returns
*/
function setupRawBodyCapture(req, _res, next) {
var routeKey = getRouteKey({ method: req.method, url: req._parsedUrl.pathname });
// Check if routeKey exist in rawDataRoutes
if (rawDataRoutes.has(routeKey)) {
// Create a PassThrough stream to capture the request body
var cloneStream = new PassThrough();
// Function to handle 'data' event
function onData(chunk) {
// Safely call clone stream write
if (cloneStream.writable) {
cloneStream.write(chunk);
}
}
// Function to handle 'end' or 'error' events
function onEnd(err) {
if (err) {
// Safely call clone stream destroy method
if (!cloneStream.destroyed) {
cloneStream.destroy(err);
}
} else {
// Safely call clone stream end method
if (cloneStream.writable) {
cloneStream.end();
}
}
}
// Attach event listeners to the request stream
req.on('data', onData)
.once('end', onEnd)
.once('error', onEnd)
// Remove listeners once the request is closed
.once('close', () => {
req.removeListener('data', onData);
});
// Attach the clone stream to the request
Object.defineProperty(req, "_nodeRedReqStream", {
value: cloneStream
});
}
// Proceed to the next middleware
return next();
}
if(typeof RED.httpNode === 'function' && (rootApp = getRootApp(RED.httpNode))) {
// Add middleware to the stack
rootApp.use(setupRawBodyCapture);
// Move the middleware to top of the stack
rootApp._router.stack.unshift(rootApp._router.stack.pop());
}
function createRequestWrapper(node,req) {
// This misses a bunch of properties (eg headers). Before we use this function
@ -125,6 +219,7 @@ module.exports = function(RED) {
return wrapper;
}
function createResponseWrapper(node,res) {
var wrapper = {
_res: res
@ -188,9 +283,16 @@ module.exports = function(RED) {
}
this.method = n.method;
this.upload = n.upload;
this.includeRawBody = n.includeRawBody;
this.swaggerDoc = n.swaggerDoc;
var node = this;
var routeKey = getRouteKey({method: this.method, url: RED.httpNode.path() + this.url});
// If the user enables raw body, add it to the raw data routes.
if(this.includeRawBody) {
rawDataRoutes.add(routeKey);
}
this.errorHandler = function(err,req,res,next) {
node.warn(err);
@ -227,7 +329,9 @@ module.exports = function(RED) {
}
var maxApiRequestSize = RED.settings.apiMaxLength || '5mb';
var jsonParser = bodyParser.json({limit:maxApiRequestSize});
var urlencParser = bodyParser.urlencoded({limit:maxApiRequestSize,extended:true});
var metricsHandler = function(req,res,next) { next(); }
@ -254,25 +358,27 @@ module.exports = function(RED) {
var mp = multer({ storage: multer.memoryStorage() }).any();
multipartParser = function(req,res,next) {
mp(req,res,function(err) {
req._body = true;
next(err);
})
};
}
if (this.method == "get") {
RED.httpNode.get(this.url,cookieParser(),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,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,multipartParser,rawBodyParser,this.callback,this.errorHandler);
RED.httpNode.post(this.url,cookieParser(), httpMiddleware, corsHandler, metricsHandler, jsonParser, urlencParser, multipartParser, rawBodyParser, this.callback, this.errorHandler);
} else if (this.method == "put") {
RED.httpNode.put(this.url,cookieParser(),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 == "patch") {
RED.httpNode.patch(this.url,cookieParser(),httpMiddleware,corsHandler,metricsHandler,jsonParser,urlencParser,rawBodyParser,this.callback,this.errorHandler);
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);
RED.httpNode.delete(this.url, cookieParser(), httpMiddleware, corsHandler, metricsHandler, jsonParser, urlencParser, rawBodyParser, this.callback, this.errorHandler);
}
this.on("close",function() {
// Remove it from the raw data routes if the node is closed
rawDataRoutes.delete(routeKey);
var node = this;
RED.httpNode._router.stack.forEach(function(route,i,routes) {
if (route.route && route.route.path === node.url && route.route.methods[node.method]) {
@ -284,9 +390,9 @@ module.exports = function(RED) {
this.warn(RED._("httpin.errors.not-created"));
}
}
RED.nodes.registerType("http in",HTTPIn);
function HTTPOut(n) {
RED.nodes.createNode(this,n);
var node = this;
@ -361,5 +467,6 @@ module.exports = function(RED) {
done();
});
}
RED.nodes.registerType("http response",HTTPOut);
}

View File

@ -451,6 +451,7 @@
"upload": "Dateiuploads akzeptieren",
"status": "Statuscode",
"headers": "Kopfzeilen",
"rawBody": "Rohdaten einbeziehen?",
"other": "andere",
"paytoqs": {
"ignore": "Ignorieren",

View File

@ -515,6 +515,7 @@
"doc": "Docs",
"return": "Return",
"upload": "Accept file uploads?",
"rawBody": "Include Raw Data?",
"status": "Status code",
"headers": "Headers",
"other": "other",

View File

@ -518,6 +518,7 @@
"status": "Status code",
"headers": "Headers",
"other": "otro",
"rawBody": "¿Incluir datos sin procesar?",
"paytoqs": {
"ignore": "Ignore",
"query": "Append to query-string parameters",

View File

@ -518,6 +518,7 @@
"status": "Code d'état",
"headers": "En-têtes",
"other": "Autre",
"rawBody": "Inclure les données brutes ?",
"paytoqs": {
"ignore": "Ignorer",
"query": "Joindre aux paramètres de chaîne de requête",

View File

@ -518,6 +518,7 @@
"status": "ステータスコード",
"headers": "ヘッダ",
"other": "その他",
"rawBody": "生データを含める?",
"paytoqs": {
"ignore": "無視",
"query": "クエリパラメータに追加",

View File

@ -397,7 +397,8 @@
"binaryBuffer": "바이너리 버퍼",
"jsonObject": "JSON오브젝트",
"authType": "종류별",
"bearerToken": "토큰"
"bearerToken": "토큰",
"rawBody": "원시 데이터를 포함할까요?"
},
"setby": "- msg.method에 정의 -",
"basicauth": "인증을 사용",

View File

@ -506,6 +506,7 @@
"status": "Código de estado",
"headers": "Cabeçalhos",
"other": "outro",
"rawBody": "Incluir dados brutos?",
"paytoqs" : {
"ignore": "Ignorar",
"query": "Anexar aos parâmetros da cadeia de caracteres de consulta",

View File

@ -411,6 +411,7 @@
"status": "Код состояния",
"headers": "Заголовки",
"other": "другое",
"rawBody": "Включить необработанные данные?",
"paytoqs": {
"ignore": "Игнорировать",
"query": "Добавлять к параметрам строки запроса",

View File

@ -508,6 +508,7 @@
"status": "状态码",
"headers": "头",
"other": "其他",
"rawBody": "包含原始数据?",
"paytoqs": {
"ignore": "忽略",
"query": "附加到查询字符串参数",

View File

@ -416,7 +416,8 @@
"binaryBuffer": "二進制buffer",
"jsonObject": "解析的JSON對象",
"authType": "類型",
"bearerToken": "Token"
"bearerToken": "Token",
"rawBody": "包含原始數據?"
},
"setby": "- 用 msg.method 設定 -",
"basicauth": "基本認證",

View File

@ -65,6 +65,7 @@ var knownOpts = {
"version": Boolean,
"define": [String, Array]
};
var shortHands = {
"?":["--help"],
"p":["--port"],
@ -286,7 +287,6 @@ httpsPromise.then(function(startupHttps) {
server = http.createServer(function(req,res) {app(req,res);});
}
server.setMaxListeners(0);
function formatRoot(root) {
if (root[0] != "/") {
root = "/" + root;