From a56ae94894fab89bc0ebc34f9b69cfccf3cb239a Mon Sep 17 00:00:00 2001 From: yawnt Date: Mon, 24 Jun 2013 11:16:23 +0200 Subject: [PATCH 01/14] [fix] refactor --- lib/node-http-proxy/http-proxy.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/node-http-proxy/http-proxy.js b/lib/node-http-proxy/http-proxy.js index 0efb2fa3c..f481ccfcc 100644 --- a/lib/node-http-proxy/http-proxy.js +++ b/lib/node-http-proxy/http-proxy.js @@ -66,7 +66,7 @@ var HttpProxy = exports.HttpProxy = function (options) { // this.forward = options.forward; this.target = options.target; - this.timeout = options.timeout; + this.timeout = options.timeout; // // Setup the necessary instances instance variables for @@ -207,12 +207,10 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) { // This NODE_ENV=production behavior is mimics Express and // Connect. // - if (process.env.NODE_ENV === 'production') { - res.write('Internal Server Error'); - } - else { - res.write('An error has occurred: ' + JSON.stringify(err)); - } + res.write(process.env.NODE_ENV === 'production' + ? 'Internal Server Error' + : 'An error has occurred: ' + JSON.stringify(err) + ); } try { res.end() } From c8d9e0f139d0a4b501bfdbb1ec48b636f0b7d086 Mon Sep 17 00:00:00 2001 From: yawnt Date: Mon, 24 Jun 2013 12:01:28 +0200 Subject: [PATCH 02/14] [refactor] start working on forwardStream --- lib/node-http-proxy.js | 8 +-- lib/node-http-proxy/http-proxy.js | 86 ++++++++++---------------- lib/node-http-proxy/streams/forward.js | 43 +++++++++++++ 3 files changed, 79 insertions(+), 58 deletions(-) create mode 100644 lib/node-http-proxy/streams/forward.js diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index 956a5f3d3..d71c589fe 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -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; // diff --git a/lib/node-http-proxy/http-proxy.js b/lib/node-http-proxy/http-proxy.js index f481ccfcc..d76691916 100644 --- a/lib/node-http-proxy/http-proxy.js +++ b/lib/node-http-proxy/http-proxy.js @@ -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'); // @@ -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 @@ -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) { @@ -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); diff --git a/lib/node-http-proxy/streams/forward.js b/lib/node-http-proxy/streams/forward.js new file mode 100644 index 000000000..55f50d9bd --- /dev/null +++ b/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); + From bd62a68017436485427a69b009ef72611bb320db Mon Sep 17 00:00:00 2001 From: yawnt Date: Mon, 24 Jun 2013 12:09:55 +0200 Subject: [PATCH 03/14] [fix] now you can pipe() --- lib/node-http-proxy/streams/forward.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/node-http-proxy/streams/forward.js b/lib/node-http-proxy/streams/forward.js index 55f50d9bd..ff494e448 100644 --- a/lib/node-http-proxy/streams/forward.js +++ b/lib/node-http-proxy/streams/forward.js @@ -1,4 +1,5 @@ var Writable = require('stream').Writable, + proxy = require('../../node-http-proxy'); http = require('http'), https = require('https'), util = require('util'); @@ -9,7 +10,8 @@ var ForwardStream = module.exports = function ForwardStream(options) { var self = this; this.once('pipe', function(req) { - self.outgoing = options.https ? https : http; + var protocol = options.https ? https : http, + outgoing = proxy._getBase(options); [ 'host', @@ -18,7 +20,7 @@ var ForwardStream = module.exports = function ForwardStream(options) { 'socketPath', 'agent' ].forEach(function(elem) { - outgoing[elem] = target[elem]; + outgoing[elem] = options[elem]; }); [ @@ -28,16 +30,15 @@ var ForwardStream = module.exports = function ForwardStream(options) { ].forEach(function(elem) { outgoing[elem] = req[elem]; }); - - }); - -} - -ForwardStream.prototype._write = function() { + self.request = protocol.request(outgoing) + }); +}; -} +ForwardStream.prototype._write = function(chunk, encoding, callback) { + this.request.write(chunk, encoding, callback); +}; util.inherits(ForwardStream, Writable); From 492134f830678f071a0e4915421b48fd4703d89f Mon Sep 17 00:00:00 2001 From: yawnt Date: Mon, 24 Jun 2013 12:17:52 +0200 Subject: [PATCH 04/14] [fix] typo --- lib/node-http-proxy/http-proxy.js | 14 +++++++------- lib/node-http-proxy/streams/forward.js | 3 ++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/node-http-proxy/http-proxy.js b/lib/node-http-proxy/http-proxy.js index d76691916..8b9ef4a8f 100644 --- a/lib/node-http-proxy/http-proxy.js +++ b/lib/node-http-proxy/http-proxy.js @@ -24,11 +24,12 @@ */ -var events = require('events'), - http = require('http'), - util = require('util'), - url = require('url'), - httpProxy = require('../node-http-proxy'); +var events = require('events'), + http = require('http'), + util = require('util'), + url = require('url'), + ForwardStream = require('./streams/forward'), + httpProxy = require('../node-http-proxy'); // // ### function HttpProxy (options) @@ -97,7 +98,6 @@ var HttpProxy = exports.HttpProxy = function (options) { this.enable.xforward = true; } - // // Setup additional options for WebSocket proxying. When forcing // the WebSocket handshake to change the `sec-websocket-location` @@ -170,7 +170,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) { // if (this.forward) { this.emit('forward', req, res, this.forward); - this._forwardRequest(req); + req.pipe(new ForwardStream(this.forward)); } // diff --git a/lib/node-http-proxy/streams/forward.js b/lib/node-http-proxy/streams/forward.js index ff494e448..c29251730 100644 --- a/lib/node-http-proxy/streams/forward.js +++ b/lib/node-http-proxy/streams/forward.js @@ -31,7 +31,8 @@ var ForwardStream = module.exports = function ForwardStream(options) { outgoing[elem] = req[elem]; }); - self.request = protocol.request(outgoing) + // pipe throw-safe? do we need to add a ` on 'error' ` handler? + self.request = protocol.request(outgoing, function() {}); }); }; From ab4a2419b3b7299336c19ede15e81988d3df7e5d Mon Sep 17 00:00:00 2001 From: yawnt Date: Mon, 24 Jun 2013 13:11:01 +0200 Subject: [PATCH 05/14] [api] first draft of ProxyStream --- lib/node-http-proxy.js | 20 +++++++++++ lib/node-http-proxy/http-proxy.js | 19 ++++------ lib/node-http-proxy/streams/forward.js | 18 +--------- lib/node-http-proxy/streams/proxy.js | 48 ++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 30 deletions(-) create mode 100644 lib/node-http-proxy/streams/proxy.js diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index d71c589fe..922c6d8f0 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -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]; + }); +}; \ No newline at end of file diff --git a/lib/node-http-proxy/http-proxy.js b/lib/node-http-proxy/http-proxy.js index 8b9ef4a8f..db4be1cc7 100644 --- a/lib/node-http-proxy/http-proxy.js +++ b/lib/node-http-proxy/http-proxy.js @@ -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'; } // @@ -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 @@ -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'; } } diff --git a/lib/node-http-proxy/streams/forward.js b/lib/node-http-proxy/streams/forward.js index c29251730..de49bf6af 100644 --- a/lib/node-http-proxy/streams/forward.js +++ b/lib/node-http-proxy/streams/forward.js @@ -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() {}); diff --git a/lib/node-http-proxy/streams/proxy.js b/lib/node-http-proxy/streams/proxy.js new file mode 100644 index 000000000..7dd9c26f7 --- /dev/null +++ b/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); + + 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); From 445add091c3b754526a18269b6e332a9a0f5e10e Mon Sep 17 00:00:00 2001 From: yawnt Date: Mon, 24 Jun 2013 15:10:33 +0200 Subject: [PATCH 06/14] [api] proxystream --- lib/node-http-proxy/http-proxy.js | 240 +-------------------------- lib/node-http-proxy/streams/proxy.js | 90 +++++++++- 2 files changed, 85 insertions(+), 245 deletions(-) diff --git a/lib/node-http-proxy/http-proxy.js b/lib/node-http-proxy/http-proxy.js index db4be1cc7..f76187889 100644 --- a/lib/node-http-proxy/http-proxy.js +++ b/lib/node-http-proxy/http-proxy.js @@ -29,6 +29,7 @@ var events = require('events'), util = require('util'), url = require('url'), ForwardStream = require('./streams/forward'), + ProxyStream = require('./streams/proxy'), httpProxy = require('../node-http-proxy'); // @@ -163,6 +164,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) { // Emit the `start` event indicating that we have begun the proxy operation. // this.emit('start', req, res, this.target); + req.pipe(new ProxyStream(res, options)).pipe(res); // // If forwarding is enabled for this instance, foward proxy the @@ -172,244 +174,6 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) { this.emit('forward', req, res, this.forward); req.pipe(new ForwardStream(this.forward)); } - - // - // #### function proxyError (err) - // #### @err {Error} Error contacting the proxy target - // Short-circuits `res` in the event of any error when - // contacting the proxy target at `host` / `port`. - // - function proxyError(err) { - errState = true; - - // - // Emit an `error` event, allowing the application to use custom - // error handling. The error handler should end the response. - // - if (self.emit('proxyError', err, req, res)) { - return; - } - - res.writeHead(500, { 'Content-Type': 'text/plain' }); - - if (req.method !== 'HEAD') { - // - // This NODE_ENV=production behavior is mimics Express and - // Connect. - // - res.write(process.env.NODE_ENV === 'production' - ? 'Internal Server Error' - : 'An error has occurred: ' + JSON.stringify(err) - ); - } - - try { res.end() } - catch (ex) { console.error("res.end error: %s", ex.message) } - } - - // - // Setup outgoing proxy with relevant properties. - // - httpProxy._setupOutgoing(outgoing, this.target, req); - - // - // If the changeOrigin option is specified, change the - // origin of the host header to the target URL! Please - // don't revert this without documenting it! - // - if (this.changeOrigin) { - outgoing.headers.host = this.target.host + ':' + this.target.port; - } - - // - // Open new HTTP request to internal resource with will act - // as a reverse proxy pass - // - reverseProxy = this.target.protocol.request(outgoing, function (response) { - // - // Process the `reverseProxy` `response` when it's received. - // - if (req.httpVersion === '1.0') { - if (req.headers.connection) { - response.headers.connection = req.headers.connection; - } else { - 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'; - } - } - - // Remove `Transfer-Encoding` header if client's protocol is HTTP/1.0 - // or if this is a DELETE request with no content-length header. - // See: https://github.com/nodejitsu/node-http-proxy/pull/373 - if (req.httpVersion === '1.0' || (req.method === 'DELETE' - && !req.headers['content-length'])) { - delete response.headers['transfer-encoding']; - } - - if ((response.statusCode === 301 || response.statusCode === 302) - && typeof response.headers.location !== 'undefined') { - location = url.parse(response.headers.location); - if (location.host === req.headers.host) { - if (self.source.https && !self.target.https) { - response.headers.location = response.headers.location.replace(/^http\:/, 'https:'); - } - if (self.target.https && !self.source.https) { - response.headers.location = response.headers.location.replace(/^https\:/, 'http:'); - } - } - } - - // - // When the `reverseProxy` `response` ends, end the - // corresponding outgoing `res` unless we have entered - // an error state. In which case, assume `res.end()` has - // already been called and the 'error' event listener - // removed. - // - var ended = false; - response.on('close', function () { - if (!ended) { response.emit('end') } - }); - - // - // After reading a chunked response, the underlying socket - // will hit EOF and emit a 'end' event, which will abort - // the request. If the socket was paused at that time, - // pending data gets discarded, truncating the response. - // This code makes sure that we flush pending data. - // - response.connection.on('end', function () { - if (response.readable && response.resume) { - response.resume(); - } - }); - - response.on('end', function () { - ended = true; - if (!errState) { - try { res.end() } - catch (ex) { console.error("res.end error: %s", ex.message) } - - // Emit the `end` event now that we have completed proxying - self.emit('end', req, res, response); - } - }); - - // Allow observer to modify headers or abort response - try { self.emit('proxyResponse', req, res, response) } - catch (ex) { - errState = true; - return; - } - - // Set the headers of the client response - Object.keys(response.headers).forEach(function (key) { - res.setHeader(key, response.headers[key]); - }); - res.writeHead(response.statusCode); - - function ondata(chunk) { - if (res.writable) { - // Only pause if the underlying buffers are full, - // *and* the connection is not in 'closing' state. - // Otherwise, the pause will cause pending data to - // be discarded and silently lost. - if (false === res.write(chunk) && response.pause - && response.connection.readable) { - response.pause(); - } - } - } - - response.on('data', ondata); - - function ondrain() { - if (response.readable && response.resume) { - response.resume(); - } - } - - res.on('drain', ondrain); - }); - - // - // Handle 'error' events from the `reverseProxy`. Setup timeout override if needed - // - reverseProxy.once('error', proxyError); - - // Set a timeout on the socket if `this.timeout` is specified. - reverseProxy.once('socket', function (socket) { - if (self.timeout) { - socket.setTimeout(self.timeout); - } - }); - - // - // Handle 'error' events from the `req` (e.g. `Parse Error`). - // - req.on('error', proxyError); - - // - // If `req` is aborted, we abort our `reverseProxy` request as well. - // - req.on('aborted', function () { - reverseProxy.abort(); - }); - - // - // For each data `chunk` received from the incoming - // `req` write it to the `reverseProxy` request. - // - req.on('data', function (chunk) { - if (!errState) { - var flushed = reverseProxy.write(chunk); - if (!flushed) { - req.pause(); - reverseProxy.once('drain', function () { - try { req.resume() } - catch (er) { console.error("req.resume error: %s", er.message) } - }); - - // - // Force the `drain` event in 100ms if it hasn't - // happened on its own. - // - setTimeout(function () { - reverseProxy.emit('drain'); - }, 100); - } - } - }); - - // - // When the incoming `req` ends, end the corresponding `reverseProxy` - // request unless we have entered an error state. - // - req.on('end', function () { - if (!errState) { - reverseProxy.end(); - } - }); - - //Aborts reverseProxy if client aborts the connection. - req.on('close', function () { - if (!errState) { - reverseProxy.abort(); - } - }); - - // - // If we have been passed buffered data, resume it. - // - if (buffer) { - return !errState - ? buffer.resume() - : buffer.destroy(); - } }; // diff --git a/lib/node-http-proxy/streams/proxy.js b/lib/node-http-proxy/streams/proxy.js index 7dd9c26f7..08dc2fdb7 100644 --- a/lib/node-http-proxy/streams/proxy.js +++ b/lib/node-http-proxy/streams/proxy.js @@ -5,18 +5,20 @@ var Duplex = require('stream').Duplex, url = require('url'), util = require('util'); -var ProxyStream = module.exports = function ProxyStream(target, changeOrigin) { +var ProxyStream = module.exports = function ProxyStream(response, options) { Duplex.call(this); - var self = this; - + var self = this, + target = options.target, + source = options.source; + this.once('pipe', function(req) { var protocol = target.https ? https : http, outgoing = proxy._getBase(target); proxy._setupOutgoing(outgoing, target, req); - if (changeOrigin) { + if (options.changeOrigin) { outgoing.headers.host = target.host + ':' + target.port; } @@ -29,14 +31,81 @@ var ProxyStream = module.exports = function ProxyStream(target, changeOrigin) { } if(req.httpVersion === '1.0' || (req.method === 'DELETE' && !req.headers['content-length'])) { - delete response.headers['transfer-encoding']; + delete res.headers['transfer-encoding']; } - if(~[301,302].indexOf(res.statusCode) && typeof response.headers.location !== 'undefined') { - location = url.parse(response.headers.location); + if(~[301,302].indexOf(res.statusCode) && typeof res.headers.location !== 'undefined') { + var location = url.parse(res.headers.location); + if ( + location.host === req.headers.host && + ( + source.https && !target.https || + target.https && !source.https + ) + ) { + res.headers.location = res.headers.location.replace(/^https\:/, 'http:'); + } } + try { + self.emit('proxyResponse', req, response, res); + } catch (e) {} + + Object.keys(res.headers).forEach(function (key) { + response.setHeader(key, res.headers[key]); + }); + response.writeHead(response.statusCode); }); + + /* + // + // Handle 'error' events from the `reverseProxy`. Setup timeout override if needed + // + self.request.once('error', proxyError); + + // Set a timeout on the socket if `this.timeout` is specified. + reverseProxy.once('socket', function (socket) { + if (self.timeout) { + socket.setTimeout(self.timeout); + } + }); */ + +/* + + // + // #### function proxyError (err) + // #### @err {Error} Error contacting the proxy target + // Short-circuits `res` in the event of any error when + // contacting the proxy target at `host` / `port`. + // + function proxyError(err) { + errState = true; + + // + // Emit an `error` event, allowing the application to use custom + // error handling. The error handler should end the response. + // + if (self.emit('proxyError', err, req, res)) { + return; + } + + res.writeHead(500, { 'Content-Type': 'text/plain' }); + + if (req.method !== 'HEAD') { + // + // This NODE_ENV=production behavior is mimics Express and + // Connect. + // + res.write(process.env.NODE_ENV === 'production' + ? 'Internal Server Error' + : 'An error has occurred: ' + JSON.stringify(err) + ); + } + + try { res.end() } + catch (ex) { console.error("res.end error: %s", ex.message) } + } + */ }); }; @@ -45,4 +114,11 @@ ForwardStream.prototype._write = function(chunk, encoding, callback) { this.request.write(chunk, encoding, callback); }; +ForwardStream.prototype._read = function(size) { + var chunk = self.request.read(); + if(chunk !== null) { + this.push(chunk); + } +}; + util.inherits(ForwardStream, Duplex); From 08293f96bc319f3c9850a279d5e4a1e11b3c62ac Mon Sep 17 00:00:00 2001 From: cronopio Date: Wed, 10 Jul 2013 12:25:15 -0500 Subject: [PATCH 07/14] [fix] use correct name, make example tests runnable --- lib/node-http-proxy/streams/proxy.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/node-http-proxy/streams/proxy.js b/lib/node-http-proxy/streams/proxy.js index 08dc2fdb7..aa5573c1a 100644 --- a/lib/node-http-proxy/streams/proxy.js +++ b/lib/node-http-proxy/streams/proxy.js @@ -110,15 +110,15 @@ var ProxyStream = module.exports = function ProxyStream(response, options) { }; -ForwardStream.prototype._write = function(chunk, encoding, callback) { +ProxyStream.prototype._write = function(chunk, encoding, callback) { this.request.write(chunk, encoding, callback); }; -ForwardStream.prototype._read = function(size) { +ProxyStream.prototype._read = function(size) { var chunk = self.request.read(); if(chunk !== null) { this.push(chunk); } }; -util.inherits(ForwardStream, Duplex); +util.inherits(ProxyStream, Duplex); From 64b4e07ffbcd5581b3f6d7f7839f0d28d9c742d7 Mon Sep 17 00:00:00 2001 From: cronopio Date: Thu, 11 Jul 2013 14:07:38 -0500 Subject: [PATCH 08/14] [fix] maybe another typo --- lib/node-http-proxy/http-proxy.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node-http-proxy/http-proxy.js b/lib/node-http-proxy/http-proxy.js index f76187889..379e9dddb 100644 --- a/lib/node-http-proxy/http-proxy.js +++ b/lib/node-http-proxy/http-proxy.js @@ -164,7 +164,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) { // Emit the `start` event indicating that we have begun the proxy operation. // this.emit('start', req, res, this.target); - req.pipe(new ProxyStream(res, options)).pipe(res); + req.pipe(new ProxyStream(res, this)).pipe(res); // // If forwarding is enabled for this instance, foward proxy the From 1a4da5d76e746209d086ee464552aadc60c4fde8 Mon Sep 17 00:00:00 2001 From: yawnt Date: Thu, 25 Jul 2013 21:19:15 +0200 Subject: [PATCH 09/14] [fix] started fixing stuff --- lib/node-http-proxy/streams/proxy.js | 33 ++++++++++++++++++---------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/node-http-proxy/streams/proxy.js b/lib/node-http-proxy/streams/proxy.js index aa5573c1a..8235e9537 100644 --- a/lib/node-http-proxy/streams/proxy.js +++ b/lib/node-http-proxy/streams/proxy.js @@ -8,9 +8,11 @@ var Duplex = require('stream').Duplex, var ProxyStream = module.exports = function ProxyStream(response, options) { Duplex.call(this); - var self = this, - target = options.target, - source = options.source; + var self = this, + target = options.target, + source = options.source; + + self.origRes = response; this.once('pipe', function(req) { var protocol = target.https ? https : http, @@ -21,8 +23,13 @@ var ProxyStream = module.exports = function ProxyStream(response, options) { if (options.changeOrigin) { outgoing.headers.host = target.host + ':' + target.port; } - - self.request = protocol.request(outgoing, function(res) { + + self.request = protocol.request(outgoing); + self.request.end(); + + self.request.on('response', function (res) { + console.log('yarr yarr'); + self.response = res; if(req.httpVersion === '1.0') { res.headers.connection = req.headers.connection || 'close'; } @@ -56,7 +63,8 @@ var ProxyStream = module.exports = function ProxyStream(response, options) { }); response.writeHead(response.statusCode); }); - + + /* // // Handle 'error' events from the `reverseProxy`. Setup timeout override if needed @@ -110,15 +118,16 @@ var ProxyStream = module.exports = function ProxyStream(response, options) { }; +ProxyStream.prototype = Object.create( + Duplex.prototype, { constructor: { value: ProxyStream } } +); + ProxyStream.prototype._write = function(chunk, encoding, callback) { this.request.write(chunk, encoding, callback); }; ProxyStream.prototype._read = function(size) { - var chunk = self.request.read(); - if(chunk !== null) { - this.push(chunk); - } + var chunk = this.response ? this.response.read(size) : ''; + console.log(chunk.toString()); + this.push(chunk); }; - -util.inherits(ProxyStream, Duplex); From 10c218b8042f61c6b68401cf4217808ba9b9bfdf Mon Sep 17 00:00:00 2001 From: yawnt Date: Thu, 25 Jul 2013 22:22:20 +0200 Subject: [PATCH 10/14] [fix] fixed that f..reaking bug --- lib/node-http-proxy/.http-proxy.js.swp | Bin 0 -> 36864 bytes lib/node-http-proxy/streams/proxy.js | 18 +++++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 lib/node-http-proxy/.http-proxy.js.swp diff --git a/lib/node-http-proxy/.http-proxy.js.swp b/lib/node-http-proxy/.http-proxy.js.swp new file mode 100644 index 0000000000000000000000000000000000000000..f3ed579f8917a029332031333a814c80584e9569 GIT binary patch literal 36864 zcmeI54|HT#ec#6ajA{gTcg!VmatY58m-qG+wnY&o}}@P zX2$d0tR&VlB%G8c5KaS0)7Ay<2QPXq?!E8NXjZ#%&gn^-nX})WdGFo7zx%uQ{{G#km9d*bDw=XTcneIHxb*bULFX{RIes>+X@6Dd)&vw^$xbM&R zJb#Y6zRP`I>niZ|Tk%&!6k=zukQwbPe|Tzt7$G*B|bA?j8@m1{4@jU_gNZ z1qKutP+&lT0R;vW7*Jq9fdK^u6!?2dfnc>z_y_d-Yoa*g|M&X;cP}p#{s8<(unX=2 z{}6oZg@wX>U=17qUw%QM@Cop7a1Oi<;ASlRQv_4crP&gPXx& z@O<#ZWrf1;fu9EfD1%Wj44wnN@Vr9do!}U_9$W=V;1%F9@O2CXUj&~4{}#L(+y&ZT z4b;FecsBTLj13K(972svy z#o+VUHGUS%fM#O0a zX?0Az5t>#z>;#g;Y}FeLvli4F{E9bB&@|Onr@mfqn$3E&8N{`Xsx2jGu9|8T)<(C& zm8exa6UL)9VbyGe!D`rv$k~=MqjJ>hc4}eO3=f|;Sy>()wk1f#r^1zrW;PoX7;OY+ zLKC-4jjMHjk)o##f={%et`(Iq?zT;^x~d%v8uUb~G;8!MEnI_5>dp1CIkgcsI4qHAPv z$hOavv^sa!wjrB54m#^$EU7c4HP&u96YktrUJ0Vm$mgh$pd78Cd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 100644 --- a/lib/node-http-proxy/streams/proxy.js +++ b/lib/node-http-proxy/streams/proxy.js @@ -11,8 +11,6 @@ var ProxyStream = module.exports = function ProxyStream(response, options) { var self = this, target = options.target, source = options.source; - - self.origRes = response; this.once('pipe', function(req) { var protocol = target.https ? https : http, @@ -25,10 +23,11 @@ var ProxyStream = module.exports = function ProxyStream(response, options) { } self.request = protocol.request(outgoing); - self.request.end(); + self.on('finish', function() { + self.request.end(); + }); self.request.on('response', function (res) { - console.log('yarr yarr'); self.response = res; if(req.httpVersion === '1.0') { res.headers.connection = req.headers.connection || 'close'; @@ -62,6 +61,15 @@ var ProxyStream = module.exports = function ProxyStream(response, options) { response.setHeader(key, res.headers[key]); }); response.writeHead(response.statusCode); + + res.on('readable', function() { + self.read(0); + }); + + res.on('end', function() { + self.push(null); + }); + self.emit('readable'); }); @@ -128,6 +136,6 @@ ProxyStream.prototype._write = function(chunk, encoding, callback) { ProxyStream.prototype._read = function(size) { var chunk = this.response ? this.response.read(size) : ''; - console.log(chunk.toString()); + if(chunk == null) { chunk = '' } this.push(chunk); }; From 3553b1ec8a7e60f4a9cdabaf6d94e66c11bfd4e3 Mon Sep 17 00:00:00 2001 From: yawnt Date: Mon, 29 Jul 2013 18:09:06 +0200 Subject: [PATCH 11/14] [fix] error --- .gitignore | 1 + lib/node-http-proxy/.http-proxy.js.swp | Bin 36864 -> 0 bytes lib/node-http-proxy/streams/forward.js | 7 ++++++- test/http/http-test.js | 10 +++++----- 4 files changed, 12 insertions(+), 6 deletions(-) delete mode 100644 lib/node-http-proxy/.http-proxy.js.swp diff --git a/.gitignore b/.gitignore index 468b525c9..967876b80 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ config.json node_modules/ npm-debug.log +*.swp diff --git a/lib/node-http-proxy/.http-proxy.js.swp b/lib/node-http-proxy/.http-proxy.js.swp deleted file mode 100644 index f3ed579f8917a029332031333a814c80584e9569..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 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1w~Qs>R$QW|~o+@$AWr|o7) zoR%a?Ua(T7nyPS$-1cXd)8|sRJrPX0XL=0x_1KWk%4v;cv+eD-Z2d#h)%*tTmwx}9 zE*W+ptX7h4?e?zDUK{u1{KnBO!V$NXq4ty`H+xGLz0eza=|d?v*LlRN1c`nq4x^pph@Cz{0J#rElobkOXFB+H6Vb7$a=7n)a_s z$QZIv`QR2Eb@l(k9N5R47R^e+FZ=#-odPoO6A~mAxxr5B(TYpifhkxLkGnVj?2QY& z-amo)^nj@5aDv|fc(o#LI^w2KFFkvn&Q)w4>AO)_)nt`J{IW^;A^WeWvQm1Ycgk$F zOyX)%dQRzqIZhtR#PuSIb2i!3p0RRT*zIC5#O&v!NNK37g<{*1Z)YgB!9kuEas^n? zLDIS+bRE)#`sSN&wxOmB6l?hoM>rWioxBlmzi-e{rb&I9j>3Er_=F0byhJK3ryd{S zDN8;Hg=baLIb^BgHzZlb_4O1wu?JW2S#b5J)*i9mM^>_MP1B_?pRkYR+3e! z6qeV^rn(r&A4t)`t!9Gdazmg3{@=99v|}#hOxethsTW=}7NnvUs`JHlB)YfD>X_Z| yq3g&z^7kFoGN1TSlVBj3omporeB3C{C@$3gS&|U diff --git a/lib/node-http-proxy/streams/forward.js b/lib/node-http-proxy/streams/forward.js index de49bf6af..c771a8902 100644 --- a/lib/node-http-proxy/streams/forward.js +++ b/lib/node-http-proxy/streams/forward.js @@ -14,9 +14,14 @@ var ForwardStream = module.exports = function ForwardStream(options) { outgoing = proxy._getBase(options); proxy._setupOutgoing(outgoing, options, req); - + // pipe throw-safe? do we need to add a ` on 'error' ` handler? self.request = protocol.request(outgoing, function() {}); + self.request.on('error', function() { }); + + self.on('finish', function() { + self.request.end(); + }); }); }; diff --git a/test/http/http-test.js b/test/http/http-test.js index 81f8726a7..65aacba38 100644 --- a/test/http/http-test.js +++ b/test/http/http-test.js @@ -38,7 +38,7 @@ var routeFile = path.join(__dirname, 'config.json'); vows.describe(helpers.describe()).addBatch({ "With a valid target server": { "and no latency": { - "and no headers": macros.http.assertProxied(), + /*"and no headers": macros.http.assertProxied(), "and headers": macros.http.assertProxied({ request: { headers: { host: 'unknown.com' } } }), @@ -73,8 +73,8 @@ vows.describe(helpers.describe()).addBatch({ "and no connection header": macros.http.assertProxied({ request: { headers: { connection: "" } }, // Must explicitly set to "" because otherwise node will automatically add a "connection: keep-alive" header outputHeaders: { connection: "keep-alive" } - }), - "and forwarding enabled": macros.http.assertForwardProxied() + }),*/ + "and forwarding enabled": macros.http.assertForwardProxied() /* }, "and latency": { "and no headers": macros.http.assertProxied({ @@ -92,11 +92,11 @@ vows.describe(helpers.describe()).addBatch({ timeout: 2000, requestLatency: 4000 }) - }, + */}/*, "With a no valid target server": { "and no latency": macros.http.assertInvalidProxy(), "and latency": macros.http.assertInvalidProxy({ latency: 2000 - }) + })*/ } }).export(module); From 605a6b494857524b01139f2d58c6bd51ddad0b16 Mon Sep 17 00:00:00 2001 From: yawnt Date: Mon, 29 Jul 2013 18:11:58 +0200 Subject: [PATCH 12/14] [minor] remove comments --- test/http/http-test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/http/http-test.js b/test/http/http-test.js index 65aacba38..81f8726a7 100644 --- a/test/http/http-test.js +++ b/test/http/http-test.js @@ -38,7 +38,7 @@ var routeFile = path.join(__dirname, 'config.json'); vows.describe(helpers.describe()).addBatch({ "With a valid target server": { "and no latency": { - /*"and no headers": macros.http.assertProxied(), + "and no headers": macros.http.assertProxied(), "and headers": macros.http.assertProxied({ request: { headers: { host: 'unknown.com' } } }), @@ -73,8 +73,8 @@ vows.describe(helpers.describe()).addBatch({ "and no connection header": macros.http.assertProxied({ request: { headers: { connection: "" } }, // Must explicitly set to "" because otherwise node will automatically add a "connection: keep-alive" header outputHeaders: { connection: "keep-alive" } - }),*/ - "and forwarding enabled": macros.http.assertForwardProxied() /* + }), + "and forwarding enabled": macros.http.assertForwardProxied() }, "and latency": { "and no headers": macros.http.assertProxied({ @@ -92,11 +92,11 @@ vows.describe(helpers.describe()).addBatch({ timeout: 2000, requestLatency: 4000 }) - */}/*, + }, "With a no valid target server": { "and no latency": macros.http.assertInvalidProxy(), "and latency": macros.http.assertInvalidProxy({ latency: 2000 - })*/ + }) } }).export(module); From 52defcb3f26258be6e85ec1a90bfc161a4fa8967 Mon Sep 17 00:00:00 2001 From: cronopio Date: Mon, 29 Jul 2013 16:32:27 -0500 Subject: [PATCH 13/14] [fix] hack to call .end() explicitly, enable error handler, at this point the test/http/http-test.js all passed --- lib/node-http-proxy/streams/proxy.js | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/node-http-proxy/streams/proxy.js b/lib/node-http-proxy/streams/proxy.js index f8d12bb3e..8f0e0227e 100644 --- a/lib/node-http-proxy/streams/proxy.js +++ b/lib/node-http-proxy/streams/proxy.js @@ -72,13 +72,16 @@ var ProxyStream = module.exports = function ProxyStream(response, options) { self.emit('readable'); }); + // HACK: please fix it + // Should we call end() splicity? + self.end(); - /* + // // Handle 'error' events from the `reverseProxy`. Setup timeout override if needed // self.request.once('error', proxyError); - +/* // Set a timeout on the socket if `this.timeout` is specified. reverseProxy.once('socket', function (socket) { if (self.timeout) { @@ -86,8 +89,6 @@ var ProxyStream = module.exports = function ProxyStream(response, options) { } }); */ -/* - // // #### function proxyError (err) // #### @err {Error} Error contacting the proxy target @@ -95,33 +96,29 @@ var ProxyStream = module.exports = function ProxyStream(response, options) { // contacting the proxy target at `host` / `port`. // function proxyError(err) { - errState = true; - // // Emit an `error` event, allowing the application to use custom // error handling. The error handler should end the response. // - if (self.emit('proxyError', err, req, res)) { - return; - } - - res.writeHead(500, { 'Content-Type': 'text/plain' }); + // if (self.emit('proxyError', err, req, res)) { + // return; + // } - if (req.method !== 'HEAD') { + response.writeHead(500, { 'Content-Type': 'text/plain' }); + if (self.request.method !== 'HEAD') { // // This NODE_ENV=production behavior is mimics Express and // Connect. // - res.write(process.env.NODE_ENV === 'production' + response.write(process.env.NODE_ENV === 'production' ? 'Internal Server Error' : 'An error has occurred: ' + JSON.stringify(err) ); } - try { res.end() } + try { response.end() } catch (ex) { console.error("res.end error: %s", ex.message) } } - */ }); }; From 77f3723bb9bf40f61c074acf88d19016257495b2 Mon Sep 17 00:00:00 2001 From: cronopio Date: Mon, 29 Jul 2013 17:11:14 -0500 Subject: [PATCH 14/14] [fix] getting rid of try..catch, as @mmalecki suggested --- lib/node-http-proxy/streams/proxy.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/node-http-proxy/streams/proxy.js b/lib/node-http-proxy/streams/proxy.js index 8f0e0227e..63b6a9c1d 100644 --- a/lib/node-http-proxy/streams/proxy.js +++ b/lib/node-http-proxy/streams/proxy.js @@ -53,9 +53,7 @@ var ProxyStream = module.exports = function ProxyStream(response, options) { } } - try { - self.emit('proxyResponse', req, response, res); - } catch (e) {} + self.emit('proxyResponse', req, response, res); Object.keys(res.headers).forEach(function (key) { response.setHeader(key, res.headers[key]);