Skip to content

Add utils module and refactor should_proxy_to() based on PR #411. Fixes #383 #416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 13 additions & 98 deletions lib/needle.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ var fs = require('fs'),
auth = require('./auth'),
cookies = require('./cookies'),
parsers = require('./parsers'),
decoder = require('./decoder');
decoder = require('./decoder'),
utils = require('./utils');

//////////////////////////////////////////
// variabilia
Expand Down Expand Up @@ -127,99 +128,13 @@ Object.keys(aliased.options).map(function(k) {
//////////////////////////////////////////
// helpers

function get_env_var(keys, try_lower) {
var val, i = -1, env = process.env;
while (!val && i < keys.length-1) {
val = env[keys[++i]];
if (!val && try_lower) {
val = env[keys[i].toLowerCase()];
}
}
return val;
}

function keys_by_type(type) {
return Object.keys(defaults).map(function(el) {
if (defaults[el] !== null && defaults[el].constructor == type)
return el;
}).filter(function(el) { return el })
}

function parse_content_type(header) {
if (!header || header === '') return {};

var found, charset = 'utf8', arr = header.split(';');

if (arr.length > 1 && (found = arr[1].match(/charset=(.+)/)))
charset = found[1];

return { type: arr[0], charset: charset };
}

function is_stream(obj) {
return typeof obj.pipe === 'function';
}

function get_stream_length(stream, given_length, cb) {
if (given_length > 0)
return cb(given_length);

if (stream.end !== void 0 && stream.end !== Infinity && stream.start !== void 0)
return cb((stream.end + 1) - (stream.start || 0));

fs.stat(stream.path, function(err, stat) {
cb(stat ? stat.size - (stream.start || 0) : null);
});
}

function resolve_url(href, base) {
if (url.URL)
return new url.URL(href, base);

// older Node version (< v6.13)
return base ? url.resolve(base, href) : href;
}

function host_and_ports_match(url1, url2) {
if (url1.indexOf('http') < 0) url1 = 'http://' + url1;
if (url2.indexOf('http') < 0) url2 = 'http://' + url2;
var a = url.parse(url1), b = url.parse(url2);

return a.host == b.host
&& String(a.port || (a.protocol == 'https:' ? 443 : 80))
== String(b.port || (b.protocol == 'https:' ? 443 : 80));
}

// returns false if a no_proxy host matches given url
function should_proxy_to(url) {
var no_proxy = get_env_var(['NO_PROXY'], true);
if (!no_proxy) return true;

var host, hosts = no_proxy.split(',');
for (var i in hosts) {
host = hosts[i];
if (host_and_ports_match(host, url)) {
return false;
}
}

return true;
}

function pump_streams(streams, cb) {
if (stream.pipeline)
return stream.pipeline.apply(null, streams.concat(cb));

var tmp = streams.shift();
while (streams.length) {
tmp = tmp.pipe(streams.shift());
tmp.once('error', function(e) {
cb && cb(e);
cb = null;
})
}
}

//////////////////////////////////////////
// the main act

Expand Down Expand Up @@ -340,12 +255,12 @@ Needle.prototype.setup = function(uri, options) {
}
}

var env_proxy = get_env_var(['HTTP_PROXY', 'HTTPS_PROXY'], true);
var env_proxy = utils.get_env_var(['HTTP_PROXY', 'HTTPS_PROXY'], true);
if (!config.proxy && env_proxy) config.proxy = env_proxy;

// if proxy is present, set auth header from either url or proxy_user option.
if (config.proxy) {
if (should_proxy_to(uri)) {
if (utils.should_proxy_to(uri)) {
if (config.proxy.indexOf('http') === -1)
config.proxy = 'http://' + config.proxy;

Expand Down Expand Up @@ -402,7 +317,7 @@ Needle.prototype.start = function() {
next(parts);
});

} else if (is_stream(data)) {
} else if (utils.is_stream(data)) {

if (method == 'get')
throw new Error('Refusing to pipe() a stream via GET. Did you mean .post?');
Expand All @@ -411,7 +326,7 @@ Needle.prototype.start = function() {
// ok, let's get the stream's length and set it as the content-length header.
// this prevents some servers from cutting us off before all the data is sent.
waiting = true;
get_stream_length(data, config.stream_length, function(length) {
utils.get_stream_length(data, config.stream_length, function(length) {
data.length = length;
next(data);
})
Expand Down Expand Up @@ -609,7 +524,7 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,

// if follow_set_cookies is true, insert cookies in the next request's headers.
// we set both the original request cookies plus any response cookies we might have received.
if (config.follow_set_cookies && host_and_ports_match(headers.location, uri)) {
if (config.follow_set_cookies && utils.host_and_ports_match(headers.location, uri)) {
var request_cookies = cookies.read(config.headers['cookie']);
config.previous_resp_cookies = resp.cookies;
if (Object.keys(request_cookies).length || Object.keys(resp.cookies || {}).length) {
Expand All @@ -625,7 +540,7 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,

config.headers['host'] = null; // clear previous Host header to avoid conflicts.

var redirect_url = resolve_url(headers.location, uri);
var redirect_url = utils.resolve_url(headers.location, uri);
debug('Redirecting to ' + redirect_url.toString());
return self.send_request(++count, method, redirect_url.toString(), config, post_data, out, callback);
} else if (config.follow_max > 0) {
Expand All @@ -650,7 +565,7 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,
out.emit('headers', headers);

var pipeline = [],
mime = parse_content_type(headers['content-type']),
mime = utils.parse_content_type(headers['content-type']),
text_response = mime.type && (mime.type.indexOf('text/') != -1 || !!mime.type.match(/(\/|\+)(xml|json)$/));

// To start, if our body is compressed and we're able to inflate it, do it.
Expand Down Expand Up @@ -689,7 +604,7 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,
pipeline.push(out);

// Now, release the kraken!
pump_streams([resp].concat(pipeline), function(err) {
utils.pump_streams([resp].concat(pipeline), function(err) {
if (err) debug(err)

// on node v8.x, if an error ocurrs on the receiving end,
Expand Down Expand Up @@ -745,7 +660,7 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,
}
})

pump_streams([resp, clean_pipe], function(err) {
utils.pump_streams([resp, clean_pipe], function(err) {
if (err) debug(err);
});

Expand Down Expand Up @@ -836,8 +751,8 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,
})

if (post_data) {
if (is_stream(post_data)) {
pump_streams([post_data, request], function(err) {
if (utils.is_stream(post_data)) {
utils.pump_streams([post_data, request], function(err) {
if (err) debug(err);
});
} else {
Expand Down
111 changes: 111 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
var fs = require('fs'),
url = require('url'),
stream = require('stream');

function resolve_url(href, base) {
if (url.URL)
return new url.URL(href, base);

// older Node version (< v6.13)
return base ? url.resolve(base, href) : href;
}

function host_and_ports_match(url1, url2) {
if (url1.indexOf('http') < 0) url1 = 'http://' + url1;
if (url2.indexOf('http') < 0) url2 = 'http://' + url2;
var a = url.parse(url1), b = url.parse(url2);

return a.host == b.host
&& String(a.port || (a.protocol == 'https:' ? 443 : 80))
== String(b.port || (b.protocol == 'https:' ? 443 : 80));
}

// returns false if a no_proxy host or pattern matches given url
function should_proxy_to(uri) {
var no_proxy = get_env_var(['NO_PROXY'], true);
if (!no_proxy) return true;

// previous (naive, simple) strategy
// var host, hosts = no_proxy.split(',');
// for (var i in hosts) {
// host = hosts[i];
// if (host_and_ports_match(host, uri)) {
// return false;
// }
// }

var pattern, pattern_list = no_proxy.split(/[\s,]+/);
for (var i in pattern_list) {
pattern = pattern_list[i];
if (pattern.trim().length == 0) continue;

// replace leading dot by asterisk, escape dots and finally replace asterisk by .*
var regex = new RegExp(pattern.replace(/^\./, "*").replace(/[.]/g, '\\$&').replace(/\*/g, '.*'))
if (uri.match(regex)) return false;
}

return true;
}

function get_env_var(keys, try_lower) {
var val, i = -1, env = process.env;
while (!val && i < keys.length-1) {
val = env[keys[++i]];
if (!val && try_lower) {
val = env[keys[i].toLowerCase()];
}
}
return val;
}

function parse_content_type(header) {
if (!header || header === '') return {};

var found, charset = 'utf8', arr = header.split(';');

if (arr.length > 1 && (found = arr[1].match(/charset=(.+)/)))
charset = found[1];

return { type: arr[0], charset: charset };
}

function is_stream(obj) {
return typeof obj.pipe === 'function';
}

function get_stream_length(stream, given_length, cb) {
if (given_length > 0)
return cb(given_length);

if (stream.end !== void 0 && stream.end !== Infinity && stream.start !== void 0)
return cb((stream.end + 1) - (stream.start || 0));

fs.stat(stream.path, function(err, stat) {
cb(stat ? stat.size - (stream.start || 0) : null);
});
}

function pump_streams(streams, cb) {
if (stream.pipeline)
return stream.pipeline.apply(null, streams.concat(cb));

var tmp = streams.shift();
while (streams.length) {
tmp = tmp.pipe(streams.shift());
tmp.once('error', function(e) {
cb && cb(e);
cb = null;
})
}
}

module.exports = {
resolve_url: resolve_url,
get_env_var: get_env_var,
host_and_ports_match: host_and_ports_match,
should_proxy_to: should_proxy_to,
parse_content_type: parse_content_type,
is_stream: is_stream,
get_stream_length: get_stream_length,
pump_streams: pump_streams
}
4 changes: 2 additions & 2 deletions test/proxy_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ describe('proxy option', function() {
}))
})

it('proxies request if matching host in list but different port', function(done) {
it('does not proxy request if matching host in list and just has a different port', function(done) {
process.env.NO_PROXY = 'localhost';
send_request({ proxy: nonexisting_host + ':123/done' }, proxied(nonexisting_host, '123', function() {
send_request({ proxy: nonexisting_host + ':123/done' }, not_proxied(function() {
delete process.env.NO_PROXY;
done();
}))
Expand Down
Loading