Skip to content

Commit d4f0da8

Browse files
committed
[fix] some stuff start debugging proxystream
1 parent 9ab8749 commit d4f0da8

File tree

5 files changed

+68
-17
lines changed

5 files changed

+68
-17
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
*.swp
33
cov
4+
ttest.js

lib/caronte.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ proxy.createProxyServer = function createProxyServer(options) {
3131
" ssl : <object to be passed to https.createServer()> ",
3232
" ws : <true/false, if you want to proxy websockets> ",
3333
" xfwd : <true/false, adds x-forward headers> ",
34+
" maxSock: <maximum number of sockets> ",
3435
" } ",
3536
" ",
3637
"NOTE: `options.ws` and `options.ssl` are optional. ",
@@ -42,19 +43,24 @@ proxy.createProxyServer = function createProxyServer(options) {
4243
['target', 'forward'].forEach(function(key) {
4344
if(!options[key]) return;
4445
options[key] = url.parse(options[key]);
46+
47+
options[key].maxSockets = options.maxSock;
48+
options[key].agent = new (options.ssl ? https.Agent : http.Agent)(options[key]);
4549
});
4650

4751
return {
4852
__proto__: new events.EventEmitter2({ wildcard: true, delimiter: ':' }),
4953
web : caronte.createWebProxy(options),
5054
ws : caronte.createWsProxy(options),
5155
listen : function listen(port) {
52-
var server = options.ssl ? http.createServer(this.web) : https.createServer(options.ssl, this.web);
56+
var server = options.ssl ? https.createServer(options.ssl, this.web) : http.createServer(this.web);
5357

5458
if(options.ws) {
5559
server.on('upgrade', this.ws);
5660
}
5761

62+
server.listen(port);
63+
5864
return server;
5965
}
6066
};

lib/caronte/common.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ var common = exports;
2020
*/
2121

2222
common.setupOutgoing = function(outgoing, options, req) {
23-
['host', 'hostname', 'port', 'socketPath', 'agent'].forEach(
24-
function(e) { outgoing[e] = options[e]; }
23+
['host', 'hostname', 'port', 'socketPath'/*, 'agent'*/].forEach(
24+
function(e) { outgoing[e] = options.target[e]; }
2525
);
2626

2727
['method', 'path', 'headers'].forEach(

lib/caronte/index.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ caronte.createWsProxy = createRightProxy('ws');
2222
*/
2323

2424
function createRightProxy(type) {
25-
passes = type === 'ws' ? ws : web;
25+
var passes = (type === 'ws') ? ws : web;
26+
2627
return function(options) {
2728

2829
passes = Object.keys(passes).map(function(pass) {
@@ -33,17 +34,17 @@ function createRightProxy(type) {
3334
var self = this,
3435
ev = 'caronte:' + type + ':';
3536

36-
self.emit(ev + 'begin', req, res);
37+
//self.emit(ev + 'begin', req, res);
3738

3839
passes.forEach(function(pass) {
39-
var event = ev + pass.name.toLowerCase();
40+
var evnt = ev + pass.name.toLowerCase();
4041

41-
self.emit(event + 'begin', req, res);
42+
//self.emit(evnt + 'begin', req, res);
4243
pass(req, res, options, self);
43-
self.emit(event + 'end');
44+
//self.emit(evnt + 'end');
4445
});
4546

46-
self.emit(ev + 'end');
47+
//self.emit(ev + 'end');
4748
};
4849
};
4950
}

lib/caronte/streams/proxy.js

+51-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ var Duplex = require('stream').Duplex,
44
https = require('https');
55

66
function ProxyStream(options, res, instance) {
7+
Duplex.call(this);
8+
79
this.options = options;
810
this.res = res;
911
this.instance = instance;
1012

1113
var self = this;
1214

13-
Duplex.call(this);
14-
1515
this.once('pipe', function(pipe) { self.onPipe(pipe); });
1616
this.once('finish', function() { self.onFinish(); });
1717
}
@@ -23,11 +23,12 @@ ProxyStream.prototype.onPipe = function(req) {
2323

2424
var self = this;
2525

26-
this.proxyReq = (options.ssl ? https : http).request(
27-
common.setupOutgoing(options.ssl || {}, options, req)
26+
this.proxyReq = (self.options.ssl ? https : http).request(
27+
common.setupOutgoing(self.options.ssl || {}, self.options, req)
2828
);
29-
29+
//console.log(common.setupOutgoing(self.options.ssl || {}, self.options, req));
3030
this.proxyReq.once('response', function(proxyRes) {
31+
console.log(proxyRes);
3132
self.onResponse(proxyRes);
3233
});
3334
this.proxyReq.on('error', function(e) {
@@ -36,16 +37,57 @@ ProxyStream.prototype.onPipe = function(req) {
3637
};
3738

3839
ProxyStream.prototype.onFinish = function() {
39-
40+
this.proxyReq.end();
4041
};
4142

4243
ProxyStream.prototype.onResponse = function(proxyRes) {
4344
this.proxyRes = proxyRes;
45+
46+
// rewrite
47+
if(req.httpVersion === '1.0') {
48+
res.headers.connection = req.headers.connection || 'close';
49+
}
50+
else if(!res.headers.connection) {
51+
res.headers.connection = req.headers.connection || 'keep-alive';
52+
}
53+
54+
if(req.httpVersion === '1.0' || (req.method === 'DELETE' && !req.headers['content-length'])) {
55+
delete res.headers['transfer-encoding'];
56+
}
57+
58+
if(~[301,302].indexOf(res.statusCode) && typeof res.headers.location !== 'undefined') {
59+
var location = url.parse(res.headers.location);
60+
if (
61+
location.host === req.headers.host &&
62+
(
63+
source.https && !target.https ||
64+
target.https && !source.https
65+
)
66+
) {
67+
res.headers.location = res.headers.location.replace(/^https\:/, 'http:');
68+
}
69+
}
70+
71+
self.emit('proxyResponse', req, response, res);
72+
73+
Object.keys(res.headers).forEach(function (key) {
74+
response.setHeader(key, res.headers[key]);
75+
});
76+
response.writeHead(response.statusCode);
77+
78+
res.on('readable', function() {
79+
self.read(0);
80+
});
81+
82+
res.on('end', function() {
83+
self.push(null);
84+
});
85+
self.emit('readable');
4486
};
4587

4688
ProxyStream.prototype.onError = function(e) {
47-
if(this.instance.emit('error', this.req, this.res, e)) return;
48-
89+
if(this.instance.emit('proxyError', this.req, this.res, e)) return;
90+
4991
this.res.writeHead(500, { 'Content-Type': 'text/plain' });
5092
this.res.end('Internal Server Error');
5193
};
@@ -60,3 +102,4 @@ ProxyStream.prototype._read = function(size) {
60102
this.push(chunk);
61103
};
62104

105+
module.exports = ProxyStream;

0 commit comments

Comments
 (0)