Skip to content

Commit

Permalink
[api] first draft of ProxyStream
Browse files Browse the repository at this point in the history
  • Loading branch information
yawnt committed Jun 24, 2013
1 parent 4a97e8c commit 44097a8
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 30 deletions.
20 changes: 20 additions & 0 deletions lib/node-http-proxy.js
Expand Up @@ -392,3 +392,23 @@ exports._getBase = function _getBase (options) {

return result;
};

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

[
'method',
'path',
'headers'
].forEach(function(elem) {
outgoing[elem] = req[elem];
});
};
19 changes: 6 additions & 13 deletions lib/node-http-proxy/http-proxy.js
Expand Up @@ -128,8 +128,8 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {

// If this is a DELETE request then set the "content-length"
// header (if it is not already set)
if (req.method === 'DELETE') {
req.headers['content-length'] = req.headers['content-length'] || '0';
if (req.method === 'DELETE' && !req.headers['content-length']) {
req.headers['content-length'] = '0';
}

//
Expand Down Expand Up @@ -210,14 +210,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
//
// Setup outgoing proxy with relevant properties.
//
outgoing.host = this.target.host;
outgoing.hostname = this.target.hostname;
outgoing.port = this.target.port;
outgoing.socketPath = this.target.socketPath;
outgoing.agent = this.target.agent;
outgoing.method = req.method;
outgoing.path = req.url;
outgoing.headers = req.headers;
httpProxy._setupOutgoing(outgoing, this.target, req);

//
// If the changeOrigin option is specified, change the
Expand All @@ -238,14 +231,14 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
//
if (req.httpVersion === '1.0') {
if (req.headers.connection) {
response.headers.connection = req.headers.connection
response.headers.connection = req.headers.connection;
} else {
response.headers.connection = 'close'
response.headers.connection = 'close';
}
} else if (!response.headers.connection) {
if (req.headers.connection) { response.headers.connection = req.headers.connection }
else {
response.headers.connection = 'keep-alive'
response.headers.connection = 'keep-alive';
}
}

Expand Down
18 changes: 1 addition & 17 deletions lib/node-http-proxy/streams/forward.js
Expand Up @@ -13,23 +13,7 @@ var ForwardStream = module.exports = function ForwardStream(options) {
var protocol = options.https ? https : http,
outgoing = proxy._getBase(options);

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

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

// pipe throw-safe? do we need to add a ` on 'error' ` handler?
self.request = protocol.request(outgoing, function() {});
Expand Down
48 changes: 48 additions & 0 deletions lib/node-http-proxy/streams/proxy.js
@@ -0,0 +1,48 @@
var Duplex = require('stream').Duplex,
proxy = require('../../node-http-proxy');
http = require('http'),
https = require('https'),
url = require('url'),
util = require('util');

var ProxyStream = module.exports = function ProxyStream(target, changeOrigin) {
Duplex.call(this);

var self = this;

this.once('pipe', function(req) {
var protocol = target.https ? https : http,
outgoing = proxy._getBase(target);

proxy._setupOutgoing(outgoing, target, req);

This comment has been minimized.

Copy link
@cronopio

cronopio Jul 11, 2013

Contributor

Seems that _setupOutgoing() is not working proper, from _getBase() comes just an empty function function () {} so thats the value of outgoing after this point, so when the request is made some lines below the outgoing var has nothing.


if (changeOrigin) {
outgoing.headers.host = target.host + ':' + target.port;
}

self.request = protocol.request(outgoing, function(res) {
if(req.httpVersion === '1.0') {
res.headers.connection = req.headers.connection || 'close';
}
else if(!res.headers.connection) {
res.headers.connection = req.headers.connection || 'keep-alive';
}

if(req.httpVersion === '1.0' || (req.method === 'DELETE' && !req.headers['content-length'])) {
delete response.headers['transfer-encoding'];
}

if(~[301,302].indexOf(res.statusCode) && typeof response.headers.location !== 'undefined') {
location = url.parse(response.headers.location);
}

});
});

};

ForwardStream.prototype._write = function(chunk, encoding, callback) {
this.request.write(chunk, encoding, callback);
};

util.inherits(ForwardStream, Duplex);

0 comments on commit 44097a8

Please sign in to comment.