Skip to content

Commit

Permalink
[api] Force connection header to be close until keep-alive is reple…
Browse files Browse the repository at this point in the history
…mented
  • Loading branch information
indexzero committed Mar 20, 2011
1 parent 3588687 commit 3fd3c96
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions lib/node-http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ HttpProxy.prototype.close = function () {
// #### @buffer {Object} **Optional** Result from `httpProxy.buffer(req)`
//
HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) {
var self = this, reverseProxy, location, errState = false;
var self = this, reverseProxy, location, errState = false, opts;

//
// Check the proxy table for this instance to see if we need
Expand Down Expand Up @@ -301,15 +301,21 @@ HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) {
res.end();
}

// Open new HTTP request to internal resource with will act as a reverse proxy pass
reverseProxy = http.request({
var opts = {
host: host,
port: port,
agent: _getAgent(host, port),
method: req.method,
path: req.url,
headers: req.headers
}, function (response) {
};

// Force the `connection` header to be 'close' until
// node.js core re-implements 'keep-alive'.
opts.headers['connection'] = 'close';

// Open new HTTP request to internal resource with will act as a reverse proxy pass
reverseProxy = http.request(opts, function (response) {

// Process the `reverseProxy` `response` when it's received.
if (response.headers.connection) {
Expand Down Expand Up @@ -362,17 +368,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) {
// request unless we have entered an error state.
//
req.on('end', function () {
//
// __Remark__ *(indexzero | 3/10/2011)*: This is a short-term workaround for a suspect error from net.js when
// `http.ClientRequest.end()` is called in reproducable, but uninvestigated scenarios
//
// net.js:313
// throw new Error('Socket.end() called already; cannot write.');
// ^
// Error: Socket.end() called already; cannot write.
// at Socket.write (net.js:313:13)
//
if (!errState && (!reverseProxy.socket || reverseProxy.socket._writeQueueLast() !== 42)) {
if (!errState) {
reverseProxy.end();
}
});
Expand All @@ -390,20 +386,26 @@ HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) {
// by `this.options.forward` ignoring errors and the subsequent response.
//
HttpProxy.prototype._forwardRequest = function (req) {
var self = this, port, host, forwardProxy;
var self = this, port, host, forwardProxy, opts;

port = this.options.forward.port;
host = this.options.forward.host;

// Open new HTTP request to internal resource with will act as a reverse proxy pass
forwardProxy = http.request({
opts = {
host: host,
port: port,
agent: _getAgent(host, port),
method: req.method,
path: req.url,
headers: req.headers
}, function (response) {
};

// Force the `connection` header to be 'close' until
// node.js core re-implements 'keep-alive'.
opts.headers['connection'] = 'close';

// Open new HTTP request to internal resource with will act as a reverse proxy pass
forwardProxy = http.request(opts, function (response) {
//
// Ignore the response from the forward proxy since this is a 'fire-and-forget' proxy.
// Remark (indexzero): We will eventually emit a 'forward' event here for performance tuning.
Expand All @@ -415,7 +417,7 @@ HttpProxy.prototype._forwardRequest = function (req) {
// Remark: Ignoring this error in the event
// forward target doesn't exist.
//
forwardProxy.on('error', function (err) { });
forwardProxy.once('error', function (err) { });

// Chunk the client request body as chunks from the proxied request come in
req.on('data', function (chunk) {
Expand Down

0 comments on commit 3fd3c96

Please sign in to comment.