[fix] default port

This commit is contained in:
yawnt 2013-09-15 13:18:21 +02:00
parent 18341d5597
commit d1663549ec
3 changed files with 4 additions and 193 deletions

View File

@ -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]; }
);

View File

@ -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);
};

View File

@ -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);
};