本文描述了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.
208Please respect copyright.PENANAWA43WvCC5r
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:208Please respect copyright.PENANAeOnUVUeLpt
---------------http response with etag----------------208Please respect copyright.PENANAcYmOK80ypF
HTTP/1.1 200 OK208Please respect copyright.PENANAhQ35SmAd11
Date: Mon, 25 Nov 2024 10:56:12 GMT208Please respect copyright.PENANAYJO5n9vcfp
accept-ranges: bytes208Please respect copyright.PENANAMevQD4VsEO
ETag: "534231200_1732277116"208Please respect copyright.PENANAXaGOBh1Qy7
Content-Length: 534231200208Please respect copyright.PENANACkONRsHljb
Content-Type: application/octet-stream208Please respect copyright.PENANAowxS38bUQN
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)208Please respect copyright.PENANAR5ZmAjPpsa
-----------------------------------------------------
208Please respect copyright.PENANAfRsbuIcm7r
Section B. if-none-match
The browser send back the etag to ask server if it has been changed by header like this:208Please respect copyright.PENANARU16ek5Pqq
-----http request for a cached resource------208Please respect copyright.PENANAsDp3UnERkf
GET /zen.iso HTTP/1.1208Please respect copyright.PENANAhwJuUgWnXa
if-none-match: "534231200_1732277116"208Please respect copyright.PENANAkNfpNeXvFK
---------------------------------------------
If the etag is unchanged, the server response like this:
1. for get and head requests:208Please respect copyright.PENANArsDEpIKE2k
-----------http response for unchanged resource------208Please respect copyright.PENANAdWYdPLTWAm
HTTP/1.1 304 Not Modified
(http body canbe empty, the client will used its cache)208Please respect copyright.PENANAQUY9T7WRrF
----------------------------------------------------
2. for other methods requests:208Please respect copyright.PENANAJMecVe6T2i
-----------http response for unchanged resource------208Please respect copyright.PENANAHIezfqjZia
HTTP/1.1 412 Precondition Failed
(http body canbe empty, the client will used its cache)208Please respect copyright.PENANA3vPTscfp92
----------------------------------------------------
208Please respect copyright.PENANAh4ZVDGZQO4
If the etag changes, the server response 200 OK with the new file content like this:208Please respect copyright.PENANAeOkIlJtRMu
---------------http response with etag----------------208Please respect copyright.PENANAzL1jwN84VU
HTTP/1.1 200 OK208Please respect copyright.PENANAeR0xnTgBv9
Date: Mon, 25 Nov 2024 10:56:12 GMT208Please respect copyright.PENANApzCUl7ZWd9
accept-ranges: bytes208Please respect copyright.PENANApaCAOd0U5V
ETag: "534231200_1732277116"208Please respect copyright.PENANAeKVEjlDBgo
Content-Length: 534231200208Please respect copyright.PENANAndfbZ2Uevk
Content-Type: application/octet-stream208Please respect copyright.PENANAIr8ezKOYcH
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)208Please respect copyright.PENANAbB57SulXtR
-----------------------------------------------------
208Please respect copyright.PENANAWiocLa4XFx
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:208Please respect copyright.PENANACKyEZs1tIG
------resume download request-------208Please respect copyright.PENANA7p7TAjAXW9
GET /zen.iso HTTP/1.1208Please respect copyright.PENANAVkvN6SaTAx
Range: bytes=287718144-208Please respect copyright.PENANADoMUpOJIax
If-Match: "534231200_1732277116"208Please respect copyright.PENANA4pV5hGZ28k
-----------------------------------208Please respect copyright.PENANAHW0y5Q0TZm
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:208Please respect copyright.PENANAFOSnGYjDtR
-------------range response-------------------208Please respect copyright.PENANAPvXRO8P78h
HTTP/1.1 206 Partial Content208Please respect copyright.PENANAuY49mZ7GC2
accept-ranges: bytes208Please respect copyright.PENANAoeb7L9ycKM
content-range: bytes 287718144-534231199/534231200208Please respect copyright.PENANABA4Zwuh14Q
ETag: "534231200_1732277116"208Please respect copyright.PENANAmipjau3WrP
Content-Length: 246513056208Please respect copyright.PENANARlfd5rwuDq
Content-Type: application/octet-stream208Please respect copyright.PENANAviBY39w39E
Content-Disposition: attachment; filename="zen.iso"
(http body is the remain file content)208Please respect copyright.PENANAQbihdfGiqb
-------------------------------------------
If the etag changes, response 412 like this:208Please respect copyright.PENANAmFJM21pPpQ
-----------http response for changed resource------208Please respect copyright.PENANAg3kPZvjmPH
HTTP/1.1 412 Precondition Failed
(http body canbe empty, the client will stop downloading the resumed file)208Please respect copyright.PENANA4YVLPW4wsq
---------------------------------------------------
208Please respect copyright.PENANAfUu3GRXaL2
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---208Please respect copyright.PENANAhqfOCUXo2M
GET /zen.iso HTTP/1.1208Please respect copyright.PENANA8wgl3FrSvI
Range: bytes=287718144-208Please respect copyright.PENANAxWE9uvhVSY
If-Range: "534231200_1732277116"208Please respect copyright.PENANAwm9s82Q50J
------------------------------------208Please respect copyright.PENANAAvUIk0y829
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:208Please respect copyright.PENANANq5aY32eY7
-------------range response-------------------208Please respect copyright.PENANAYtlBAyzw2N
HTTP/1.1 206 Partial Content208Please respect copyright.PENANABwMFqTvEwg
accept-ranges: bytes208Please respect copyright.PENANAb9hbXgnrj5
content-range: bytes 287718144-534231199/534231200208Please respect copyright.PENANAnf2atrpS7n
ETag: "534231200_1732277116"208Please respect copyright.PENANAsjTQZ851Dg
Content-Length: 246513056208Please respect copyright.PENANA27Y4JNCuma
Content-Type: application/octet-stream208Please respect copyright.PENANAW8bN4KgYsf
Content-Disposition: attachment; filename="zen.iso"
(http body is the remain file content)208Please respect copyright.PENANAcetnap29mE
-------------------------------------------
If the etag changes, response 200 from beginning like this:208Please respect copyright.PENANAC79BMyXFnR
-----------http response from the beginning------208Please respect copyright.PENANAJzfTzwtsn3
HTTP/1.1 200 OK208Please respect copyright.PENANAKyeDQpFkNz
Date: Mon, 25 Nov 2024 10:56:12 GMT208Please respect copyright.PENANA3g2WEidDkS
accept-ranges: bytes208Please respect copyright.PENANAMNaSwR97hF
ETag: "534231200_1732277116"208Please respect copyright.PENANAdhQUBieqxL
Content-Length: 534231200208Please respect copyright.PENANAkmwSJDs3iO
Content-Type: application/octet-stream208Please respect copyright.PENANAayqCzkILOi
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)208Please respect copyright.PENANAvcmjVISW9A
---------------------------------------------------
208Please respect copyright.PENANAsPzimOsZMG
Section X. Thanks
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match208Please respect copyright.PENANAFJ4BaH2aXh
https://blog.csdn.net/catoop/article/details/134174653208Please respect copyright.PENANAriM8vLxmGV
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]208Please respect copyright.PENANAoOZOEfgajG
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: 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.251da2