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

change http request to use http_proxy environment rather than do it ourselves

This commit is contained in:
dceejay 2015-05-19 09:45:00 +01:00
parent e852d1e57c
commit 38ab1550d2
3 changed files with 35 additions and 29 deletions

View File

@ -152,7 +152,7 @@
<li><code>statusCode</code> is the status code of the response, or the error code if the request could not be completed</li> <li><code>statusCode</code> is the status code of the response, or the error code if the request could not be completed</li>
<li><code>headers</code> is an object containing the response headers</li> <li><code>headers</code> is an object containing the response headers</li>
</ul> </ul>
<p><b>Note</b>: See the <i>settings.js</i> file if you need to configure a proxy.</p> <p><b>Note</b>: If you need to configure a proxy please add <b>http_proxy=...</b> to your environment variables and restart Node-RED.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">

View File

@ -248,19 +248,25 @@ module.exports = function(RED) {
} }
} }
if (opts.headers['content-length'] == null) { if (opts.headers['content-length'] == null) {
opts.headers['content-length'] = Buffer.byteLength(payload); if (Buffer.isBuffer(payload)) {
opts.headers['content-length'] = payload.length;
} else {
opts.headers['content-length'] = Buffer.byteLength(payload);
}
} }
} }
var urltotest = url; var urltotest = url;
if (RED.settings.httpNodeProxy) { if (process.env.http_proxy != null) {
var proxy = RED.settings.httpNodeProxy.host; var match = process.env.http_proxy.match(/^(http:\/\/)?([^:\/]+)(:([0-9]+))?/i);
opts.protocol = "http:"; if (match) {
opts.headers['Host'] = opts.host; opts.protocol = "http:";
opts.host = opts.hostname = proxy; opts.headers['Host'] = opts.host;
opts.port = RED.settings.httpNodeProxy.port || opts.port; opts.host = opts.hostname = match[2],
if (opts.port) { opts.host = opts.host+":"+opts.port; } opts.port = (match[4] != null ? match[4] : 80),
opts.path = opts.pathname = opts.href; opts.path = opts.pathname = opts.href;
urltotest = proxy; urltotest = match[2];
}
else { node.warn("Bad proxy url: "+process.env.http_proxy); }
} }
var req = ((/^https/.test(urltotest))?https:http).request(opts,function(res) { var req = ((/^https/.test(urltotest))?https:http).request(opts,function(res) {
(node.ret === "bin") ? res.setEncoding('binary') : res.setEncoding('utf8'); (node.ret === "bin") ? res.setEncoding('binary') : res.setEncoding('utf8');

View File

@ -18,7 +18,6 @@
// to make it available: // to make it available:
//var fs = require("fs"); //var fs = require("fs");
module.exports = { module.exports = {
// the tcp port that the Node-RED web server is listening on // the tcp port that the Node-RED web server is listening on
uiPort: 1880, uiPort: 1880,
@ -50,7 +49,7 @@ module.exports = {
// To enabled pretty-printing of the flow within the flow file, set the following // To enabled pretty-printing of the flow within the flow file, set the following
// property to true: // property to true:
//flowFilePretty: true, //flowFilePretty: true,
// By default, all user data is stored in the Node-RED install directory. To // By default, all user data is stored in the Node-RED install directory. To
// use a different location, the following property can be used // use a different location, the following property can be used
//userDir: '/home/nol/.node-red/', //userDir: '/home/nol/.node-red/',
@ -73,12 +72,12 @@ module.exports = {
// can be used to specifiy a different root path. If set to false, this is // can be used to specifiy a different root path. If set to false, this is
// disabled. // disabled.
//httpNodeRoot: '/nodes', //httpNodeRoot: '/nodes',
// To password protect the node-defined HTTP endpoints, the following property // To password protect the node-defined HTTP endpoints, the following property
// can be used. // can be used.
// The password must be an md5 hash eg.. 5f4dcc3b5aa765d61d8327deb882cf99 ('password') // The password must be an md5 hash eg.. 5f4dcc3b5aa765d61d8327deb882cf99 ('password')
//httpNodeAuth: {user:"user",pass:"5f4dcc3b5aa765d61d8327deb882cf99"}, //httpNodeAuth: {user:"user",pass:"5f4dcc3b5aa765d61d8327deb882cf99"},
// When httpAdminRoot is used to move the UI to a different root path, the // When httpAdminRoot is used to move the UI to a different root path, the
// following property can be used to identify a directory of static content // following property can be used to identify a directory of static content
// that should be served at http://localhost:1880/. // that should be served at http://localhost:1880/.
@ -87,26 +86,26 @@ module.exports = {
// To password protect the static content, the following property can be used. // To password protect the static content, the following property can be used.
// The password must be an md5 hash eg.. 5f4dcc3b5aa765d61d8327deb882cf99 ('password') // The password must be an md5 hash eg.. 5f4dcc3b5aa765d61d8327deb882cf99 ('password')
//httpStaticAuth: {user:"user",pass:"5f4dcc3b5aa765d61d8327deb882cf99"}, //httpStaticAuth: {user:"user",pass:"5f4dcc3b5aa765d61d8327deb882cf99"},
// The following property can be used in place of 'httpAdminRoot' and 'httpNodeRoot', // The following property can be used in place of 'httpAdminRoot' and 'httpNodeRoot',
// to apply the same root to both parts. // to apply the same root to both parts.
//httpRoot: '/red', //httpRoot: '/red',
// The following property can be used in place of 'httpAdminAuth' and 'httpNodeAuth', // The following property can be used in place of 'httpAdminAuth' and 'httpNodeAuth',
// to apply the same authentication to both parts. // to apply the same authentication to both parts.
//httpAuth: {user:"user",pass:"5f4dcc3b5aa765d61d8327deb882cf99"}, //httpAuth: {user:"user",pass:"5f4dcc3b5aa765d61d8327deb882cf99"},
// The following property can be used to disable the editor. The admin API // The following property can be used to disable the editor. The admin API
// is not affected by this option. To disable both the editor and the admin // is not affected by this option. To disable both the editor and the admin
// API, use either the httpRoot or httpAdminRoot properties // API, use either the httpRoot or httpAdminRoot properties
//disableEditor: false, //disableEditor: false,
// The following property can be used to enable HTTPS // The following property can be used to enable HTTPS
// See http://nodejs.org/api/https.html#https_https_createserver_options_requestlistener // See http://nodejs.org/api/https.html#https_https_createserver_options_requestlistener
// for details on its contents. // for details on its contents.
// See the comment at the top of this file on how to load the `fs` module used by // See the comment at the top of this file on how to load the `fs` module used by
// this setting. // this setting.
// //
//https: { //https: {
// key: fs.readFileSync('privatekey.pem'), // key: fs.readFileSync('privatekey.pem'),
// cert: fs.readFileSync('certificate.pem') // cert: fs.readFileSync('certificate.pem')
@ -121,9 +120,11 @@ module.exports = {
// methods: "GET,PUT,POST,DELETE" // methods: "GET,PUT,POST,DELETE"
//}, //},
// The following property can be used to configure a proxy for use by the // If you need to set an http proxy please set an environment variable
// http request node. // called http_proxy outside of Node-RED in the operating system.
//httpNodeProxy : { host:"myproxy.acme.com", port:8080 }, // For example - http_proxy=http://myproxy.com:8080
// (Setting it here will have no effect)
// The following property can be used to add a custom middleware function // The following property can be used to add a custom middleware function
// in front of all http in nodes. This allows custom authentication to be // in front of all http in nodes. This allows custom authentication to be
@ -133,7 +134,7 @@ module.exports = {
// // by calling next(); // // by calling next();
// next(); // next();
//}, //},
// Anything in this hash is globally available to all functions. // Anything in this hash is globally available to all functions.
// It is accessed as context.global. // It is accessed as context.global.
// eg: // eg:
@ -147,8 +148,7 @@ module.exports = {
// jfive:require("johnny-five"), // jfive:require("johnny-five"),
// j5board:require("johnny-five").Board({repl:false}) // j5board:require("johnny-five").Board({repl:false})
}, },
// The following property can be used to order the categories in the editor // The following property can be used to order the categories in the editor
// palette. If a node's category is not in the list, the category will get // palette. If a node's category is not in the list, the category will get
// added to the end of the palette. // added to the end of the palette.
@ -159,15 +159,15 @@ module.exports = {
logging: { logging: {
// Only console logging is currently supported // Only console logging is currently supported
console: { console: {
// Level of logging to be recorded. Options are: // Level of logging to be recorded. Options are:
// fatal - only those errors which make the application unusable should be recorded // fatal - only those errors which make the application unusable should be recorded
// error - record errors which are deemed fatal for a particular request + fatal errors // error - record errors which are deemed fatal for a particular request + fatal errors
// warn - record problems which are non fatal + errors + fatal errors // warn - record problems which are non fatal + errors + fatal errors
// info - record information about the general running of the application + warn + error + fatal errors // info - record information about the general running of the application + warn + error + fatal errors
// debug - record information which is more verbose than info + info + warn + error + fatal errors // debug - record information which is more verbose than info + info + warn + error + fatal errors
// trace - record very detailed logging + debug + info + warn + error + fatal errors // trace - record very detailed logging + debug + info + warn + error + fatal errors
level: "info", level: "info",
// Whether or not to include metric events in the log output // Whether or not to include metric events in the log output
metrics: false metrics: false
} }