本文描述了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.
94Please respect copyright.PENANAu7OexP54o9
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:94Please respect copyright.PENANAwA9xkI84LZ
---------------http response with etag----------------94Please respect copyright.PENANACV3ok4WKba
HTTP/1.1 200 OK94Please respect copyright.PENANAswSmUt5Xo9
Date: Mon, 25 Nov 2024 10:56:12 GMT94Please respect copyright.PENANA6NCjDOxslF
accept-ranges: bytes94Please respect copyright.PENANAmGIjWYNmlY
ETag: "534231200_1732277116"94Please respect copyright.PENANAvZ61YnhoI3
Content-Length: 53423120094Please respect copyright.PENANAWTgR0sWh7M
Content-Type: application/octet-stream94Please respect copyright.PENANAwDgb7FRRjt
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)94Please respect copyright.PENANAINVC7B0JVF
-----------------------------------------------------
94Please respect copyright.PENANAg9IgSXj5QZ
Section B. if-none-match
The browser send back the etag to ask server if it has been changed by header like this:94Please respect copyright.PENANAOoKjcBRzaI
-----http request for a cached resource------94Please respect copyright.PENANA09r6g50Gw4
GET /zen.iso HTTP/1.194Please respect copyright.PENANApcbgtGD6Dv
if-none-match: "534231200_1732277116"94Please respect copyright.PENANAo9qFH7dmVJ
---------------------------------------------
If the etag is unchanged, the server response like this:
1. for get and head requests:94Please respect copyright.PENANAFR44krPwdl
-----------http response for unchanged resource------94Please respect copyright.PENANAzPNsVzD1ER
HTTP/1.1 304 Not Modified
(http body canbe empty, the client will used its cache)94Please respect copyright.PENANABlbAxJkM0E
----------------------------------------------------
2. for other methods requests:94Please respect copyright.PENANAyF5jM0fLwo
-----------http response for unchanged resource------94Please respect copyright.PENANA7rfhH5MGr7
HTTP/1.1 412 Precondition Failed
(http body canbe empty, the client will used its cache)94Please respect copyright.PENANARewmWsWlvy
----------------------------------------------------
94Please respect copyright.PENANAh0JEnHsuCQ
If the etag changes, the server response 200 OK with the new file content like this:94Please respect copyright.PENANAohffJAaBIf
---------------http response with etag----------------94Please respect copyright.PENANAF39RNXb0cN
HTTP/1.1 200 OK94Please respect copyright.PENANAkAhOnEtKbM
Date: Mon, 25 Nov 2024 10:56:12 GMT94Please respect copyright.PENANAWlDmkFGLLC
accept-ranges: bytes94Please respect copyright.PENANAHx4azvDeyL
ETag: "534231200_1732277116"94Please respect copyright.PENANA3bKGIyhA0d
Content-Length: 53423120094Please respect copyright.PENANAjbSovCh0gm
Content-Type: application/octet-stream94Please respect copyright.PENANAk94qI8q2rn
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)94Please respect copyright.PENANAmalunmp3W1
-----------------------------------------------------
94Please respect copyright.PENANAO0Jh4wCmm1
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:94Please respect copyright.PENANAoM0DvifPeD
------resume download request-------94Please respect copyright.PENANAheVLIQeB6b
GET /zen.iso HTTP/1.194Please respect copyright.PENANASNHxEOrNMn
Range: bytes=287718144-94Please respect copyright.PENANAax6jVuc6FX
If-Match: "534231200_1732277116"94Please respect copyright.PENANALGu90CIuIf
-----------------------------------94Please respect copyright.PENANAFOXYi3qThn
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:94Please respect copyright.PENANAJaPZayCDsf
-------------range response-------------------94Please respect copyright.PENANA8jvpFMlx7Q
HTTP/1.1 206 Partial Content94Please respect copyright.PENANAaQodM4P0qf
accept-ranges: bytes94Please respect copyright.PENANAblusNR2ysS
content-range: bytes 287718144-534231199/53423120094Please respect copyright.PENANA8SPAhQGThf
ETag: "534231200_1732277116"94Please respect copyright.PENANAoRN3uWqCsc
Content-Length: 24651305694Please respect copyright.PENANAIhdyTKZFyK
Content-Type: application/octet-stream94Please respect copyright.PENANAsYT0RUpaJn
Content-Disposition: attachment; filename="zen.iso"
(http body is the remain file content)94Please respect copyright.PENANAIgHl2ID3Ne
-------------------------------------------
If the etag changes, response 412 like this:94Please respect copyright.PENANABTU3btw255
-----------http response for changed resource------94Please respect copyright.PENANAwhsMBeACVc
HTTP/1.1 412 Precondition Failed
(http body canbe empty, the client will stop downloading the resumed file)94Please respect copyright.PENANAegjha1XnCY
---------------------------------------------------
94Please respect copyright.PENANAApfSuquxVM
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---94Please respect copyright.PENANAh7gySjuGbz
GET /zen.iso HTTP/1.194Please respect copyright.PENANAnp2XaY7r5j
Range: bytes=287718144-94Please respect copyright.PENANASumB7T0EBU
If-Range: "534231200_1732277116"94Please respect copyright.PENANAKd67sLRtZh
------------------------------------94Please respect copyright.PENANAYINoao1CYV
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:94Please respect copyright.PENANAUyahK4415r
-------------range response-------------------94Please respect copyright.PENANAJhlnVsMeS4
HTTP/1.1 206 Partial Content94Please respect copyright.PENANA7RqdpOvDEy
accept-ranges: bytes94Please respect copyright.PENANA2ieu48o5Te
content-range: bytes 287718144-534231199/53423120094Please respect copyright.PENANAYzHmCQEjxr
ETag: "534231200_1732277116"94Please respect copyright.PENANAShNxxLD4Cy
Content-Length: 24651305694Please respect copyright.PENANAXeO4VkeLag
Content-Type: application/octet-stream94Please respect copyright.PENANADQev98CqqB
Content-Disposition: attachment; filename="zen.iso"
(http body is the remain file content)94Please respect copyright.PENANAsQ7ypgd0LR
-------------------------------------------
If the etag changes, response 200 from beginning like this:94Please respect copyright.PENANAYq0CN5AIpt
-----------http response from the beginning------94Please respect copyright.PENANADqmodCqXX5
HTTP/1.1 200 OK94Please respect copyright.PENANAeBENET4lJt
Date: Mon, 25 Nov 2024 10:56:12 GMT94Please respect copyright.PENANAbkaiPDvRxx
accept-ranges: bytes94Please respect copyright.PENANAL26ttnYLiY
ETag: "534231200_1732277116"94Please respect copyright.PENANAkgAXiX35Z7
Content-Length: 53423120094Please respect copyright.PENANAe9tIz7QmrI
Content-Type: application/octet-stream94Please respect copyright.PENANACbp54dySSa
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)94Please respect copyright.PENANA09f19NXNsY
---------------------------------------------------
94Please respect copyright.PENANAu3Yvw85rPZ
Section X. Thanks
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match94Please respect copyright.PENANAtlxVEqaFGc
https://blog.csdn.net/catoop/article/details/13417465394Please respect copyright.PENANArMBYQbtON7
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]94Please respect copyright.PENANABaRa9jem4V
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: si_jinmin, 我的email: [email protected]
如果您對C/C++ Programming, Linux, HTTP Protocol, Website Development, Vue, Git, VsCode感興趣,邀請您加入「Linux/C/C++ Website Development」微信群,請加我的微信(si_jinmin)以便拉您进群。
ns18.222.34.209da2