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

Response is not getting compressed or being corrupted? #61

Closed
ashishnetworks opened this issue Jun 18, 2017 · 6 comments
Closed

Response is not getting compressed or being corrupted? #61

ashishnetworks opened this issue Jun 18, 2017 · 6 comments

Comments

@ashishnetworks
Copy link

this is my middleware code

app.use(compress({ filter: function (content_type) { return /text/i.test(content_type) }, threshold: 1, flush: require('zlib').Z_SYNC_FLUSH }))

And this is my response code

ctx.body = 'Hello world'
ctx.compress = true
ctx.set('Content-Type', 'text/plain')
ctx.set('content-encoding', 'gzip')

now when I hit the url localhost:3000/test, I get the error saying

ERR_CONTENT_DECODING_FAILED

But when I hit same url using CURL, I get the text saying

Hello World

I guess it's apparent that data is not getting compressed otherwise curl wouldn't have shown plain text. And I think Chrome error is due to content-encoding being gzip but actual data being plain text. May be I'm wrong but this is sure that there is some problem is my code otherwise chrome should have shown Hello World without error, isn't?

@jonathanong
Copy link
Member

don't set the content encoding urself.

@ashishnetworks
Copy link
Author

ashishnetworks commented Jun 21, 2017

Thanks, @jonathanong for your time. Well, without setting content encoding myself, I stop getting errors. But I believe it's not getting compressed when I don't set content encoding. Because in such case when I hit the URL using curl I simply get the plain text saying 'Hello World'. If curl doesn't support decompression by default, how am I receiving a plain text then?
And if I don't set encoding myself, the question is, how is it supposed to know which encoding to use out of deflate and gzip?

@jonathanong
Copy link
Member

if content-encoding is set, i believe this module will not run.

can you make a PR with a failing test case?

@ashishnetworks
Copy link
Author

sorry for being a noob, but what's PR? do you mean sample programs?

@jonathanong
Copy link
Member

pull request

@ashishnetworks
Copy link
Author

ashishnetworks commented Jun 21, 2017

Thank you @jonathanong for your comments. I just retested the whole process once again and I realised that my code was already working. I was making mistakes in assuming that response should always be compressed. But what I realised now, after lots of research is that compression works only when the client sends a header with Accept-Encoding. If supported Accept-Encoding is gzip, the response will be compressed in gzip if it's deflate, the response shall be compressed in deflate form, and if it's not mentioned response shall be simple plain data. I was receiving plain text in curl because curl does not send Accept-Encoding in its default request, but when I sent curl request using curl -H 'Accept-Encoding: gzip' -D - http://localhost:3000 my response was coded into compressed form. So my code has been working for always. Thanks

Edit: And you were also right that if content-encoding is set, module will not run
so we don't have to explicitly set the content-encoding, it should be set by compression middleware depending on Accept-Encoding of the request

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

2 participants