Permalink
Browse files

HttpProxy support

  • Loading branch information...
chriso committed Jun 22, 2011
1 parent 2e56985 commit ec15ec6d52592802ca1df33ed1457ba85b16f646
Showing with 14 additions and 6 deletions.
  1. +5 −2 lib/node.io/process_worker.js
  2. +9 −4 lib/node.io/request.js
@@ -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.
@@ -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
View
@@ -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) {
@@ -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;
@@ -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;
}

0 comments on commit ec15ec6

Please sign in to comment.