From d1663549ec070e7ae8bc45ffb148f40ee903192f Mon Sep 17 00:00:00 2001 From: yawnt Date: Sun, 15 Sep 2013 13:18:21 +0200 Subject: [PATCH] [fix] default port --- lib/caronte/common.js | 5 +- lib/caronte/streams/forward.js | 88 ---------------------------- lib/caronte/streams/proxy.js | 104 --------------------------------- 3 files changed, 4 insertions(+), 193 deletions(-) delete mode 100644 lib/caronte/streams/forward.js delete mode 100644 lib/caronte/streams/proxy.js diff --git a/lib/caronte/common.js b/lib/caronte/common.js index 2a2954bb5..6b75c6f47 100644 --- a/lib/caronte/common.js +++ b/lib/caronte/common.js @@ -21,7 +21,10 @@ var common = exports; */ common.setupOutgoing = function(outgoing, options, req, forward) { - ['host', 'hostname', 'port', 'socketPath', 'agent'].forEach( + outgoing.port = options[forward || 'target'].port || + (~['https:', 'wss:'].indexOf(options[forward || 'target'].protocol) ? 443 : 80); + + ['host', 'hostname', 'socketPath', 'agent'].forEach( function(e) { outgoing[e] = options[forward || 'target'][e]; } ); diff --git a/lib/caronte/streams/forward.js b/lib/caronte/streams/forward.js deleted file mode 100644 index b1bb58f3b..000000000 --- a/lib/caronte/streams/forward.js +++ /dev/null @@ -1,88 +0,0 @@ -var Writable = require('stream').Writable, - common = require('../common'), - http = require('http'), - https = require('https'); - -module.exports = ForwardStream; - -/** - * Forwards the request to the external target specified in options - * - * Examples: - * - * new ForwardStream(options) - * // => { ... } - * - * @param {Object} Options Config object passed to the proxy - *  - * @return {ForwardStream} Stream A clone of ForwardStream - * - * @api private - */ - -function ForwardStream(options) { - var self = this; - - self.options = options; - // To uncomment the line below, please see - // https://github.com/yawnt/caronte/commit/9ab8749a9bec33b49c495975e8364336ad7be1a3#commitcomment-3947117 - //self.res = res; - - Writable.call(this); - - this.once('pipe', function(pipe) { self.onPipe(pipe) }); - this.once('finish', function() { self.onFinish() }); -} - -require('util').inherits(ForwardStream, Writable); - -/** - * Fires up the request to the external target - * - * Examples: - * - * (new ForwardStream(options)).onPipe(req) - * // => undefined - * - * @param {HttpRequest} Req Request object - * - * @api private - */ - -ForwardStream.prototype.onPipe = function(request) { - this.forwardReq = (this.options.ssl ? https : http).request( - common.setupOutgoing(this.options.ssl || {}, this.options, request, 'forward') - ); - - this.forwardReq.on('error', function() {}); /** Fire and forget */ -}; - -/** - * Closes forwarded request when `pipe` is finished. - * - * Examples: - * - * (new ForwardStream(options)).onFinish() - * // => undefined - * - * @api private - */ - -ForwardStream.prototype.onFinish = function() { - this.forwardReq.end(); -}; - -/** - * Implements `stream.Writable`, writes to the forwarded request - * - * Examples: - * - * (new ForwardStream(options))._write(chunk, encoding, clb) - * // => undefined - * - * @api private - */ - -ForwardStream.prototype._write = function(chunk, encoding, clb) { - this.forwardReq.write(chunk, encoding, clb); -}; diff --git a/lib/caronte/streams/proxy.js b/lib/caronte/streams/proxy.js deleted file mode 100644 index 29c2fa78f..000000000 --- a/lib/caronte/streams/proxy.js +++ /dev/null @@ -1,104 +0,0 @@ -var Duplex = require('stream').Duplex, - common = require('../common'), - http = require('http'), - https = require('https'); - -module.exports = ProxyStream; - -function ProxyStream(options, res) { - Duplex.call(this); - - this.options = options; - this.res = res; - - var self = this; - - this.once('pipe', function(pipe) { self.onPipe(pipe); }); - this.once('finish', function() { self.onFinish(); }); -} - -require('util').inherits(ProxyStream, Duplex); - -ProxyStream.prototype.onPipe = function(req) { - this.req = req; - - var self = this; - - this.proxyReq = (self.options.ssl ? https : http).request( - common.setupOutgoing(self.options.ssl || {}, self.options, req) - ); - //console.log(common.setupOutgoing(self.options.ssl || {}, self.options, req)); - this.proxyReq.once('response', function(proxyRes) { - self.onResponse(proxyRes); - }); - this.proxyReq.on('error', function(e) { - self.onError(e); - }); -}; - -ProxyStream.prototype.onFinish = function() { - this.proxyReq.end(); -}; - -ProxyStream.prototype.onResponse = function(proxyRes) { - this.proxyRes = proxyRes; - - var self = this; - - if(this.req.httpVersion === '1.0') { - proxyRes.headers.connection = this.req.headers.connection || 'close'; - } - else if(!proxyRes.headers.connection) { - proxyRes.headers.connection = this.req.headers.connection || 'keep-alive'; - } - - if(this.req.httpVersion === '1.0' || (this.req.method === 'DELETE' && !this.req.headers['content-length'])) { - delete proxyRes.headers['transfer-encoding']; - } - - /*if(~[301,302].indexOf(this.res.statusCode) && typeof this.res.headers.location !== 'undefined') { - var location = url.parse(this.res.headers.location); - if ( - location.host === this.req.headers.host && - ( - source.https && !target.https || - target.https && !source.https - ) - ) { - this.res.headers.location = this.res.headers.location.replace(/^https\:/, 'http:'); - } - }*/ - - Object.keys(proxyRes.headers).forEach(function (key) { - self.res.setHeader(key, proxyRes.headers[key]); - }); - - this.res.writeHead(proxyRes.statusCode); - - proxyRes.on('readable', function() { - self.read(0); - }); - - proxyRes.on('end', function() { - self.push(null); - }); - - self.emit('readable'); -}; - -ProxyStream.prototype.onError = function(e) { - if(this.options.ee.emit('proxyError', this.req, this.res, e)) return; - - this.res.writeHead(500, { 'Content-Type': 'text/plain' }); - this.res.end('Internal Server Error'); -}; - -ProxyStream.prototype._write = function(chunk, encoding, callback) { - this.proxyReq.write(chunk, encoding, callback); -}; - -ProxyStream.prototype._read = function(size) { - var chunk = (this.proxyRes ? this.proxyRes.read(size) : '') || ''; - - this.push(chunk); -};