Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when HTTP-server is done sending data

I'm working on a browser/proxy oriented project where I need to download webpages. After sending a custom HTTP request to a web server I start listening for a server response.

When reading the response, I check the response headers for a Content-Length:-row. If I get one of those, it's easy to determine when the server is done sending data since I always know how many bytes of data I have received.

The problem occurs when the server doesn't include the Content-Length header and also keeps the connection open for further requests. For example, the google server responds with gzipped-content, but doesn't include content length. How do I know when to stop waiting for more data and close the connection?

I have considered using a timeout value to close the connection when no data has been received for a while, but this seems like the wrong way to do it. Chrome for example, can download the same pages as me and always seem to know exactly when to close the connection.

like image 473
Accatyyc Avatar asked Dec 20 '25 18:12

Accatyyc


2 Answers

Have a look at IETF RfC 2616, search for chunked encoding and Content-Range.

HTTP is designed to return content of unknown length, as in:

HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked

25
This is the data in the first chunk

1C
and this is the second one

3
con
8
sequence
0

source Wikipedia

like image 87
bew Avatar answered Dec 22 '25 08:12

bew


I would try to suggest you to force Connection: close header so you are sure that the server closes the connection after output is finished, no matter if the Content-length is set or not. Performance will be partially affected by this

like image 26
usr-local-ΕΨΗΕΛΩΝ Avatar answered Dec 22 '25 07:12

usr-local-ΕΨΗΕΛΩΝ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!