Commit 3fd3c96f authored by indexzero's avatar indexzero

[api] Force connection header to be `close` until keep-alive is replemented

parent 35886878
...@@ -242,7 +242,7 @@ HttpProxy.prototype.close = function () { ...@@ -242,7 +242,7 @@ HttpProxy.prototype.close = function () {
// #### @buffer {Object} **Optional** Result from `httpProxy.buffer(req)` // #### @buffer {Object} **Optional** Result from `httpProxy.buffer(req)`
// //
HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) { 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 // Check the proxy table for this instance to see if we need
...@@ -301,15 +301,21 @@ HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) { ...@@ -301,15 +301,21 @@ HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) {
res.end(); res.end();
} }
// Open new HTTP request to internal resource with will act as a reverse proxy pass var opts = {
reverseProxy = http.request({
host: host, host: host,
port: port, port: port,
agent: _getAgent(host, port), agent: _getAgent(host, port),
method: req.method, method: req.method,
path: req.url, path: req.url,
headers: req.headers 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. // Process the `reverseProxy` `response` when it's received.
if (response.headers.connection) { if (response.headers.connection) {
...@@ -362,17 +368,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) { ...@@ -362,17 +368,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) {
// request unless we have entered an error state. // request unless we have entered an error state.
// //
req.on('end', function () { req.on('end', function () {
// if (!errState) {
// __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)) {
reverseProxy.end(); reverseProxy.end();
} }
}); });
...@@ -390,20 +386,26 @@ HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) { ...@@ -390,20 +386,26 @@ HttpProxy.prototype.proxyRequest = function (req, res, port, host, buffer) {
// by `this.options.forward` ignoring errors and the subsequent response. // by `this.options.forward` ignoring errors and the subsequent response.
// //
HttpProxy.prototype._forwardRequest = function (req) { HttpProxy.prototype._forwardRequest = function (req) {
var self = this, port, host, forwardProxy; var self = this, port, host, forwardProxy, opts;
port = this.options.forward.port; port = this.options.forward.port;
host = this.options.forward.host; host = this.options.forward.host;
// Open new HTTP request to internal resource with will act as a reverse proxy pass opts = {
forwardProxy = http.request({
host: host, host: host,
port: port, port: port,
agent: _getAgent(host, port), agent: _getAgent(host, port),
method: req.method, method: req.method,
path: req.url, path: req.url,
headers: req.headers 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. // 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. // Remark (indexzero): We will eventually emit a 'forward' event here for performance tuning.
...@@ -415,7 +417,7 @@ HttpProxy.prototype._forwardRequest = function (req) { ...@@ -415,7 +417,7 @@ HttpProxy.prototype._forwardRequest = function (req) {
// Remark: Ignoring this error in the event // Remark: Ignoring this error in the event
// forward target doesn't exist. // 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 // Chunk the client request body as chunks from the proxied request come in
req.on('data', function (chunk) { req.on('data', function (chunk) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment