expressjs / compression

Node.js compression middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to get deflate to work

Exocomp opened this issue · comments

I'm using node v8.9.4, express v4.16.2, compression v1.7.2. Whenever I make a request the response is always gzip even though I set the Accept-Encoding to deflate with the request header.

I'm using Postman to do the test.

Request:

GET /server/ HTTP/1.1
Host: www.server.com
Accept-Encoding: deflate
Cache-Control: no-cache
Postman-Token: 5774702d-e02z-61a4-z407-z635ae5eb5b5

Response:

content-encoding →	gzip
content-type →	text/html; charset=utf-8
date →	Thu, 22 Mar 2018 15:39:23 GMT
etag →	W/"0815"
transfer-encoding →	chunked
vary →	Accept-Encoding
x-powered-by →	Express

Here is how I've configured it:

app.use(compression({filter: shouldCompress}))
function shouldCompress (req, res) {
  if (req.headers['x-no-compression']) {
    return false
  }
  return compression.filter(req, res)
}

What am I missing, shouldn't the response content-encoding be deflate? Why am I getting gzip ?

Very strange. You didn't provide a full app, but I formed what you did give into a basic app and it responded with a deflate response as expected. Can you help me better understand the steps to reproduce the issue? What do I need to change below to get the same result as you? If I can reproduce the issue, I can dig in with a debugger to see what is going on 👍

$ cat app.js 
const express = require('express')
const compression = require('compression')

const app = express()

app.use(compression({filter: shouldCompress}))
app.get('/', (req, res) => {
  res.send(Buffer.alloc(2048).toString())
})
function shouldCompress (req, res) {
  if (req.headers['x-no-compression']) {
    return false
  }
  return compression.filter(req, res)
}

app.listen(3000)

$ node app &
[1] 81312

$ curl -H'Host: www.server.com' -H'Accept-Encoding: deflate' -H'Cache-Control: no-cache' -H'Postman-Token: 5774702d-e02z-61a4-z407-z635ae5eb5b5' -v http://127.0.0.1:3000
* Rebuilt URL to: http://127.0.0.1:3000/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> Host: www.server.com
> User-Agent: curl/7.54.0
> Accept: */*
> Accept-Encoding: deflate
> Cache-Control: no-cache
> Postman-Token: 5774702d-e02z-61a4-z407-z635ae5eb5b5
> 
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: text/html; charset=utf-8
< ETag: W/"800-YF2z/br/S6E3KTca0MT7qziJN44"
< Vary: Accept-Encoding
< Content-Encoding: deflate
< Date: Wed, 28 Mar 2018 02:27:21 GMT
< Connection: keep-alive
< Transfer-Encoding: chunked
< 
x?c`?`??Q0
* Connection #0 to host 127.0.0.1 left intact

Well this is certainly weird.

I tried your sample code/app in your response and I can reproduce it in Postman but I can't reproduce it with curl.

Postman

GET / HTTP/1.1
Host: localhost:3000
User-Agent: curl/7.50.1
Accept: */*
Accept-Encoding: deflate
Cache-Control: no-cache
Postman-Token: 5774702d-e02z-61a4-z407-z635ae5eb5b5

connection →keep-alive
content-encoding →gzip
content-type →text/html; charset=utf-8
date →Thu, 29 Mar 2018 18:45:34 GMT
etag →W/"800-YF2z/br/S6E3KTca0MT7qziJN44"
transfer-encoding →chunked
vary →Accept-Encoding
x-powered-by →Express

curl

$ curl -H'Host: www.server.com' -H'Accept-Encoding: deflate' -H'Cache-Control: no-cache' -H'Postman-Token: 5774702d-e02z-61a4-z407-z635ae5eb5b5' -v http://127.0.0.1:3000
* Rebuilt URL to: http://127.0.0.1:3000/
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> Host: www.server.com
> User-Agent: curl/7.50.1
> Accept: */*
> Accept-Encoding: deflate
> Cache-Control: no-cache
> Postman-Token: 5774702d-e02z-61a4-z407-z635ae5eb5b5
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: text/html; charset=utf-8
< ETag: W/"800-YF2z/br/S6E3KTca0MT7qziJN44"
< Vary: Accept-Encoding
< Content-Encoding: deflate
< Date: Thu, 29 Mar 2018 18:34:29 GMT
< Connection: keep-alive
< Transfer-Encoding: chunked

Are you 100% sure that postman is actually sending the Accept-Encoding: deflate header? Can you send a packet capture to see what it looks like on the wire?

If you are using the Postman app in Chrome, according to https://www.getpostman.com/docs/v6/postman/sending_api_requests/interceptor_extension you have no control over the value of Accept-Encoding that is sent to the server. Try using the non-Chrome Postman app.

I tried with C# and it is working. Apologies for the confusion. In my defense the issue/confusion was caused by Postman - it is the first time it has failed me.