Socket
Socket
Sign inDemoInstall

http-proxy-agent

Package Overview
Dependencies
2
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.0 to 0.2.0

.travis.yml

12

History.md

@@ -0,1 +1,13 @@

0.2.0 / 2013-09-16
==================
- http-proxy-agent: update to `agent-base` v1.0.0 API
- http-proxy-agent: rename `secure` option to `secureProxy`
- http-proxy-agent: default the "port" to 80 if not set
- http-proxy-agent: use "extend" module
- test: refactor tests
- test: add 407 auth test
- test: add bad proxy info test
- test: add "secureProxy" option tests
0.1.0 / 2013-09-03

@@ -2,0 +14,0 @@ ==================

61

http-proxy-agent.js

@@ -9,2 +9,3 @@

var url = require('url');
var extend = require('extend');
var Agent = require('agent-base');

@@ -30,10 +31,12 @@ var inherits = require('util').inherits;

if (!opts) throw new Error('an HTTP(S) proxy server `host` and `port` must be specified!');
var proxy = clone(opts, {});
Agent.call(this);
Agent.call(this, connect);
this.secure = proxy.protocol && proxy.protocol == 'https:';
var proxy = extend({}, opts);
// if `true`, then connect to the proxy server over TLS. defaults to `false`.
this.secureProxy = proxy.protocol ? proxy.protocol == 'https:' : false;
// prefer `hostname` over `host`, and set the `port` if needed
proxy.host = proxy.hostname || proxy.host;
proxy.port = +proxy.port || (this.secure ? 443 : 80);
proxy.port = +proxy.port || (this.secureProxy ? 443 : 80);

