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

Move to request module

This commit is contained in:
HirokiUchikawa 2018-05-23 17:16:20 +09:00
parent 61f6535be8
commit 9fd5d1db56
3 changed files with 63 additions and 93 deletions

View File

@ -16,9 +16,7 @@
module.exports = function(RED) { module.exports = function(RED) {
"use strict"; "use strict";
var http = require("follow-redirects").http; var request = require("request");
var https = require("follow-redirects").https;
var urllib = require("url");
var mustache = require("mustache"); var mustache = require("mustache");
var querystring = require("querystring"); var querystring = require("querystring");
var cookie = require("cookie"); var cookie = require("cookie");
@ -78,9 +76,13 @@ module.exports = function(RED) {
if (msg.method && n.method && (n.method === "use")) { if (msg.method && n.method && (n.method === "use")) {
method = msg.method.toUpperCase(); // use the msg parameter method = msg.method.toUpperCase(); // use the msg parameter
} }
var opts = urllib.parse(url); var opts = {};
opts.url = url;
opts.timeout = node.reqTimeout;
opts.method = method; opts.method = method;
opts.headers = {}; opts.headers = {};
opts.encoding = null; // Force NodeJs to return a Buffer (instead of a string)
opts.maxRedirects = 21;
var ctSet = "Content-Type"; // set default camel case var ctSet = "Content-Type"; // set default camel case
var clSet = "Content-Length"; var clSet = "Content-Length";
if (msg.headers) { if (msg.headers) {
@ -109,7 +111,7 @@ module.exports = function(RED) {
} }
} }
if (msg.hasOwnProperty('followRedirects')) { if (msg.hasOwnProperty('followRedirects')) {
opts.followRedirects = msg.followRedirects; opts.followRedirect = msg.followRedirects;
} }
if (msg.cookies) { if (msg.cookies) {
var cookies = []; var cookies = [];
@ -134,7 +136,10 @@ module.exports = function(RED) {
} }
} }
if (this.credentials && this.credentials.user) { if (this.credentials && this.credentials.user) {
opts.auth = this.credentials.user+":"+(this.credentials.password||""); opts.auth = {
user: this.credentials.user,
pass: this.credentials.password||""
};
} }
var payload = null; var payload = null;
@ -160,6 +165,7 @@ module.exports = function(RED) {
opts.headers[clSet] = Buffer.byteLength(payload); opts.headers[clSet] = Buffer.byteLength(payload);
} }
} }
opts.body = payload;
} }
// revert to user supplied Capitalisation if needed. // revert to user supplied Capitalisation if needed.
if (opts.headers.hasOwnProperty('content-type') && (ctSet !== 'content-type')) { if (opts.headers.hasOwnProperty('content-type') && (ctSet !== 'content-type')) {
@ -170,7 +176,6 @@ module.exports = function(RED) {
opts.headers[clSet] = opts.headers['content-length']; opts.headers[clSet] = opts.headers['content-length'];
delete opts.headers['content-length']; delete opts.headers['content-length'];
} }
var urltotest = url;
var noproxy; var noproxy;
if (noprox) { if (noprox) {
for (var i in noprox) { for (var i in noprox) {
@ -180,23 +185,12 @@ module.exports = function(RED) {
if (prox && !noproxy) { if (prox && !noproxy) {
var match = prox.match(/^(http:\/\/)?(.+)?:([0-9]+)?/i); var match = prox.match(/^(http:\/\/)?(.+)?:([0-9]+)?/i);
if (match) { if (match) {
//opts.protocol = "http:"; opts.proxy = prox;
//opts.host = opts.hostname = match[2]; } else {
//opts.port = (match[3] != null ? match[3] : 80); node.warn("Bad proxy url: "+ prox);
opts.headers['Host'] = opts.host; opts.proxy = null;
var heads = opts.headers;
var path = opts.pathname = opts.href;
opts = urllib.parse(prox);
opts.path = opts.pathname = path;
opts.headers = heads;
opts.method = method;
urltotest = match[0];
if (opts.auth) {
opts.headers['Proxy-Authorization'] = "Basic "+new Buffer(opts.auth).toString('Base64')
} }
} }
else { node.warn("Bad proxy url: "+process.env.http_proxy); }
}
if (tlsNode) { if (tlsNode) {
tlsNode.addTLSOptions(opts); tlsNode.addTLSOptions(opts);
} else { } else {
@ -204,16 +198,23 @@ module.exports = function(RED) {
opts.rejectUnauthorized = msg.rejectUnauthorized; opts.rejectUnauthorized = msg.rejectUnauthorized;
} }
} }
var req = ((/^https/.test(urltotest))?https:http).request(opts,function(res) { request(opts, function(err, res, body) {
// Force NodeJs to return a Buffer (instead of a string) if(err){
// See https://github.com/nodejs/node/issues/6038 if(err.code === 'ETIMEDOUT' || err.code === 'ESOCKETTIMEDOUT') {
res.setEncoding(null); node.error(RED._("common.notification.errors.no-response"), msg);
delete res._readableState.decoder; node.status({fill:"red", shape:"ring", text:"common.notification.errors.no-response"});
}else{
node.error(err,msg);
node.status({fill:"red", shape:"ring", text:err.code});
}
msg.payload = err.toString() + " : " + url;
msg.statusCode = err.code;
node.send(msg);
}else{
msg.statusCode = res.statusCode; msg.statusCode = res.statusCode;
msg.headers = res.headers; msg.headers = res.headers;
msg.responseUrl = res.responseUrl; msg.responseUrl = res.request.uri.href;
msg.payload = []; msg.payload = body;
if (msg.headers.hasOwnProperty('set-cookie')) { if (msg.headers.hasOwnProperty('set-cookie')) {
msg.responseCookies = {}; msg.responseCookies = {};
@ -224,22 +225,10 @@ module.exports = function(RED) {
parsedCookie.value = parsedCookie[key]; parsedCookie.value = parsedCookie[key];
delete parsedCookie[key]; delete parsedCookie[key];
msg.responseCookies[key] = parsedCookie; msg.responseCookies[key] = parsedCookie;
});
})
} }
msg.headers['x-node-red-request-node'] = hashSum(msg.headers); msg.headers['x-node-red-request-node'] = hashSum(msg.headers);
// msg.url = url; // revert when warning above finally removed // msg.url = url; // revert when warning above finally removed
res.on('data',function(chunk) {
if (!Buffer.isBuffer(chunk)) {
// if the 'setEncoding(null)' fix above stops working in
// a new Node.js release, throw a noisy error so we know
// about it.
throw new Error("HTTP Request data chunk not a Buffer");
}
msg.payload.push(chunk);
});
res.on('end',function() {
if (node.metric()) { if (node.metric()) {
// Calculate request time // Calculate request time
var diff = process.hrtime(preRequestTimestamp); var diff = process.hrtime(preRequestTimestamp);
@ -251,13 +240,7 @@ module.exports = function(RED) {
} }
} }
// Check that msg.payload is an array - if the req error
// handler has been called, it will have been set to a string
// and the error already handled - so no further action should
// be taken. #1344
if (Array.isArray(msg.payload)) {
// Convert the payload to the required return type // Convert the payload to the required return type
msg.payload = Buffer.concat(msg.payload); // bin
if (node.ret !== "bin") { if (node.ret !== "bin") {
msg.payload = msg.payload.toString('utf8'); // txt msg.payload = msg.payload.toString('utf8'); // txt
@ -271,25 +254,6 @@ module.exports = function(RED) {
} }
}); });
}); });
req.setTimeout(node.reqTimeout, function() {
node.error(RED._("common.notification.errors.no-response"),msg);
setTimeout(function() {
node.status({fill:"red",shape:"ring",text:"common.notification.errors.no-response"});
},10);
req.abort();
});
req.on('error',function(err) {
node.error(err,msg);
msg.payload = err.toString() + " : " + url;
msg.statusCode = err.code;
node.status({fill:"red",shape:"ring",text:err.code});
node.send(msg);
});
if (payload) {
req.write(payload);
}
req.end();
});
this.on("close",function() { this.on("close",function() {
node.status({}); node.status({});

View File

@ -44,7 +44,6 @@
"cron": "1.3.0", "cron": "1.3.0",
"express": "4.16.3", "express": "4.16.3",
"express-session": "1.15.6", "express-session": "1.15.6",
"follow-redirects": "1.4.1",
"fs-extra": "5.0.0", "fs-extra": "5.0.0",
"fs.notify": "0.0.4", "fs.notify": "0.0.4",
"hash-sum": "1.0.2", "hash-sum": "1.0.2",
@ -69,6 +68,7 @@
"passport-http-bearer": "1.0.1", "passport-http-bearer": "1.0.1",
"passport-oauth2-client-password": "0.1.2", "passport-oauth2-client-password": "0.1.2",
"raw-body": "2.3.3", "raw-body": "2.3.3",
"request": "2.87.0",
"semver": "5.5.0", "semver": "5.5.0",
"sentiment": "2.1.0", "sentiment": "2.1.0",
"uglify-js": "3.3.25", "uglify-js": "3.3.25",

View File

@ -825,7 +825,13 @@ describe('HTTP Request Node', function() {
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try { try {
msg.should.have.property('statusCode','ECONNRESET'); msg.should.have.property('statusCode','ESOCKETTIMEDOUT');
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == 'http request';
});
logEvents.should.have.length(1);
var tstmp = logEvents[0][0].timestamp;
logEvents[0][0].should.eql({level:helper.log().ERROR, id:'n1',type:'http request',msg:'common.notification.errors.no-response', timestamp:tstmp});
done(); done();
} catch(err) { } catch(err) {
done(err); done(err);