Skip to content

Commit

Permalink
[refactor] start working on forwardStream
Browse files Browse the repository at this point in the history
  • Loading branch information
yawnt committed Jun 24, 2013
1 parent d561e27 commit ac92c65
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 58 deletions.
8 changes: 4 additions & 4 deletions lib/node-http-proxy.js
Expand Up @@ -24,10 +24,10 @@
*/

var util = require('util'),
http = require('http'),
https = require('https'),
events = require('events'),
var util = require('util'),
http = require('http'),
https = require('https'),
events = require('events'),
maxSockets = 100;

//
Expand Down
86 changes: 32 additions & 54 deletions lib/node-http-proxy/http-proxy.js
Expand Up @@ -24,10 +24,10 @@
*/

var events = require('events'),
http = require('http'),
util = require('util'),
url = require('url'),
var events = require('events'),
http = require('http'),
util = require('util'),
url = require('url'),
httpProxy = require('../node-http-proxy');

//
Expand Down Expand Up @@ -91,10 +91,12 @@ var HttpProxy = exports.HttpProxy = function (options) {
//
// Setup opt-in features
//
this.enable = options.enable || {};
this.enable.xforward = typeof this.enable.xforward === 'boolean'
? this.enable.xforward
: true;
this.enable = options.enable || {};

if(typeof this.enable.xforward !== 'boolean') {
this.enable.xforward = true;
}


//
// Setup additional options for WebSocket proxying. When forcing
Expand Down Expand Up @@ -140,29 +142,17 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
// * `x-forwarded-port`: Port of the original request.
//
if (this.enable.xforward && req.connection && req.socket) {
if (req.headers['x-forwarded-for']) {
var addressToAppend = "," + req.connection.remoteAddress || req.socket.remoteAddress;
req.headers['x-forwarded-for'] += addressToAppend;
}
else {
req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.socket.remoteAddress;
}

if (req.headers['x-forwarded-port']) {
var portToAppend = "," + req.connection.remotePort || req.socket.remotePort;
req.headers['x-forwarded-port'] += portToAppend;
}
else {
req.headers['x-forwarded-port'] = req.connection.remotePort || req.socket.remotePort;
}

if (req.headers['x-forwarded-proto']) {
var protoToAppend = "," + getProto(req);
req.headers['x-forwarded-proto'] += protoToAppend;
}
else {
req.headers['x-forwarded-proto'] = getProto(req);
}
req.headers['x-forwarded-for'] = (req.headers['x-forwarded-for'] || '') +
(req.headers['x-forwarded-for'] ? ',' : '') +
(req.connection.remoteAddress || socket.remoteAddress);

req.headers['x-forwarded-port'] = (req.headers['x-forwarded-port'] || '') +
(req.headers['x-forwarded-port'] ? ',' : '') +
(req.connection.remotePort || socket.remotePort);

req.headers['x-forwarded-proto'] = (req.headers['x-forwarded-proto'] || '') +
(req.headers['x-forwarded-proto'] ? ',' : '') +
getProto(req);
}

if (this.timeout) {
Expand Down Expand Up @@ -469,29 +459,17 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, upgradeHead,
// * `x-forwarded-port`: Port of the original request.
//
if (this.enable.xforward && req.connection) {
if (req.headers['x-forwarded-for']) {
var addressToAppend = "," + req.connection.remoteAddress || socket.remoteAddress;
req.headers['x-forwarded-for'] += addressToAppend;
}
else {
req.headers['x-forwarded-for'] = req.connection.remoteAddress || socket.remoteAddress;
}

if (req.headers['x-forwarded-port']) {
var portToAppend = "," + req.connection.remotePort || socket.remotePort;
req.headers['x-forwarded-port'] += portToAppend;
}
else {
req.headers['x-forwarded-port'] = req.connection.remotePort || socket.remotePort;
}

if (req.headers['x-forwarded-proto']) {
var protoToAppend = "," + (req.connection.pair ? 'wss' : 'ws');
req.headers['x-forwarded-proto'] += protoToAppend;
}
else {
req.headers['x-forwarded-proto'] = req.connection.pair ? 'wss' : 'ws';
}
req.headers['x-forwarded-for'] = (req.headers['x-forwarded-for'] || '') +
(req.headers['x-forwarded-for'] ? ',' : '') +
(req.connection.remoteAddress || socket.remoteAddress);

req.headers['x-forwarded-port'] = (req.headers['x-forwarded-port'] || '') +
(req.headers['x-forwarded-port'] ? ',' : '') +
(req.connection.remotePort || socket.remotePort);

req.headers['x-forwarded-proto'] = (req.headers['x-forwarded-proto'] || '') +
(req.headers['x-forwarded-proto'] ? ',' : '') +
(req.connection.pair ? 'wss' : 'ws');
}

self.emit('websocket:start', req, socket, head, this.target);
Expand Down
43 changes: 43 additions & 0 deletions lib/node-http-proxy/streams/forward.js
@@ -0,0 +1,43 @@
var Writable = require('stream').Writable,
http = require('http'),
https = require('https'),
util = require('util');

var ForwardStream = module.exports = function ForwardStream(options) {
Writable.call(this);

var self = this;

this.once('pipe', function(req) {
self.outgoing = options.https ? https : http;

[
'host',
'hostname',
'port',
'socketPath',
'agent'
].forEach(function(elem) {
outgoing[elem] = target[elem];
});

[
'method',
'path',
'headers'
].forEach(function(elem) {
outgoing[elem] = req[elem];
});

});

}

ForwardStream.prototype._write = function() {



}

util.inherits(ForwardStream, Writable);

0 comments on commit ac92c65

Please sign in to comment.