本文描述了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.
132Please respect copyright.PENANA19GtUHqJUp
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:132Please respect copyright.PENANARTLHHN0oe1
---------------http response with etag----------------132Please respect copyright.PENANAq73vGxFvfs
HTTP/1.1 200 OK132Please respect copyright.PENANAJ6xPM7JUVk
Date: Mon, 25 Nov 2024 10:56:12 GMT132Please respect copyright.PENANACVITZdoQWK
accept-ranges: bytes132Please respect copyright.PENANADlPaMbF9jK
ETag: "534231200_1732277116"132Please respect copyright.PENANAluK23cQmZp
Content-Length: 534231200132Please respect copyright.PENANAD4euo3UyMr
Content-Type: application/octet-stream132Please respect copyright.PENANAWUY9IdV2MS
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)132Please respect copyright.PENANASK8An9i3SH
-----------------------------------------------------
132Please respect copyright.PENANATMe6foDdvm
Section B. if-none-match
The browser send back the etag to ask server if it has been changed by header like this:132Please respect copyright.PENANAepDPp1q8Sf
-----http request for a cached resource------132Please respect copyright.PENANArZYC1qgWua
GET /zen.iso HTTP/1.1132Please respect copyright.PENANADkIBjLA8Wb
if-none-match: "534231200_1732277116"132Please respect copyright.PENANAlHrIDdLrRp
---------------------------------------------
If the etag is unchanged, the server response like this:
1. for get and head requests:132Please respect copyright.PENANA1wWxONDlrJ
-----------http response for unchanged resource------132Please respect copyright.PENANAd8UCKn9M8V
HTTP/1.1 304 Not Modified
(http body canbe empty, the client will used its cache)132Please respect copyright.PENANAIkTvV3CHAq
----------------------------------------------------
2. for other methods requests:132Please respect copyright.PENANALFlqRqM3Gl
-----------http response for unchanged resource------132Please respect copyright.PENANAObhErTNJn8
HTTP/1.1 412 Precondition Failed
(http body canbe empty, the client will used its cache)132Please respect copyright.PENANAX30n9g0pRK
----------------------------------------------------
132Please respect copyright.PENANAScKPHdTtCD
If the etag changes, the server response 200 OK with the new file content like this:132Please respect copyright.PENANAx8Qi9Q1f3Q
---------------http response with etag----------------132Please respect copyright.PENANAfIfGJDkNdJ
HTTP/1.1 200 OK132Please respect copyright.PENANAS6TNy2PGbu
Date: Mon, 25 Nov 2024 10:56:12 GMT132Please respect copyright.PENANA6bCxI5s0DI
accept-ranges: bytes132Please respect copyright.PENANAjTVDdeSTLP
ETag: "534231200_1732277116"132Please respect copyright.PENANAX6Yf2Q91k6
Content-Length: 534231200132Please respect copyright.PENANA4BCdZmOyDd
Content-Type: application/octet-stream132Please respect copyright.PENANAOmw8Bhln6O
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)132Please respect copyright.PENANAwZe73d4fxr
-----------------------------------------------------
132Please respect copyright.PENANAAytaYKHfLd
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:132Please respect copyright.PENANAD2Mf2vnSet
------resume download request-------132Please respect copyright.PENANAdcwLeyZKGa
GET /zen.iso HTTP/1.1132Please respect copyright.PENANAsMwWc809eK
Range: bytes=287718144-132Please respect copyright.PENANA2kyxAMWjwQ
If-Match: "534231200_1732277116"132Please respect copyright.PENANAKvITMesCIC
-----------------------------------132Please respect copyright.PENANAtjiJ7DBdS1
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:132Please respect copyright.PENANAdVaiR7uBv2
-------------range response-------------------132Please respect copyright.PENANAkfacsQQJIz
HTTP/1.1 206 Partial Content132Please respect copyright.PENANAtX8jStBnNx
accept-ranges: bytes132Please respect copyright.PENANA2OwQH9jOeE
content-range: bytes 287718144-534231199/534231200132Please respect copyright.PENANA1onwAlFBNu
ETag: "534231200_1732277116"132Please respect copyright.PENANAT6vkU2YOd8
Content-Length: 246513056132Please respect copyright.PENANAkksMhKADnj
Content-Type: application/octet-stream132Please respect copyright.PENANAlYC2jAQZAR
Content-Disposition: attachment; filename="zen.iso"
(http body is the remain file content)132Please respect copyright.PENANAjGYlyhreZI
-------------------------------------------
If the etag changes, response 412 like this:132Please respect copyright.PENANAmp59AGtLPg
-----------http response for changed resource------132Please respect copyright.PENANAujYWBbvx6M
HTTP/1.1 412 Precondition Failed
(http body canbe empty, the client will stop downloading the resumed file)132Please respect copyright.PENANAE5b0QkrtaM
---------------------------------------------------
132Please respect copyright.PENANAZa05c3TFgz
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---132Please respect copyright.PENANAS50IAe6E3P
GET /zen.iso HTTP/1.1132Please respect copyright.PENANAvHJbWYzM9z
Range: bytes=287718144-132Please respect copyright.PENANAor3KKbNUa2
If-Range: "534231200_1732277116"132Please respect copyright.PENANAhh9Lz9vH9R
------------------------------------132Please respect copyright.PENANApUFTBVzVra
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:132Please respect copyright.PENANA7nQ2op6LH7
-------------range response-------------------132Please respect copyright.PENANApt6R9dhNeO
HTTP/1.1 206 Partial Content132Please respect copyright.PENANA643HEm9Fo9
accept-ranges: bytes132Please respect copyright.PENANAZ8W7I2vOrz
content-range: bytes 287718144-534231199/534231200132Please respect copyright.PENANAH4eUIEhcVI
ETag: "534231200_1732277116"132Please respect copyright.PENANAFThKPdtrg2
Content-Length: 246513056132Please respect copyright.PENANAO9mW084MC9
Content-Type: application/octet-stream132Please respect copyright.PENANAB315m3at1W
Content-Disposition: attachment; filename="zen.iso"
(http body is the remain file content)132Please respect copyright.PENANAWts10W8sUP
-------------------------------------------
If the etag changes, response 200 from beginning like this:132Please respect copyright.PENANATeAmo0vgmN
-----------http response from the beginning------132Please respect copyright.PENANACWNafixt1T
HTTP/1.1 200 OK132Please respect copyright.PENANAESXApOLjj0
Date: Mon, 25 Nov 2024 10:56:12 GMT132Please respect copyright.PENANAAz9eAdGRRs
accept-ranges: bytes132Please respect copyright.PENANArUE2fKwMYO
ETag: "534231200_1732277116"132Please respect copyright.PENANA4Ysg7oz5Mt
Content-Length: 534231200132Please respect copyright.PENANAmVzhxwXng1
Content-Type: application/octet-stream132Please respect copyright.PENANArS6jnUU2R2
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)132Please respect copyright.PENANAfo9kYoV7i6
---------------------------------------------------
132Please respect copyright.PENANA6Y43Keiefo
Section X. Thanks
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match132Please respect copyright.PENANAWvIULPimtj
https://blog.csdn.net/catoop/article/details/134174653132Please respect copyright.PENANAXrqm3ACCuP
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]132Please respect copyright.PENANAFyc0tnBamC
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: si_jinmin, 我的email: [email protected]
如果您對C/C++ Programming, Linux, HTTP Protocol, Website Development, Vue, Git, VsCode感興趣,邀請您加入「Linux/C/C++ Website Development」微信群,請加我的微信(si_jinmin)以便拉您进群。
ns18.220.129.249da2