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>headers</code> is an object containing the response headers</li>
</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 type="text/javascript">

View File

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

View File

@ -18,7 +18,6 @@
// to make it available:
//var fs = require("fs");
module.exports = {
// the tcp port that the Node-RED web server is listening on
uiPort: 1880,
@ -50,7 +49,7 @@ module.exports = {
// To enabled pretty-printing of the flow within the flow file, set the following
// property to true:
//flowFilePretty: true,
// By default, all user data is stored in the Node-RED install directory. To
// use a different location, the following property can be used
//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
// disabled.
//httpNodeRoot: '/nodes',
// To password protect the node-defined HTTP endpoints, the following property
// can be used.
// The password must be an md5 hash eg.. 5f4dcc3b5aa765d61d8327deb882cf99 ('password')
//httpNodeAuth: {user:"user",pass:"5f4dcc3b5aa765d61d8327deb882cf99"},
// 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
// 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.
// The password must be an md5 hash eg.. 5f4dcc3b5aa765d61d8327deb882cf99 ('password')
//httpStaticAuth: {user:"user",pass:"5f4dcc3b5aa765d61d8327deb882cf99"},
// The following property can be used in place of 'httpAdminRoot' and 'httpNodeRoot',
// to apply the same root to both parts.
//httpRoot: '/red',
// The following property can be used in place of 'httpAdminAuth' and 'httpNodeAuth',
// to apply the same authentication to both parts.
//httpAuth: {user:"user",pass:"5f4dcc3b5aa765d61d8327deb882cf99"},
// 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
// API, use either the httpRoot or httpAdminRoot properties
//disableEditor: false,
// The following property can be used to enable HTTPS
// See http://nodejs.org/api/https.html#https_https_createserver_options_requestlistener
// for details on its contents.
// See the comment at the top of this file on how to load the `fs` module used by
// this setting.
//
//
//https: {
// key: fs.readFileSync('privatekey.pem'),
// cert: fs.readFileSync('certificate.pem')
@ -121,9 +120,11 @@ module.exports = {
// methods: "GET,PUT,POST,DELETE"
//},
// The following property can be used to configure a proxy for use by the
// http request node.
//httpNodeProxy : { host:"myproxy.acme.com", port:8080 },
// If you need to set an http proxy please set an environment variable
// called http_proxy outside of Node-RED in the operating system.
// 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
// in front of all http in nodes. This allows custom authentication to be
@ -133,7 +134,7 @@ module.exports = {
// // by calling next();
// next();
//},
// Anything in this hash is globally available to all functions.
// It is accessed as context.global.
// eg:
@ -147,8 +148,7 @@ module.exports = {
// jfive:require("johnny-five"),
// j5board:require("johnny-five").Board({repl:false})
},
// 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
// added to the end of the palette.
@ -159,15 +159,15 @@ module.exports = {
logging: {
// Only console logging is currently supported
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
// 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
// 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
// trace - record very detailed logging + debug + info + warn + error + fatal errors
level: "info",
// Whether or not to include metric events in the log output
metrics: false
}