Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
85 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,18 +5,20 @@ var Duplex = require('stream').Duplex, | |
url = require('url'), | ||
util = require('util'); | ||
|
||
var ProxyStream = module.exports = function ProxyStream(target, changeOrigin) { | ||
var ProxyStream = module.exports = function ProxyStream(response, options) { | ||
Duplex.call(this); | ||
|
||
var self = this; | ||
|
||
var self = this, | ||
target = options.target, | ||
source = options.source; | ||
|
||
this.once('pipe', function(req) { | ||
var protocol = target.https ? https : http, | ||
outgoing = proxy._getBase(target); | ||
|
||
proxy._setupOutgoing(outgoing, target, req); | ||
|
||
if (changeOrigin) { | ||
if (options.changeOrigin) { | ||
outgoing.headers.host = target.host + ':' + target.port; | ||
} | ||
|
||
|
@@ -29,14 +31,81 @@ var ProxyStream = module.exports = function ProxyStream(target, changeOrigin) { | |
} | ||
|
||
if(req.httpVersion === '1.0' || (req.method === 'DELETE' && !req.headers['content-length'])) { | ||
delete response.headers['transfer-encoding']; | ||
delete res.headers['transfer-encoding']; | ||
} | ||
|
||
if(~[301,302].indexOf(res.statusCode) && typeof response.headers.location !== 'undefined') { | ||
location = url.parse(response.headers.location); | ||
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:'); | ||
} | ||
} | ||
|
||
try { | ||
This comment has been minimized.
Sorry, something went wrong.
mmalecki
Contributor
|
||
self.emit('proxyResponse', req, response, res); | ||
} catch (e) {} | ||
|
||
Object.keys(res.headers).forEach(function (key) { | ||
response.setHeader(key, res.headers[key]); | ||
}); | ||
response.writeHead(response.statusCode); | ||
}); | ||
|
||
/* | ||
// | ||
// Handle 'error' events from the `reverseProxy`. Setup timeout override if needed | ||
// | ||
self.request.once('error', proxyError); | ||
// Set a timeout on the socket if `this.timeout` is specified. | ||
reverseProxy.once('socket', function (socket) { | ||
if (self.timeout) { | ||
socket.setTimeout(self.timeout); | ||
} | ||
}); */ | ||
|
||
/* | ||
// | ||
// #### function proxyError (err) | ||
// #### @err {Error} Error contacting the proxy target | ||
// Short-circuits `res` in the event of any error when | ||
// contacting the proxy target at `host` / `port`. | ||
// | ||
function proxyError(err) { | ||
errState = true; | ||
// | ||
// Emit an `error` event, allowing the application to use custom | ||
// error handling. The error handler should end the response. | ||
// | ||
if (self.emit('proxyError', err, req, res)) { | ||
return; | ||
} | ||
res.writeHead(500, { 'Content-Type': 'text/plain' }); | ||
if (req.method !== 'HEAD') { | ||
// | ||
// This NODE_ENV=production behavior is mimics Express and | ||
// Connect. | ||
// | ||
res.write(process.env.NODE_ENV === 'production' | ||
? 'Internal Server Error' | ||
: 'An error has occurred: ' + JSON.stringify(err) | ||
); | ||
} | ||
try { res.end() } | ||
catch (ex) { console.error("res.end error: %s", ex.message) } | ||
} | ||
*/ | ||
}); | ||
|
||
}; | ||
|
@@ -45,4 +114,11 @@ ForwardStream.prototype._write = function(chunk, encoding, callback) { | |
this.request.write(chunk, encoding, callback); | ||
}; | ||
|
||
ForwardStream.prototype._read = function(size) { | ||
var chunk = self.request.read(); | ||
if(chunk !== null) { | ||
this.push(chunk); | ||
} | ||
}; | ||
|
||
util.inherits(ForwardStream, Duplex); |
Here the
options
var is undefined, I just change it tothis
because there is the field calledsource
andtarget
required byProxyStream()
and now Im seeing a problem in the options passed to request. More info here 44097a8#commitcomment-3615874