本文描述了Http服务器使用http range支持浏览器的下载管理器下载大文件时可暂停和继续的实现方法,后面用英文写,但我的英文不太好,如有错误之处,希望大家帮我纠正,我的联系方式在后面。
Section A. Summary
I'm making a http server in C, in order to support big file download, I have to support Http Range in my server, as the usual way for web browsers to download big files is to use its download manager, where we can pause and resume the download progress. For the pause and resume functions of download manager to work, the http server must support Http Range requests.
Besides file download, http range is also needed for <video> element to play videos on web page, when the user click a random position of the video time line, the browser use http range requests to get the corresponding file content of the video file.
Http range is imported into http protocol from http 1.1. A http range request is like this:163Please respect copyright.PENANAwPy893zgoH
----------http range request-------163Please respect copyright.PENANAewLPkHtFCM
GET /zen.iso HTTP/1.1163Please respect copyright.PENANARITGPLiDoX
Range: bytes=100-1023163Please respect copyright.PENANAISRsCiaQRQ
If-Match: "534231200_1732277116"163Please respect copyright.PENANAPJJTsOmIWK
......163Please respect copyright.PENANArrpp6YYgmY
-----------------------------------163Please respect copyright.PENANAGI2sDkxM9q
It means to get the content from byte 100 to byte 1023, totally 924 bytes, and it tells the http server the etag value of already downloaded part is "534231200_1732277116", the etag value is used for the web browser to determine if the requested content has been modified, it's necessory for some browsers like chrome and edge to resume the download of a file. If http server do not provide etag for range content, the resume function of web browser download manager will not work.
Section B. Detailed Steps
The following is the steps for serving file download, support pause and resume functions of web browser download manager.
163Please respect copyright.PENANAM9r9yiBB5A
Step 1. make a link for file to download, for example:163Please respect copyright.PENANAhxGHko80Tb
----------------html-----------------163Please respect copyright.PENANAoPAFZPvFCI
<a href="/zen.iso">zen.iso</a>163Please respect copyright.PENANANb3Kbqfutr
-------------------------------------163Please respect copyright.PENANAK2hex07C0F
When user click the link, web browser download manager will take care of the file download immediately. We cannot use javascript fetch(url) to start downloading, as in this way javascript code will take care of the download instead of download manager.
When the link is clicked, the http request sent to server is like this:163Please respect copyright.PENANAEs1dJKR24j
----------get request------------163Please respect copyright.PENANAHsKYQdsAAj
GET /zen.iso HTTP/1.1163Please respect copyright.PENANAYXmKd9UuC6
......163Please respect copyright.PENANAYyM0syirQI
---------------------------------
163Please respect copyright.PENANAQgbgb6lKjs
Step 2. when http server receives above request, it sends the whole file content as response body back as usual, and indicates support of http range in the response headers, like this:163Please respect copyright.PENANAlh0SaNrKGq
----------------server response--------------163Please respect copyright.PENANAhqkfsjFv5r
HTTP/1.1 200 OK163Please respect copyright.PENANA63AvlcKrP8
Date: Sat, 23 Nov 2024 06:29:19 GMT163Please respect copyright.PENANAL2ZyUHoPf6
accept-ranges: bytes163Please respect copyright.PENANAV20ikJo1ZM
content-range: bytes 0-534231199/534231200163Please respect copyright.PENANAhbIe4x5ASE
ETag: "534231200_1732277116"163Please respect copyright.PENANAx78dgwZMWR
Content-Disposition: attachment; filename="zen.iso"163Please respect copyright.PENANA8b7c0oerYe
Content-Length: 534231200163Please respect copyright.PENANARUwvVYSe4R
Content-Type: application/octet-stream
(http response body is the whole file content)163Please respect copyright.PENANA19q9RMyilk
-------------------------------------------
Http server usually use a small buf to send the whole file content by multiple times if the file size is bigger than it, like this:163Please respect copyright.PENANAHkOQFp77Ps
---------------http server send big file content by multiple times--------------163Please respect copyright.PENANAW7yHNoPnOR
const int buf_size=20000000; // buf size is about 20M163Please respect copyright.PENANA5H7JomZRUj
int client_socket; char send_buf[buf_size]; FILE *file;
// first send http response headers163Please respect copyright.PENANA1oM7PmJe7D
send_http_response_headers(client_socket, http_response_headers);
// then send the whole file content as response body by multiple times if the file is big163Please respect copyright.PENANA5Ckr9VJCrr
while(there_is_remain_file_content_to_send)163Please respect copyright.PENANAVyoEis8ULm
{ 163Please respect copyright.PENANATH6YoUsADS
read_remain_file_content_to_send_buf(file, send_buf);163Please respect copyright.PENANAhVVVs8kq9r
send_result = send(client_socket, send_buf);163Please respect copyright.PENANAtq0vgDH5NA
if(send_result == fail) return; else continue;163Please respect copyright.PENANACMObTmImOK
}
// a way to make etag for a file using its file size and last modifed time163Please respect copyright.PENANAt9QWRcS7gI
const char *file_etag(const char *path) 163Please respect copyright.PENANA58fv5eY0GY
{163Please respect copyright.PENANAdQ8yMk69JM
struct stat st; if (stat(path, &st) < 0) return NULL; char* data=malloc(64); snprintf(data, 63, "%ld_%ld", (long)st.st_size, (long)st.st_mtime); return data;163Please respect copyright.PENANAY5bQejB3u1
}163Please respect copyright.PENANAyMt9wppw8O
-------------------------------------------------163Please respect copyright.PENANAT4WogWUQXb
"accept-ranges: bytes" tells client web browser that the http server support range.163Please respect copyright.PENANAeJutMnvn6E
"content-range: bytes 0-534231199/534231200" tells client web browser the content range(0-534231199) of the response body and the total file size(534231200).163Please respect copyright.PENANAPEcdQmHt0R
'ETag: "534231200_1732277116"' tells client web browser the file version.163Please respect copyright.PENANArT9GOGFVpE
'Content-Disposition: attachment; filename="zen.iso"' tells client web browser to download the file as attachment, and its file name.163Please respect copyright.PENANACdSURHEvsH
"Content-Length: 534231200" tells client web browser the total size of the response body, which is the size of the whole file in this example.163Please respect copyright.PENANAwUWy48vMsH
"Content-Type: application/octet-stream" tells client web browser to treat the response body as binary stream.
163Please respect copyright.PENANAwMl5XwoRSK
step 3. When the web browser receives above response, it knows the server support http range, and knows the total file size by "content-range: bytes 0-534231199/534231200", it also saves the etag for later comparation.163Please respect copyright.PENANAchN240b4yX
If the file is big, the above sends may take several minutes, during this time, the user can click pause in the download manager of web browser(for firefox, you have to press ctrl and right click the file to show the pause command.)163Please respect copyright.PENANA6caCxdqoik
When pause is pressed, the web browser disconnects with http server, and the "send_result = send(client_socket, send_buf);" of http server will be fail, then the http server stops sending anymore.
163Please respect copyright.PENANAb5FGvYqtkT
step 4. Later the user can click resume to continue the download in download manager, and the download manager will send a http range request to tell the server the resume start point, like this:163Please respect copyright.PENANAQOPgaBlASh
---------resume range request---------163Please respect copyright.PENANA6YoGWD4RT4
GET /zen.iso HTTP/1.1163Please respect copyright.PENANABIryblG8Ah
Range: bytes=243220480-163Please respect copyright.PENANABDFkjJSD5j
If-Match: "534231200_1732277116"163Please respect copyright.PENANAy0tf5KF8Hc
......163Please respect copyright.PENANAS3QVN5Zsk5
-------------------------------------163Please respect copyright.PENANAQIqBqVHi4Z
"Range: bytes=243220480-" tells the http server to send content from byte 243220480 to end.163Please respect copyright.PENANA0FWV4GltnW
'If-Match: "534231200_1732277116"' tells the http server to check the file etag.
If the etag remains the same, it means the file is not changed, so the server can send from the resume point 243220480, and its response status should be 206 partial content, like this:163Please respect copyright.PENANATPzLwkGYNk
-------response content from resume point-------163Please respect copyright.PENANAAUbQji7Uzj
HTTP/1.1 206 Partial Content163Please respect copyright.PENANA2PYCq2D5GR
Date: Sat, 23 Nov 2024 06:56:17 GMT163Please respect copyright.PENANALCTYmWNf9H
accept-ranges: bytes163Please respect copyright.PENANAXlO3TbpDXf
content-range: bytes 243220480-534231199/534231200163Please respect copyright.PENANANbnJqWAqpz
Content-Length: 291010720163Please respect copyright.PENANAmlFJhrUiLz
Content-Type: application/octet-stream163Please respect copyright.PENANAFC6kthHzxk
Content-Disposition: attachment; filename="zen.iso"163Please respect copyright.PENANAweZ5xrr59j
ETag: "534231200_1732277116"
(http response body is the file content from resume point to end)163Please respect copyright.PENANAlRggNi2n69
------------------------------------------------163Please respect copyright.PENANAJ9p1IgYwUt
When client download manager receives this response, it will keep the already downloaded content and append the new respone body content to it.
If the etag diffs, it means the file has been changed, then the server should send the file content from beginning to end, with a 200 OK response status, like this:163Please respect copyright.PENANAFeSBP9sUw6
-------response content from resume point-------163Please respect copyright.PENANAhladqqcP0F
HTTP/1.1 200 OK163Please respect copyright.PENANAFzNSIjWnds
Date: Sat, 23 Nov 2024 06:29:19 GMT163Please respect copyright.PENANAml5O7wnli4
accept-ranges: bytes163Please respect copyright.PENANA0LuzG9RdEd
content-range: bytes 0-634231201/634231202163Please respect copyright.PENANAxw01dtjGP1
ETag: "634231202_2732277118"163Please respect copyright.PENANA6o4tRsb3r4
Content-Disposition: attachment; filename="zen.iso"163Please respect copyright.PENANAy2i1NX1k4K
Content-Length: 634231202163Please respect copyright.PENANAQ8m0YsxvNL
Content-Type: application/octet-stream
(http response body is the whole file content)163Please respect copyright.PENANAf2ygm4x4Dn
------------------------------------------------163Please respect copyright.PENANA6n1jpwispf
When client download manager receives this response, it will clear the already downloaded content and start download from beginning.
163Please respect copyright.PENANA8RwboxPMLh
step 5. when a <video src="/video_file.mp4"> is used to play videos on web page, the browser also used http range requests, as Zeng Xu's article describes:163Please respect copyright.PENANAwNRsWJwuZE
https://www.zeng.dev/post/2023-http-range-and-play-mp4-in-browser/?
163Please respect copyright.PENANA203Nzwos32
Section C. Thanks
感謝曾旭的幫助,曾旭的文章对http range有非常好的描述。
感谢stackoverflow对使用etag的提示163Please respect copyright.PENANAwKWYd0i4Mr
https://stackoverflow.com/questions/66998172/does-google-chrome-and-similar-browsers-support-range-headers-for-standard-downl
感谢gemini等ai对写代码的辅助163Please respect copyright.PENANAe72AngTV1j
https://gemini.google.com/
感谢互联网上无私奉献的参考资163Please respect copyright.PENANA1uwnXMcxNx
https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests163Please respect copyright.PENANAvfW1tqvgJA
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Range163Please respect copyright.PENANAfI3HE8Espi
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag163Please respect copyright.PENANA1DLN3L28Ix
https://www.rfc-editor.org/rfc/rfc7233163Please respect copyright.PENANANnONhHRMjl
https://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/24.10/
163Please respect copyright.PENANAiFQafGKZHH
Section D. Contacts Me
If you found any errors or have any suggestions for this article, please let me know, my wechat: si_jinmin, my email: [email protected]163Please respect copyright.PENANApVnl3QvPG4
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: si_jinmin, 我的email: [email protected]
如果您對C/C++ programming, Linux, website development, Vue, Git, vscode感興趣,邀請您加入「Linux/C/C++ Website Development」 微信群,請加我的微信(si_jinmin)以便拉您进群。
ns216.73.216.41da2