Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Set headers prior to calling writeHead() (fixes #316)

This allows the connect.compress() middleware to work on proxied
requests.

If this header is not manually set then the compress middleware
will be invoked in the writeHead() call:

  https://github.com/nodejitsu/node-http-proxy/blob/1df2b30e84f078d86e744601bd6fc59381a1c7b3/lib/node-http-proxy/http-proxy.js#L260

before the header has been written. Therefore it will perform its
content-type check:

  https://github.com/senchalabs/connect/blob/7b5621b8dd4d9d82296b9b5d7bf3bea10de37a6c/lib/middleware/compress.js#L94

and so the check will fail and compression will be disabled.

Futhermore, even if that check were bypassed, compress() would be
unable to delete the content-length header:

  https://github.com/senchalabs/connect/blob/7b5621b8dd4d9d82296b9b5d7bf3bea10de37a6c/lib/middleware/compress.js#L123

because by then it wouldn't exist (it will be written after that
header event has been processed by compress). The affect of that
bug is that connections with keep-alive will stall for several
minutes because the browser is waiting for more data (based on
the orignal content-length header, set prior to compression).
  • Loading branch information...
commit 8c999c0f95515f0a95ef9a1d38df80cf63c445ef 1 parent 1df2b30
Francois Marier authored October 17, 2012

Showing 1 changed file with 6 additions and 1 deletion. Show diff stats Hide diff stats

  1. 7  lib/node-http-proxy/http-proxy.js
7  lib/node-http-proxy/http-proxy.js
@@ -257,7 +257,12 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
257 257
     }
258 258
 
259 259
     // Set the headers of the client response
260  
-    res.writeHead(response.statusCode, response.headers);
  260
+    for (var key in response.headers) {
  261
+      if (response.headers.hasOwnProperty(key)) {
  262
+        res.setHeader(key, response.headers[key]);
  263
+      }
  264
+    }
  265
+    res.writeHead(response.statusCode);
261 266
 
262 267
     // If `response.statusCode === 304`: No 'data' event and no 'end'
263 268
     if (response.statusCode === 304) {

0 notes on commit 8c999c0

Please sign in to comment.
Something went wrong with that request. Please try again.