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');