Kirill Klimuk
Kirill Klimuk

Reputation: 13

How can you tell if a file is fully downloaded via HTTP?

How can you tell if a file is fully downloaded via HTTP?

What if you have flushed the packets from the server, but the user is still downloading them? Won't the TCP FIN packet look identical - whether the user has finished or has cancelled the download?

Upvotes: 1

Views: 1694

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596256

There is no way for a server to know if the client has received all of the bytes. All it can do is send and flush the bytes, and then close the connection if an HTTP keep-alive is not being used. It is the client's responsibility to:

  1. detect how the bytes are being sent (normal, compressed, chunked, etc) so it knows how to read them.

  2. detect how many bytes are being sent (it will not be able to detect this up front if the data is being sent in a chunked format).

  3. detect when all expected bytes have been received so it can stop reading.

So the client, not the server, knows when it has reached the end of the download vs when the download has been aborted prematurely. If the client known/feels the download is incomplete, it can always re-query the server for the file metadata and compare that to the file is already has, and then re-download if needed.

Upvotes: 1

Related Questions