skip to Main Content

I am trying to implement support for Content-Range in PHP-generated files. When a browser sends Range request my script gives correct bytes and it works well.
But while testing how Content-Range looks when downloading a PDF from Apache server I realized the first request from a web browser to my server does not contain Range header but somehow server still doesn’t return full file and only 32 kB.

On this screenshot you can see that Firefox sends 5 requests to Apache for my_pdf.pdf and Apache each time responds with 32-192 kB. The whole PDF is 28 MB. Requests 2-5 do contain Range request. But the first request- highlighted does not. You can see on the right that Content-Length is 28 MB but that Apache returned only 32 kB.

So my question is- how did Apache know to return only 32 kB and not the whole 28 MB PDF file?

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    Answer posted by @duskwuff is correct- Firefox terminates the transfer of the first requests once it gets enough to process the PDF.

    Below is just a few details I discovered. Firefox will terminate if your scripts returns these headers:

    Accept-Ranges: bytes
    Content-Length: 29293315
    

    You can (but don't have to) also return this header:

    header("Content-Range: bytes 0-29293314/29293315");   
    

    However by default Apache tries to compress whatever PHP returns and then adds this header:

    Transfer-Encoding: chunked
    

    And when Firefox (and Chrome) see this they won't close the connection. So I just disabled Apache compression and everything works. Now Firefox just does a few requests, get bits of PDF instead of the whole file and renders first page just fine (because it didn't need whole PDF to render just the first page).


  2. So my question is- how did Apache know to return only 32 kB and not the whole 28 MB PDF file?

    It didn’t. If you look at the Content-Length header in the response, it shows the full file size of 29.3 million bytes.

    The client probably closed the connection without reading the entire response.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search