@@ -52,2 +55,8 @@ if (proxy.host && proxy.path) {

/**
* Default options for the "connect" opts object.
*/
var defaults = { port: 80 };
/**
* Called when the node-core HTTP client library is creating a new HTTP request.

@@ -58,16 +67,10 @@ *

HttpProxyAgent.prototype.addRequest = function (req, host, port, localAddress) {
var opts;
if ('object' == typeof host) {
// >= v0.11.x API
opts = host;
} else {
// <= v0.10.x API
opts = {
host: host,
port: port,
localAddress: localAddress
};
}
function connect (req, _opts, fn) {
var proxy = this.proxy;
var secureProxy = this.secureProxy;
// these `opts` are the connect options to connect to the destination endpoint
var opts = extend({}, proxy, defaults, _opts);
// change the `http.ClientRequest` instance's "path" field

@@ -84,3 +87,3 @@ // to the absolute path of the URL that will be requested

// inject the `Proxy-Authorization` header if necessary
var auth = this.proxy.auth;
var auth = proxy.auth;
if (auth) {

@@ -90,26 +93,10 @@ req.setHeader('Proxy-Authorization', 'Basic ' + new Buffer(auth).toString('base64'));

Agent.prototype.addRequest.apply(this, arguments);
};
/**
* Initiates a TCP connection to the specified HTTP proxy server.
*
* @api protected
*/
HttpProxyAgent.prototype.createConnection = function (opts, fn) {
var socket;
if (this.secure) {
socket = tls.connect(this.proxy);
if (secureProxy) {
socket = tls.connect(proxy);
} else {
socket = net.connect(this.proxy);
socket = net.connect(proxy);
}
fn(null, socket);
return socket;
};
function clone (src, dest) {
for (var i in src) dest[i] = src[i];
return dest;
}
{
"name": "http-proxy-agent",
"version": "0.1.0",
"version": "0.2.0",
"description": "An HTTP(s) proxy `http.Agent` implementation for HTTP",

@@ -25,7 +25,9 @@ "main": "http-proxy-agent.js",

"dependencies": {
"agent-base": "~0.0.1"
"agent-base": "~1.0.1",
"extend": "~1.2.0"
},
"devDependencies": {
"mocha": "~1.12.0"
"mocha": "~1.12.0",
"proxy": "~0.2.0"
}
}
http-proxy-agent
================
### An HTTP(s) proxy `http.Agent` implementation for HTTP
[![Build Status](https://travis-ci.org/TooTallNate/node-http-proxy-agent.png?branch=master)](https://travis-ci.org/TooTallNate/node-http-proxy-agent)

@@ -5,0 +6,0 @@ This module provides an `http.Agent` implementation that connects to a specified

@@ -6,6 +6,8 @@

var net = require('net');
var fs = require('fs');
var url = require('url');
var http = require('http');
var https = require('https');
var assert = require('assert');
var Proxy = require('proxy');
var HttpProxyAgent = require('../');

@@ -15,67 +17,221 @@

this.slow(5000);
this.timeout(10000);
var server;
var serverPort;
var link = process.env.LINK || 'http://jsonip.com/';
var proxy;
var proxyPort;
it('should throw an Error if no "proxy" is given', function () {
assert.throws(function () {
new HttpProxyAgent();
var sslProxy;
var sslProxyPort;
before(function (done) {
// setup HTTP proxy server
proxy = Proxy();
proxy.listen(function () {
proxyPort = proxy.address().port;
done();
});
});
it('should work over an HTTP proxy', function (done) {
var proxy = process.env.HTTP_PROXY || process.env.http_proxy || 'http://10.1.10.200:3128';
var agent = new HttpProxyAgent(proxy);
before(function (done) {
// setup target HTTP server
server = http.createServer();
server.listen(function () {
serverPort = server.address().port;
done();
});
});
var opts = url.parse(link);
opts.agent = agent;
before(function (done) {
// setup SSL HTTP proxy server
var options = {
key: fs.readFileSync(__dirname + '/server.key'),
cert: fs.readFileSync(__dirname + '/server.crt')
};
sslProxy = Proxy(https.createServer(options));
sslProxy.listen(function () {
sslProxyPort = sslProxy.address().port;
done();
});
});
http.get(opts, function (res) {
var data = '';
res.setEncoding('utf8');
res.on('data', function (b) {
data += b;
// shut down test HTTP server
after(function (done) {
proxy.once('close', function () { done(); });
proxy.close();
});
after(function (done) {
server.once('close', function () { done(); });
server.close();
});
after(function (done) {
sslProxy.once('close', function () { done(); });
sslProxy.close();
});
describe('constructor', function () {
it('should throw an Error if no "proxy" argument is given', function () {
assert.throws(function () {
new HttpProxyAgent();
});
res.on('end', function () {
data = JSON.parse(data);
assert('ip' in data);
var ips = data.ip.split(/\,\s*/g);
assert(ips.length >= 1);
ips.forEach(function (ip) {
assert(net.isIP(ip));
});
done();
});
it('should accept a "string" proxy argument', function () {
var agent = new HttpProxyAgent('http://127.0.0.1:' + proxyPort);
assert.equal('127.0.0.1', agent.proxy.host);
assert.equal(proxyPort, agent.proxy.port);
});
it('should accept a `url.parse()` result object argument', function () {
var opts = url.parse('http://127.0.0.1:' + proxyPort);
var agent = new HttpProxyAgent(opts);
assert.equal('127.0.0.1', agent.proxy.host);
assert.equal(proxyPort, agent.proxy.port);
});
describe('secureProxy', function () {
it('should default to `false`', function () {
var agent = new HttpProxyAgent({ port: proxyPort });
assert.equal(false, agent.secureProxy);
});
it('should be `false` when "http:" protocol is used', function () {
var agent = new HttpProxyAgent({ port: proxyPort, protocol: 'http:' });
assert.equal(false, agent.secureProxy);
});
it('should be `true` when "https:" protocol is used', function () {
var agent = new HttpProxyAgent({ port: proxyPort, protocol: 'https:' });
assert.equal(true, agent.secureProxy);
});
});
});
it('should work over an HTTPS proxy', function (done) {
var proxy = process.env.HTTPS_PROXY || process.env.https_proxy || 'https://10.1.10.200:3130';
proxy = url.parse(proxy);
proxy.rejectUnauthorized = false;
var agent = new HttpProxyAgent(proxy);
describe('"http" module', function () {
it('should work over an HTTP proxy', function (done) {
// set HTTP "request" event handler for this test
server.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
});
var opts = url.parse(link);
opts.agent = agent;
var proxy = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort;
var agent = new HttpProxyAgent(proxy);
http.get(opts, function (res) {
var data = '';
res.setEncoding('utf8');
res.on('data', function (b) {
data += b;
var opts = url.parse('http://127.0.0.1:' + serverPort);
opts.agent = agent;
http.get(opts, function (res) {
var data = '';
res.setEncoding('utf8');
res.on('data', function (b) {
data += b;
});
res.on('end', function () {
data = JSON.parse(data);
assert.equal('127.0.0.1:' + serverPort, data.host);
assert('via' in data);
done();
});
});
res.on('end', function () {
data = JSON.parse(data);
assert('ip' in data);
var ips = data.ip.split(/\,\s*/g);
assert(ips.length >= 1);
ips.forEach(function (ip) {
assert(net.isIP(ip));
});
it('should work over an HTTPS proxy', function (done) {
// set HTTP "request" event handler for this test
server.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
});
var proxy = process.env.HTTPS_PROXY || process.env.https_proxy || 'https://127.0.0.1:' + sslProxyPort;
proxy = url.parse(proxy);
proxy.rejectUnauthorized = false;
var agent = new HttpProxyAgent(proxy);
var opts = url.parse('http://127.0.0.1:' + serverPort);
opts.agent = agent;
http.get(opts, function (res) {
var data = '';
res.setEncoding('utf8');
res.on('data', function (b) {
data += b;
});
res.on('end', function () {
data = JSON.parse(data);
assert.equal('127.0.0.1:' + serverPort, data.host);
assert('via' in data);
done();
});
});
});
it('should receive the 407 authorization code on the `http.ClientResponse`', function (done) {
// set a proxy authentication function for this test
proxy.authenticate = function (req, fn) {
// reject all requests
fn(null, false);
};
var proxyUri = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort;
var agent = new HttpProxyAgent(proxyUri);
var opts = {};
// `host` and `port` don't really matter since the proxy will reject anyways
opts.host = '127.0.0.1';
opts.port = 80;
opts.agent = agent;
http.get(opts, function (res) {
assert.equal(407, res.statusCode);
assert('proxy-authenticate' in res.headers);
delete proxy.authenticate;
done();
});
});
it('should send the "Proxy-Authorization" request header', function (done) {
// set a proxy authentication function for this test
proxy.authenticate = function (req, fn) {
// username:password is "foo:bar"
fn(null, req.headers['proxy-authorization'] == 'Basic Zm9vOmJhcg==');
};
// set HTTP "request" event handler for this test
server.once('request', function (req, res) {
res.end(JSON.stringify(req.headers));
});
var proxyUri = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort;
var proxyOpts = url.parse(proxyUri);
proxyOpts.auth = 'foo:bar';
var agent = new HttpProxyAgent(proxyOpts);
var opts = url.parse('http://127.0.0.1:' + serverPort);
opts.agent = agent;
http.get(opts, function (res) {
var data = '';
res.setEncoding('utf8');
res.on('data', function (b) {
data += b;
});
res.on('end', function () {
data = JSON.parse(data);
assert.equal('127.0.0.1:' + serverPort, data.host);
assert('via' in data);
delete proxy.authenticate;
done();
});
});
});
it('should emit an "error" event on the `http.ClientRequest` if the proxy does not exist', function (done) {
// port 4 is a reserved, but "unassigned" port
var proxyUri = 'http://127.0.0.1:4';
var agent = new HttpProxyAgent(proxyUri);
var opts = url.parse('http://nodejs.org');
opts.agent = agent;
var req = http.get(opts);
req.once('error', function (err) {
assert.equal('ECONNREFUSED', err.code);
req.abort();
done();
});
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc