How can I use multi-thread download if I can't get the "Content-Length"

c, http, networking

Solution

Sending `Range` requests is only possible if the response on a full request (which can be a HEAD if you just want to check the headers) to that file returns a `Accept-Ranges` response header with value of `bytes` and a `Content-Range` or `Content-Length` header which both contain information about the content length (and also `ETag` and/or `Last-Modified` so that the file can be uniquely identified).

If there's no `Accept-Ranges` response header at all, then you're lost. It means that the server simply doesn't support it.

Problem

I have learned that I can download a part of a file if I send the `Range: bytes=n-m` header to an HTTP server. Does that mean I can use multiple threads to download only if I know the exact file length? What I'm confused about is, how can I write a multi-threaded program, if I can't get the `Content-Length` beforehand?

Original source