Skip to content

Commit

Permalink
[refactor] core proxy logic. all tests should be passing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marak committed Oct 25, 2011
1 parent cdb4524 commit 63ac925
Showing 1 changed file with 16 additions and 38 deletions.
54 changes: 16 additions & 38 deletions lib/node-http-proxy/http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,46 +218,24 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
return;
}

//
// For each data `chunk` received from the `reverseProxy`
// `response` write it to the outgoing `res`.
// If the res socket has been killed already, then write()
// will throw. Nevertheless, try our best to end it nicely.
//
var paused = false;
response.on('data', function (chunk) {
if (req.method !== 'HEAD' && res.writable) {
try {
var flushed = res.write(chunk);
}
catch (ex) {
console.error("res.write error: %s", ex.message);

try { res.end() }
catch (ex) { console.error("res.end error: %s", ex.message) }

return;
}

if (!flushed && !paused) {
paused = true;
response.pause();
res.once('drain', function () {
paused = false;
try { response.resume() }
catch (er) { console.error("response.resume error: %s", er.message) }
});

//
// Force the `drain` event in 100ms if it hasn't
// happened on its own.
//
setTimeout(function () {
res.emit('drain');
}, 100);

function ondata(chunk) {
if (res.writable) {
if (false === res.write(chunk) && response.pause) {
response.pause();
}
}
}
});

response.on('data', ondata);

function ondrain() {
if (response.readable && response.resume) {
response.resume();
}
}

res.on('drain', ondrain);

//
// When the `reverseProxy` `response` ends, end the
Expand Down

0 comments on commit 63ac925

Please sign in to comment.