Skip to content

Commit 356f43d

Browse files
committed
[fix] ProxyStraem now works
1 parent d4f0da8 commit 356f43d

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

lib/caronte/streams/proxy.js

+41-41
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ ProxyStream.prototype.onPipe = function(req) {
2828
);
2929
//console.log(common.setupOutgoing(self.options.ssl || {}, self.options, req));
3030
this.proxyReq.once('response', function(proxyRes) {
31-
console.log(proxyRes);
3231
self.onResponse(proxyRes);
3332
});
3433
this.proxyReq.on('error', function(e) {
@@ -43,46 +42,47 @@ ProxyStream.prototype.onFinish = function() {
4342
ProxyStream.prototype.onResponse = function(proxyRes) {
4443
this.proxyRes = proxyRes;
4544

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

8888
ProxyStream.prototype.onError = function(e) {

0 commit comments

Comments
 (0)