Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compression returns (invalid) Content-Length which hangs HTTP connections. #6

Closed
wrr opened this issue Mar 26, 2014 · 10 comments
Closed

Comments

@wrr
Copy link

wrr commented Mar 26, 2014

The middleware does not process and modify headers that are passed as arguments to the writeHead() function. If the Content-Length is passed to the writeHead(), it is returned unaltered, with a value that after compression is invalid.

A breaking example:

var connect = require('connect');
var compress = require('compression');
var http = require('http');

var app = connect()
  .use(compress({
    threshold: 0
  }))
  .use(function(req, res) {
    res.setHeader('Content-Type', 'text/plain')
    res.writeHead(200, {'Content-Length': 3})
    res.end('foo');
  });

http.createServer(app).listen(5000);

This returns following headers:

Connection:keep-alive
Content-Encoding:gzip
Content-Length:3
Content-Type:text/plain
Date:Wed, 26 Mar 2014 19:16:26 GMT
Vary:Accept-Encoding

A failing unit test

There are other related issues caused by the same bug. Following headers are incorrectly processed if they are passed to the writeHead function: Vary, Content-Type, Content-Encoding.

@wrr wrr changed the title Compressions returns (invalid) Content-Length which hangs HTTP connections. Compression returns (invalid) Content-Length which hangs HTTP connections. Mar 26, 2014
@jonathanong
Copy link
Member

PR welcome, but IMO .writeHead() is a terrible api that people should avoid.

@dougwilson
Copy link
Contributor

Also, @wrr if you are going to make a PR, please create a new test that fails, rather than modifying an existing test.

@Fishrock123
Copy link
Contributor

@wrr Any middleware cannot process things that further middleware does.

Your breaking example will always fail. (not the repurposed the unit test)

Furthermore, I have seen the use of writeHead discouraged in mainstream use by node's maintainers, and you should probably avoid it.

@wrr
Copy link
Author

wrr commented Apr 28, 2014

writeHead is a part of a stable node API, it is not deprecated and middleware should not break it. writeHead isn't also discouraged in the official node documentation. If compression does not to support writeHead, it IMO should explicitly fail (with an assertion in the writeHead method). But compression tries to implement writeHead, the implementation is broken and your argument is that it is fine because writeHead API is ugly?

@Fishrock123
Copy link
Contributor

Oh. I'm wrong. We actually overwrite res.writeHead so that using it after compression works properly. That means that your unit test should fail (because we don't handle that). Your example should also fail.

We only call node's writeHead after everything else, is an issue?
https://github.com/expressjs/compression/blob/master/index.js#L141-L156

As far as I can tell, we aren't breaking it.

@Fishrock123 Fishrock123 reopened this Apr 28, 2014
@wrr
Copy link
Author

wrr commented Apr 28, 2014

In my example, if you remove compression middleware, the example works fine, if you add the middleware, it returns invalid response. This means that the middleware does not support writeHead with non empty headers argument and breaks code that uses this API. Returning invalid responses is not the same as failing. If compressions failed (with an exception), it would be easy to detect that returning headers in writeHead is not supported.

@Fishrock123
Copy link
Contributor

Of course res.writeHead(200, {'Content-Length': 3}) is going to cause an invalid response, what do you expect?

@wrr
Copy link
Author

wrr commented Apr 28, 2014

My expectations are the same as with res.setHeader('Content-Length', 3) which does not cause an invalid response. Compression middleware is altering length of content, and it should handle changing Content-Length headers (or replacing them with chunked encoding) in such a way that returned response remains valid. It does so correctly when headers are passed with setHeader(), it doesn't do it when headers are passed with writeHead().

Are you arguing that there is no bug? My impression was that @jonathanong acknowledged the bug, but pointed that he isn't in favor of supporting 'writeHead` API.

@Fishrock123
Copy link
Contributor

I'm going with: I do not understand this enough and someone else will probably have to fix it.

Apologies.

@dougwilson
Copy link
Contributor

This thread is soo annoying. It's fixed now, you're welcome.

cecchi pushed a commit to deal/compression that referenced this issue Feb 28, 2019
updated the dependencies to their latest versions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants