本文描述了HTTP协议使用Etag对内容进行缓存以便减少不必要的网络传输以提高运行效率的机制。后面用英文写,但我的英文不太好,如有错误之处,希望大家帮我纠正,我的联系方式在后面。
This article describes the HTTP procotol mechanism of using Etag to cache resources and resume file download in order to reduce unnecessory network transfer so the performance can increase.
153Please respect copyright.PENANASSHiGpCrzb
Section A. ETag
Etag is an identity of a resource, for example, we can use "10000_20241125150500" as the identity of a file where 10000 is the file size and 20241125150500 is the last modifed time of the file. We tell client browsers the etag of a file, then the browser cache the file and its etag, when next time the file is requested again, the browser can first just send the etag of the file to server, and if the server find the etag remain unchanged, then the browser can use the cache, no need to get the file from server once again.
The server send the etag of a resource by header like this:153Please respect copyright.PENANAlLNurursL9
---------------http response with etag----------------153Please respect copyright.PENANAIbvTJ0wFOT
HTTP/1.1 200 OK153Please respect copyright.PENANARAEfogRv9O
Date: Mon, 25 Nov 2024 10:56:12 GMT153Please respect copyright.PENANAmN7A3BgRUT
accept-ranges: bytes153Please respect copyright.PENANAQJrSOHbGzN
ETag: "534231200_1732277116"153Please respect copyright.PENANANVAOlSyviy
Content-Length: 534231200153Please respect copyright.PENANA6m8ZMwWD12
Content-Type: application/octet-stream153Please respect copyright.PENANAQDo8FB1M3p
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)153Please respect copyright.PENANAWyOR3EmY9X
-----------------------------------------------------
153Please respect copyright.PENANAsHP4dh2PAP
Section B. if-none-match
The browser send back the etag to ask server if it has been changed by header like this:153Please respect copyright.PENANAzmW418rtWK
-----http request for a cached resource------153Please respect copyright.PENANAsU37znjvZ7
GET /zen.iso HTTP/1.1153Please respect copyright.PENANANVbfpiz33Y
if-none-match: "534231200_1732277116"153Please respect copyright.PENANAuNOZ028qF7
---------------------------------------------
If the etag is unchanged, the server response like this:
1. for get and head requests:153Please respect copyright.PENANAhsMcVek7Xk
-----------http response for unchanged resource------153Please respect copyright.PENANA0AU9BlSIxb
HTTP/1.1 304 Not Modified
(http body canbe empty, the client will used its cache)153Please respect copyright.PENANAil4BpGnZqt
----------------------------------------------------
2. for other methods requests:153Please respect copyright.PENANAOWDFA46xZc
-----------http response for unchanged resource------153Please respect copyright.PENANAPWGjIHCmTj
HTTP/1.1 412 Precondition Failed
(http body canbe empty, the client will used its cache)153Please respect copyright.PENANA19NF6Zlzdh
----------------------------------------------------
153Please respect copyright.PENANA41eJAlu4Gs
If the etag changes, the server response 200 OK with the new file content like this:153Please respect copyright.PENANAeafcOHpsl5
---------------http response with etag----------------153Please respect copyright.PENANAW0XTQSmeq5
HTTP/1.1 200 OK153Please respect copyright.PENANApK96LmRyVW
Date: Mon, 25 Nov 2024 10:56:12 GMT153Please respect copyright.PENANAzS4N9XYbO5
accept-ranges: bytes153Please respect copyright.PENANAonS8luL8gN
ETag: "534231200_1732277116"153Please respect copyright.PENANAyBQ0NEZQrG
Content-Length: 534231200153Please respect copyright.PENANAgzsWzMlObx
Content-Type: application/octet-stream153Please respect copyright.PENANAkQaZ6VzOUv
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)153Please respect copyright.PENANA3gbk9vzDWZ
-----------------------------------------------------
153Please respect copyright.PENANAeCSexgtXoj
Section C. if-match
When resuming a download in the browser download manager, the browser sends a range request with the if-match header like this:153Please respect copyright.PENANAFPlX9dJ43M
------resume download request-------153Please respect copyright.PENANAPkowzJEH0i
GET /zen.iso HTTP/1.1153Please respect copyright.PENANAwAoAsv6UYr
Range: bytes=287718144-153Please respect copyright.PENANArcW6Hfgk0d
If-Match: "534231200_1732277116"153Please respect copyright.PENANAv5hVSBhyR8
-----------------------------------153Please respect copyright.PENANAYSayZqvZd0
It tells the server to check the etag in If-Match, if unchanged, resume to get the remain content; otherwise, return 412 Precondition Failed.
The server response for unchanged etag is like this:153Please respect copyright.PENANA9i9mOP3VBQ
-------------range response-------------------153Please respect copyright.PENANAM1rnYqgfTg
HTTP/1.1 206 Partial Content153Please respect copyright.PENANAjOwnJgLaS3
accept-ranges: bytes153Please respect copyright.PENANA21BCYU4EWO
content-range: bytes 287718144-534231199/534231200153Please respect copyright.PENANAqldvQKqTEe
ETag: "534231200_1732277116"153Please respect copyright.PENANAfuhOe3oXsA
Content-Length: 246513056153Please respect copyright.PENANAJs7xYBnHwv
Content-Type: application/octet-stream153Please respect copyright.PENANAruGmQxOJAk
Content-Disposition: attachment; filename="zen.iso"
(http body is the remain file content)153Please respect copyright.PENANAlt3segDGNX
-------------------------------------------
If the etag changes, response 412 like this:153Please respect copyright.PENANA8LC3eQK6iW
-----------http response for changed resource------153Please respect copyright.PENANAXdLxSRMAX2
HTTP/1.1 412 Precondition Failed
(http body canbe empty, the client will stop downloading the resumed file)153Please respect copyright.PENANA7Sfjx6YatR
---------------------------------------------------
153Please respect copyright.PENANAf8nfLHrH52
Section D. if-range
If-Range is similar with If-Match, it's used by chrome when resuming a download in the download manager, whereas firefox use If-Match. They are the same except that when the etag changes, if-range response the resource content from beginning, so the resumed download will start downloading from beginning, while if-match just response the 412 error to stop the resumed download. I think chrome do a better job.
---resume download request by chrome---153Please respect copyright.PENANAS9C99E5Jff
GET /zen.iso HTTP/1.1153Please respect copyright.PENANAzfFioGB2DS
Range: bytes=287718144-153Please respect copyright.PENANA7j12VUEH6L
If-Range: "534231200_1732277116"153Please respect copyright.PENANAVxEaC9nFyn
------------------------------------153Please respect copyright.PENANACoQ3ocy4HS
It tells the server to check the etag in If-Range, if unchanged, resume to get the remain content; otherwise, return 200 OK and the file content from the beginning.
The server response for unchanged etag is like this:153Please respect copyright.PENANA0AO71ek6zx
-------------range response-------------------153Please respect copyright.PENANAuWVFBZPn4U
HTTP/1.1 206 Partial Content153Please respect copyright.PENANA8bZtvQfZki
accept-ranges: bytes153Please respect copyright.PENANAO0W2ThqgJs
content-range: bytes 287718144-534231199/534231200153Please respect copyright.PENANAxlAVH8zqtc
ETag: "534231200_1732277116"153Please respect copyright.PENANA3V3Qi5MMc0
Content-Length: 246513056153Please respect copyright.PENANABKalnSc9RN
Content-Type: application/octet-stream153Please respect copyright.PENANAjb1CCHPzDO
Content-Disposition: attachment; filename="zen.iso"
(http body is the remain file content)153Please respect copyright.PENANAKaN8vXccqU
-------------------------------------------
If the etag changes, response 200 from beginning like this:153Please respect copyright.PENANAyuUb8qaXK5
-----------http response from the beginning------153Please respect copyright.PENANAtPwYpmRY9p
HTTP/1.1 200 OK153Please respect copyright.PENANAQ9MFQvq50e
Date: Mon, 25 Nov 2024 10:56:12 GMT153Please respect copyright.PENANAU0V0ml5VnG
accept-ranges: bytes153Please respect copyright.PENANA137CkACO0f
ETag: "534231200_1732277116"153Please respect copyright.PENANAgQAHg5B2we
Content-Length: 534231200153Please respect copyright.PENANA6GKgWecHrL
Content-Type: application/octet-stream153Please respect copyright.PENANASPVscuuHE0
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)153Please respect copyright.PENANAtLPfUoqSiJ
---------------------------------------------------
153Please respect copyright.PENANATNwubZLbHd
Section X. Thanks
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match153Please respect copyright.PENANAhbofcx2kuP
https://blog.csdn.net/catoop/article/details/134174653153Please respect copyright.PENANAkxbNUfmpTC
https://blog.csdn.net/phker/article/details/50722619
Section Y. 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]153Please respect copyright.PENANA23frVes0uo
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: si_jinmin, 我的email: [email protected]
如果您對C/C++ Programming, Linux, HTTP Protocol, Website Development, Vue, Git, VsCode感興趣,邀請您加入「Linux/C/C++ Website Development」微信群,請加我的微信(si_jinmin)以便拉您进群。
ns216.73.216.51da2