3

Why Content-Length header is removed when HTTP response is compressed using GZIP?

When I set the response headers like:

response.setHeader("Content-Encoding", "gzip");
response.setContentLengthLong(46464);

the Content-Length header is not sent by the GlassFish4 server. My question is - why it is removed?

I'm using java Servlet/3.1.

Response header:

Server: GlassFish Server Open Source Edition  4.1
X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition  4.1  Java/Oracle Corporation/1.8)
Date: Sat, 28 Nov 2015 19:36:53 GMT
Accept-Ranges: bytes
Connection: keep-alive
Keep-Alive: timeout=1800
Content-Disposition: inline;filename*=utf-8''big-buck-bunny_traixler.webm
ETag: big-buck-bunny_traixler.webm-2165175-1448346083000
Cache-Control: private,max-age=604800
Last-Modified: Tue, 24 Nov 2015 06:21:23 GMT
Content-Encoding: gzip

response.setContentLengthLong(46464); is set after compressed data written to byte array (thus you know compressed data length), and before compressed data are written to output stream and flushed.

Also - Content-Type header is removed...

Ernestas Gruodis
  • 8,567
  • 14
  • 55
  • 117
  • Why won't the server calculate the content length automatically? – xyz Nov 28 '15 at 20:14
  • But when you click on download link - you have to wait for download dialog to appear for a long time (at least in Firefox). When there is no compression - the download start dialog shows immediately. I think it's related to `Content-Length` header. – Ernestas Gruodis Nov 28 '15 at 20:18
  • I suspect this post might answer your doubts :) https://stackoverflow.com/questions/3819280/content-length-when-using-http-compression – froskos Jun 27 '18 at 09:25

0 Answers0