本文描述了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.
189Please respect copyright.PENANAPBdICeybPw
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:189Please respect copyright.PENANAeyywpXJH92
---------------http response with etag----------------189Please respect copyright.PENANAtrfLex4Q89
HTTP/1.1 200 OK189Please respect copyright.PENANAOewzAvosQi
Date: Mon, 25 Nov 2024 10:56:12 GMT189Please respect copyright.PENANA6uJATDIA2s
accept-ranges: bytes189Please respect copyright.PENANABX4r1X6z5d
ETag: "534231200_1732277116"189Please respect copyright.PENANA8fv1dqFrBt
Content-Length: 534231200189Please respect copyright.PENANAEIyOISSuQn
Content-Type: application/octet-stream189Please respect copyright.PENANAUAuY5HUBCv
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)189Please respect copyright.PENANAE8YJGzDNMu
-----------------------------------------------------
189Please respect copyright.PENANAUEImKrdZ9C
Section B. if-none-match
The browser send back the etag to ask server if it has been changed by header like this:189Please respect copyright.PENANASBW9KOTgV7
-----http request for a cached resource------189Please respect copyright.PENANA2241b8CdcH
GET /zen.iso HTTP/1.1189Please respect copyright.PENANAYOCJOoj6no
if-none-match: "534231200_1732277116"189Please respect copyright.PENANAFUE9hp3IGz
---------------------------------------------
If the etag is unchanged, the server response like this:
1. for get and head requests:189Please respect copyright.PENANADYHg3whI86
-----------http response for unchanged resource------189Please respect copyright.PENANAzZa5HL6vgn
HTTP/1.1 304 Not Modified
(http body canbe empty, the client will used its cache)189Please respect copyright.PENANAJ4QFQouTOU
----------------------------------------------------
2. for other methods requests:189Please respect copyright.PENANA03kxWP3J1o
-----------http response for unchanged resource------189Please respect copyright.PENANAiSLOad0bkY
HTTP/1.1 412 Precondition Failed
(http body canbe empty, the client will used its cache)189Please respect copyright.PENANALTffFfiwQi
----------------------------------------------------
189Please respect copyright.PENANAvyHJ4GHb6j
If the etag changes, the server response 200 OK with the new file content like this:189Please respect copyright.PENANA5tPlsFMCdw
---------------http response with etag----------------189Please respect copyright.PENANABiC1vLY0mD
HTTP/1.1 200 OK189Please respect copyright.PENANAnarrqcx6c9
Date: Mon, 25 Nov 2024 10:56:12 GMT189Please respect copyright.PENANA5oW2UsBlZe
accept-ranges: bytes189Please respect copyright.PENANAAkx4NIt8qS
ETag: "534231200_1732277116"189Please respect copyright.PENANANMRNrWkqkU
Content-Length: 534231200189Please respect copyright.PENANAQ9VelOc091
Content-Type: application/octet-stream189Please respect copyright.PENANAzwZg33jZsG
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)189Please respect copyright.PENANALTBZxY80O6
-----------------------------------------------------
189Please respect copyright.PENANAvHSZT1vAqp
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:189Please respect copyright.PENANAoCkbg2wNb0
------resume download request-------189Please respect copyright.PENANANNQXRU0BS1
GET /zen.iso HTTP/1.1189Please respect copyright.PENANAzRuPrpSGun
Range: bytes=287718144-189Please respect copyright.PENANAyurzPqFiX1
If-Match: "534231200_1732277116"189Please respect copyright.PENANABbD6OXO8BI
-----------------------------------189Please respect copyright.PENANAPs57q7jAn2
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:189Please respect copyright.PENANAs4WwmG0TZ8
-------------range response-------------------189Please respect copyright.PENANAPp1RHb1T5b
HTTP/1.1 206 Partial Content189Please respect copyright.PENANA1gNt1soaS8
accept-ranges: bytes189Please respect copyright.PENANA4CAC1SlWCE
content-range: bytes 287718144-534231199/534231200189Please respect copyright.PENANAdxcwcgLyMe
ETag: "534231200_1732277116"189Please respect copyright.PENANATufgVxIwoG
Content-Length: 246513056189Please respect copyright.PENANAjbR8DZj1Ke
Content-Type: application/octet-stream189Please respect copyright.PENANAo9eAh2mREL
Content-Disposition: attachment; filename="zen.iso"
(http body is the remain file content)189Please respect copyright.PENANAbipN0zPJyE
-------------------------------------------
If the etag changes, response 412 like this:189Please respect copyright.PENANAKu8GEK9KWA
-----------http response for changed resource------189Please respect copyright.PENANAplKVxCnzr8
HTTP/1.1 412 Precondition Failed
(http body canbe empty, the client will stop downloading the resumed file)189Please respect copyright.PENANAm3aDF89fsC
---------------------------------------------------
189Please respect copyright.PENANAcrIoKat7kb
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---189Please respect copyright.PENANAJmhKRcyATt
GET /zen.iso HTTP/1.1189Please respect copyright.PENANAllLgG2ybty
Range: bytes=287718144-189Please respect copyright.PENANAYqITQlobP4
If-Range: "534231200_1732277116"189Please respect copyright.PENANAyS4hPs3RV0
------------------------------------189Please respect copyright.PENANARiUOPS7EIV
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:189Please respect copyright.PENANATW7BdhO41F
-------------range response-------------------189Please respect copyright.PENANADjVTHzCGoW
HTTP/1.1 206 Partial Content189Please respect copyright.PENANA0LazTN2tZT
accept-ranges: bytes189Please respect copyright.PENANAcu7gMRNAhK
content-range: bytes 287718144-534231199/534231200189Please respect copyright.PENANAqsNiTgzH4D
ETag: "534231200_1732277116"189Please respect copyright.PENANA322DTf4YE4
Content-Length: 246513056189Please respect copyright.PENANAmRLb1Cc9xi
Content-Type: application/octet-stream189Please respect copyright.PENANA3wNAXvkOLb
Content-Disposition: attachment; filename="zen.iso"
(http body is the remain file content)189Please respect copyright.PENANAeTBFRfUZ28
-------------------------------------------
If the etag changes, response 200 from beginning like this:189Please respect copyright.PENANA11bo7zgXAz
-----------http response from the beginning------189Please respect copyright.PENANAoJd5uUCNgA
HTTP/1.1 200 OK189Please respect copyright.PENANAt414iJ4aC3
Date: Mon, 25 Nov 2024 10:56:12 GMT189Please respect copyright.PENANAo93i5rnB4X
accept-ranges: bytes189Please respect copyright.PENANAt8GJeaeS2g
ETag: "534231200_1732277116"189Please respect copyright.PENANARG80yLLZhu
Content-Length: 534231200189Please respect copyright.PENANAbeFARLAjr7
Content-Type: application/octet-stream189Please respect copyright.PENANARPkhVs118y
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)189Please respect copyright.PENANAC5xk7Xa6zT
---------------------------------------------------
189Please respect copyright.PENANAyufg8XE25c
Section X. Thanks
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match189Please respect copyright.PENANAA8HHKeH7kt
https://blog.csdn.net/catoop/article/details/134174653189Please respect copyright.PENANAjtO5x5shCP
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]189Please respect copyright.PENANADO3vNizRa4
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: 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.85da2