Skip to content

Commit

Permalink
[api test dist doc] Updated for 0.3.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Sep 10, 2010
1 parent 9f0aeac commit a9084b9
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 125 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

node-http-proxy

Copyright (c) 2010 Charlie Robbins & Marak Squires http://github.com/nodejitsu/node-http-proxy
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, & Marak Squires

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
48 changes: 35 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# node-http-proxy - v0.1.5
# node-http-proxy - v0.3.0

<img src = "http://i.imgur.com/dSSUX.png"/>

Expand All @@ -19,7 +19,6 @@

Let's suppose you were running multiple http application servers, but you only wanted to expose one machine to the internet. You could setup node-http-proxy on that one machine and then reverse-proxy the incoming http requests to locally running services which were not exposed to the outside network.


### Installing npm (node package manager)
<pre>
curl http://npmjs.org/install.sh | sh
Expand All @@ -44,7 +43,7 @@ Let's suppose you were running multiple http application servers, but you only w
}).listen(9000);
</pre>

see the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js) for further examples.
See the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js) for further examples.

### How to setup a proxy server with custom server logic
<pre>
Expand All @@ -54,15 +53,34 @@ see the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js)
// create a proxy server with custom application logic
httpProxy.createServer(function (req, res, proxy) {
// Put your custom server logic here
proxy.proxyRequest(9000, 'localhost', req, res);
proxy.proxyRequest(9000, 'localhost');
}).listen(8000);

http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
</pre>

### How to setup a proxy server with latency (e.g. IO, etc)
<pre>
var http = require('http'),
httpProxy = require('http-proxy');

// create a proxy server with custom application logic
httpProxy.createServer(function (req, res, proxy) {
// Wait for two seconds then respond
setTimeout(function () {
proxy.proxyRequest(9000, 'localhost');
}, 2000);
}).listen(8000);

http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);

</pre>

### How to proxy requests with a regular http server
Expand All @@ -72,30 +90,34 @@ see the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js)

// create a regular http server and proxy its handler
http.createServer(function (req, res){
var proxy = new httpProxy.HttpProxy;
proxy.watch(req, res);
// Put your custom server logic here
// Create a new instance of HttProxy for this request
// each instance is only valid for serving one request
//
// Don't worry benchmarks show the object
// creation is lightning fast
var proxy = new httpProxy.HttpProxy(req, res);

// Put your custom server logic here, then proxy
proxy.proxyRequest(9000, 'localhost', req, res);
}).listen(8001);

http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);

}).listen(9000);
</pre>

### Why doesn't node-http-proxy have more advanced features like x, y, or z?

If you have a suggestion for a feature currently not supported, feel free to open a [support issue](http://github.com/nodejitsu/node-http-proxy/issues). node-http-proxy is designed to just proxy http requests from one server to another, but we will be soon releasing many other complimentary projects that can be used in conjunction with node-http-proxy.

<br/><hr/>
<br/>
### License

(The MIT License)

Copyright (c) 2010 Charlie Robbins & Marak Squires http://github.com/nodejitsu/
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, & Marak Squires

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand All @@ -116,4 +138,4 @@ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

[0]:http://nodejitsu.com "nodejitsu.com"
[0]: http://nodejitsu.com
2 changes: 1 addition & 1 deletion benchmarks/http-server-base
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Marak@Maraks-MacBook-Pro ~/dev/zalgo.js]$ ab -c 50 -n 1000 -k http://127.0.0.1:9000/
$ ab -c 50 -n 1000 -k http://127.0.0.1:9000/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Expand Down
42 changes: 21 additions & 21 deletions benchmarks/proxy-to-http
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Marak@Maraks-MacBook-Pro ~]$ ab -c 5 -n 1000 http://127.0.0.1:8000/
$ ab -c 5 -n 1000 http://127.0.0.1:8000/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Expand All @@ -22,35 +22,35 @@ Server Hostname: 127.0.0.1
Server Port: 8000

Document Path: /
Document Length: 155 bytes
Document Length: 160 bytes

