3

When apache returns a gzip compressed response with 204 response code and empty body server returns invalid header Content-Length: 20 instead of Content-Length: 0.

Without gzip compression (without Accept-Encoding header) server returns valid header Content-Length: 0.

Request and response with compression:

0 % curl -v http://mta.dev/api/wtf/\?id\=09102 --compressed
* Hostname was NOT found in DNS cache
*   Trying 172.17.0.2...
* Connected to mta.dev (172.17.0.2) port 80 (#0)
> GET /api/wtf/?id=09102 HTTP/1.1
> User-Agent: curl/7.38.0
> Host: mta.dev
> Accept: */*
> Accept-Encoding: deflate, gzip
> 
< HTTP/1.1 204 No Content
< Date: Thu, 09 Jun 2016 15:44:53 GMT
* Server Apache/2.4.7 (Ubuntu) is not blacklisted
< Server: Apache/2.4.7 (Ubuntu)
< X-Powered-By: PHP/5.5.9-1ubuntu4.17
< P3P: policyref="/bitrix/p3p.xml", CP="NON DSP COR CUR ADM DEV PSA PSD OUR UNR BUS UNI COM NAV INT DEM STA"
< X-Powered-CMS: Bitrix Site Manager (d04cd2b3dbab106e7537af3767043172)
< Set-Cookie: PHPSESSID=8arlnd14t1k97bri56clb2qhh1; path=/; HttpOnly
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< Pragma: no-cache
< Set-Cookie: BITRIX_SM_GUEST_ID=2328047; expires=Sun, 04-Jun-2017 15:44:53 GMT; Max-Age=31104000; path=/
< Set-Cookie: BITRIX_SM_LAST_VISIT=09.06.2016+18%3A44%3A53; expires=Sun, 04-Jun-2017 15:44:53 GMT; Max-Age=31104000; path=/
< Content-Encoding: gzip
< Content-Length: 20
< Content-Type: application/json
< 
* Excess found in a non pipelined read: excess = 20 url = /api/wtf/?id=09102 (zero-length body)
* Connection #0 to host mta.dev left intact

Request and response without compression:

0 % curl -v http://mta.dev/api/wtf/\?id\=09102
* Hostname was NOT found in DNS cache
*   Trying 172.17.0.2...
* Connected to mta.dev (172.17.0.2) port 80 (#0)
> GET /api/wtf/?id=09102 HTTP/1.1
> User-Agent: curl/7.38.0
> Host: mta.dev
> Accept: */*
> 
< HTTP/1.1 204 No Content
< Date: Thu, 09 Jun 2016 15:38:43 GMT
* Server Apache/2.4.7 (Ubuntu) is not blacklisted
< Server: Apache/2.4.7 (Ubuntu)
< X-Powered-By: PHP/5.5.9-1ubuntu4.17
< P3P: policyref="/bitrix/p3p.xml", CP="NON DSP COR CUR ADM DEV PSA PSD OUR UNR BUS UNI COM NAV INT DEM STA"
< X-Powered-CMS: Bitrix Site Manager (d04cd2b3dbab106e7537af3767043172)
< Set-Cookie: PHPSESSID=ceqsuv4ie3fkq497uvk6e2gki1; path=/; HttpOnly
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< Pragma: no-cache
< Set-Cookie: BITRIX_SM_GUEST_ID=2328047; expires=Sun, 04-Jun-2017 15:38:43 GMT; Max-Age=31104000; path=/
< Set-Cookie: BITRIX_SM_LAST_VISIT=09.06.2016+18%3A38%3A43; expires=Sun, 04-Jun-2017 15:38:43 GMT; Max-Age=31104000; path=/
< Content-Length: 0
< Content-Type: application/json
< 
* Connection #0 to host mta.dev left intact

Setting Content-Length: 0 header in PHP application manually has no effect because apache recalculates length after gzipping.

I found this bug in Apache bugtracker https://bz.apache.org/bugzilla/show_bug.cgi?id=51350 where developer says that this bug fixed in 2.4.1 version. I have 2.4.7 version installed and this bug still occurs.

How i can disable gzip compression for 204 responses, or for responses with empty body? Or maybe there is a way to disable overwriting Content-Length header by apache?

1
  • I have this exact same problem. Running Apache 2.4.23.
    – nickdnk
    Nov 21, 2016 at 14:03

1 Answer 1

1

Seems the reported content length is correct: If the content is empty, the gzip algorithm compresses this to 20 bytes. Try on the command line:

gzip < /dev/null | wc -c
20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .