Skip to content

Commit

Permalink
[fix] ProxyStraem now works
Browse files Browse the repository at this point in the history
  • Loading branch information
yawnt committed Aug 20, 2013
1 parent d4f0da8 commit 356f43d
Showing 1 changed file with 41 additions and 41 deletions.
82 changes: 41 additions & 41 deletions lib/caronte/streams/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ ProxyStream.prototype.onPipe = function(req) {
);
//console.log(common.setupOutgoing(self.options.ssl || {}, self.options, req));
this.proxyReq.once('response', function(proxyRes) {
console.log(proxyRes);
self.onResponse(proxyRes);
});
this.proxyReq.on('error', function(e) {
Expand All @@ -43,46 +42,47 @@ ProxyStream.prototype.onFinish = function() {
ProxyStream.prototype.onResponse = function(proxyRes) {
this.proxyRes = proxyRes;

// rewrite
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 res.headers['transfer-encoding'];
}

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:');
}
}

self.emit('proxyResponse', req, response, res);

Object.keys(res.headers).forEach(function (key) {
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');
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) {
Expand Down

0 comments on commit 356f43d

Please sign in to comment.