Skip to content

Commit

Permalink
HttpProxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Jun 22, 2011
1 parent 2e56985 commit ec15ec6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
7 changes: 5 additions & 2 deletions lib/node.io/process_worker.js
Expand Up @@ -7,7 +7,7 @@
var Processor = require('./processor').Processor,
crc32 = require('./utils').crc32,
JobClass = require('./job').JobClass,
Proxy = require('./request').Proxy;
HttpProxy = require('./request').HttpProxy;

/**
* Routes messages received from the master process.
Expand Down Expand Up @@ -236,10 +236,13 @@ Processor.prototype.setupWorkerEvents = function (job, master) {

//Bind a proxy if the `proxy` op is set. Also allow the user to specify
//a callback which returns a Proxy (e.g. to easily cycle proxies)
if (job.options.proxy instanceof Proxy) {
if (typeof job.options.proxy.proxify === 'function') {
job.options.proxy.proxify(instance);
} else if (typeof job.options.proxy === 'function') {
job.options.proxy().proxify(instance);
} else if (typeof job.options.proxy === 'string') {
job.options.proxy = new HttpProxy(job.options.proxy);
job.options.proxy.proxify(instance);
}

//Assign some input to the instance
Expand Down
13 changes: 9 additions & 4 deletions lib/node.io/request.js
Expand Up @@ -261,7 +261,9 @@ Job.prototype.doRequest = function (method, resource, body, headers, callback, p
//Copy `headers` before modifying it
headers = utils.put({}, headers);

headers.host = url.hostname;
if (typeof headers.host === 'undefined') {
headers.host = url.hostname;
}

//Add headers set before the doRequest call if from the same host (e.g. cookie, user-agent, referer, etc.)
if (typeof this.last.headers === 'object' && this.last.host === url.hostname) {
Expand Down Expand Up @@ -346,6 +348,10 @@ Job.prototype.doRequest = function (method, resource, body, headers, callback, p
} else {
//Handle the 30x redirect
var location = resolve(resource, response.headers.location);
var redirect = urlparse(location);
if (redirect.host) {
headers.host = redirect.host;
}
self.doRequest(method, location, body, headers, callback, parse, ++redirects);
}
return;
Expand Down Expand Up @@ -572,16 +578,15 @@ Proxy.prototype.proxify = function (job) {
/**
* Creates a new proxy that routes requests through the specified host.
*
* Note: 100% UNTESTED - use at your own peril.
*
* @param {Function} host
* @api public
*/
var HttpProxy = function (host) {
var proxy = urlparse(host);
var url_callback = function (url) {
var u = urlparse(url);
this.url_host = u.host;
url = host + u.pathname;
url = proxy.protocol + '//' + proxy.host + u.pathname;
if (u.search) {
url += u.search;
}
Expand Down

0 comments on commit ec15ec6

Please sign in to comment.