Concurrency Level: 5
Time taken for tests: 0.602 seconds
Time taken for tests: 0.450 seconds
Complete requests: 1000
Failed requests: 5
(Connect: 0, Receive: 0, Length: 5, Exceptions: 0)
Failed requests: 1
(Connect: 0, Receive: 0, Length: 1, Exceptions: 0)
Write errors: 0
Total transferred: 247030 bytes
HTML transferred: 155030 bytes
Requests per second: 1660.64 [#/sec] (mean)
Time per request: 3.011 [ms] (mean)
Time per request: 0.602 [ms] (mean, across all concurrent requests)
Transfer rate: 400.61 [Kbytes/sec] received
Total transferred: 252006 bytes
HTML transferred: 160006 bytes
Requests per second: 2219.97 [#/sec] (mean)
Time per request: 2.252 [ms] (mean)
Time per request: 0.450 [ms] (mean, across all concurrent requests)
Transfer rate: 546.33 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 1
Processing: 2 3 0.8 3 10
Waiting: 2 3 0.8 3 10
Total: 2 3 0.8 3 10
Connect: 0 0 0.0 0 0
Processing: 1 2 0.9 2 10
Waiting: 1 2 0.9 2 10
Total: 1 2 0.9 2 10

Percentage of the requests served within a certain time (ms)
50% 3
66% 3
50% 2
66% 2
75% 3
80% 3
90% 3
95% 4
98% 5
99% 9
100% 10 (longest request)
95% 3
98% 3
99% 7
100% 10 (longest request)
15 changes: 7 additions & 8 deletions demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,22 @@ httpProxy.createServer(9000, 'localhost').listen(8000);
sys.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);

/****** http proxy server with latency******/
/*httpProxy.createServer(function (req, res, proxy){
httpProxy.createServer(function (req, res, proxy){
setTimeout(function(){
proxy.proxyRequest(9000, 'localhost', req, res);
proxy.proxyRequest(9000, 'localhost');
}, 200)
}).listen(8001);
sys.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8001 '.yellow + 'with latency'.magenta.underline );*/
sys.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8001 '.yellow + 'with latency'.magenta.underline );

/****** http server with proxyRequest handler and latency******/
/*http.createServer(function (req, res){
var proxy = new httpProxy.HttpProxy;
proxy.watch(req, res);
http.createServer(function (req, res){
var proxy = new httpProxy.HttpProxy(req, res);

setTimeout(function(){
proxy.proxyRequest(9000, 'localhost', req, res);
proxy.proxyRequest(9000, 'localhost');
}, 200);
}).listen(8002);
sys.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8002 '.yellow + 'with proxyRequest handler'.cyan.underline + ' and latency'.magenta);*/
sys.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8002 '.yellow + 'with proxyRequest handler'.cyan.underline + ' and latency'.magenta);

/****** regular http server ******/
http.createServer(function (req, res){
Expand Down
67 changes: 8 additions & 59 deletions lib/node-http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

var sys = require('sys'),
http = require('http'),
eyes = require('eyes'),
pool = require('pool'),
events = require('events'),
pool = require('pool'),
min = 0,
Expand All @@ -48,6 +46,10 @@ exports.createServer = function () {
var server = http.createServer(function (req, res){
var proxy = new HttpProxy(req, res);

proxy.emitter.on('proxy', function (err, body) {
server.emit('proxy', err, body);
});

// If we were passed a callback to process the request
// or response in some way, then call it.
if(callback) {
Expand Down Expand Up @@ -110,7 +112,7 @@ HttpProxy.prototype = {

// Rebroadcast any events that have been buffered
for (var i = 0, len = this.events.length; i < len; ++i) {
req.emit.apply(req, this.events[i]);
req.emit.apply(req, this.events[i]);
}
},

Expand All @@ -133,6 +135,7 @@ HttpProxy.prototype = {

res.end();
};

// Add a listener for the connection timeout event
reverse_proxy.connection.addListener('error', error);

Expand All @@ -157,8 +160,7 @@ HttpProxy.prototype = {
// Add event listener for end of proxied response
response.addListener('end', function () {
// Remark: Emit the end event for testability
self.emitter.emit('end', null, self.body);

self.emitter.emit('proxy', null, self.body);
res.end();
});
});
Expand All @@ -179,57 +181,4 @@ HttpProxy.prototype = {
}
};

exports.HttpProxy = HttpProxy;

/*// Create an error handler so we can use it temporarily
var error = function (err) {
res.writeHead(200, {'Content-Type': 'text/plain'});
if(req.method !== 'HEAD') {
res.write('An error has occurred: ' + sys.puts(JSON.stringify(err)));
}
res.end();
};
// Add a listener for the connection timeout event
reverse_proxy.connection.addListener('error', error);
// Add a listener for the reverse_proxy response event
reverse_proxy.addListener('response', function (response) {
if (response.headers.connection) {
if (req.headers.connection) response.headers.connection = req.headers.connection;
else response.headers.connection = 'close';
}
// Set the response headers of the client response
res.writeHead(response.statusCode, response.headers);
// Add event handler for the proxied response in chunks
response.addListener('data', function (chunk) {
if(req.method !== 'HEAD') {
res.write(chunk, 'binary');
self.body += chunk;
}
});
// Add event listener for end of proxied response
response.addListener('end', function () {
// Remark: Emit the end event for testability
self.emitter.emit('end', null, self.body);
res.end();
});
});
// Chunk the client request body as chunks from the proxied request come in
req.addListener('data', function (chunk) {
reverse_proxy.write(chunk, 'binary');
})
// At the end of the client request, we are going to stop the proxied request
req.addListener('end', function () {
reverse_proxy.end();
//reverse_proxy.connection.removeListener('error', error);
});
self.unwatch(req);*/
exports.HttpProxy = HttpProxy;
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "http-proxy",
"description": "A full-featured http reverse proxy for node.js",
"version": "0.1.6",
"version": "0.3.0",
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
"contributors": [
"contributors": [
{ "name": "Mikeal Rogers", "email": 'mikeal.rogers@gmail.com' },
{ "name": "Marak Squires", "email": "marak.squires@gmail.com" }
],
"repository": {
Expand All @@ -12,9 +13,10 @@
},
"keywords": ["reverse", "proxy", "http"],
"dependencies": {
"colors": ">= 0.3.0"
"colors": ">= 0.3.0",
"pool": ">= 0.4.1"
},
"main": "./lib/node-http-proxy",
"scripts": { "test": "vows" },
"engines": { "node": ">= 0.1.98" }
"engines": { "node": ">= 0.2.0" }
}
Loading

0 comments on commit a9084b9

Please sign in to comment.