本文描述了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.
353Please respect copyright.PENANAPkwZJd1s72
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:353Please respect copyright.PENANAmpTePV4934
---------------http response with etag----------------353Please respect copyright.PENANA5WtkddS7Ws
HTTP/1.1 200 OK353Please respect copyright.PENANAQmCSoYTSfN
Date: Mon, 25 Nov 2024 10:56:12 GMT353Please respect copyright.PENANA7kSIinC1rg
accept-ranges: bytes353Please respect copyright.PENANA6t88kASIkR
ETag: "534231200_1732277116"353Please respect copyright.PENANAMmYXsuX5cn
Content-Length: 534231200353Please respect copyright.PENANA7SuDPZpazT
Content-Type: application/octet-stream353Please respect copyright.PENANAZGAU6oIvyE
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)353Please respect copyright.PENANAIhmCsCJ3n7
-----------------------------------------------------
353Please respect copyright.PENANAC8cR1ho1zC
Section B. if-none-match
The browser send back the etag to ask server if it has been changed by header like this:353Please respect copyright.PENANAubCSA8lWU9
-----http request for a cached resource------353Please respect copyright.PENANADeUFBt4iby
GET /zen.iso HTTP/1.1353Please respect copyright.PENANAkcaKZ0w59A
if-none-match: "534231200_1732277116"353Please respect copyright.PENANA214T7CU2Pz
---------------------------------------------
If the etag is unchanged, the server response like this:
1. for get and head requests:353Please respect copyright.PENANAJ8SywfXuwg
-----------http response for unchanged resource------353Please respect copyright.PENANAyT5NWy0n0y
HTTP/1.1 304 Not Modified
(http body canbe empty, the client will used its cache)353Please respect copyright.PENANAAZGWWEUqTJ
----------------------------------------------------
2. for other methods requests:353Please respect copyright.PENANAxo42Q7QEGO
-----------http response for unchanged resource------353Please respect copyright.PENANAGBhbdAU2JW
HTTP/1.1 412 Precondition Failed
(http body canbe empty, the client will used its cache)353Please respect copyright.PENANAfx5w9RyjAB
----------------------------------------------------
353Please respect copyright.PENANASDHbAhHEyv
If the etag changes, the server response 200 OK with the new file content like this:353Please respect copyright.PENANAcjJxPWB6Uj
---------------http response with etag----------------353Please respect copyright.PENANAxs5ld0olfB
HTTP/1.1 200 OK353Please respect copyright.PENANAT90WuaQy1f
Date: Mon, 25 Nov 2024 10:56:12 GMT353Please respect copyright.PENANAaGO9eMGAV0
accept-ranges: bytes353Please respect copyright.PENANAULT6N3C9Qq
ETag: "534231200_1732277116"353Please respect copyright.PENANAmqiopgZhti
Content-Length: 534231200353Please respect copyright.PENANAW0XiO3HGjR
Content-Type: application/octet-stream353Please respect copyright.PENANAOn9Wkp0ZSD
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)353Please respect copyright.PENANA53LUJLpb9f
-----------------------------------------------------
353Please respect copyright.PENANAbRqYl5oWpF
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:353Please respect copyright.PENANAOKBCa46WOT
------resume download request-------353Please respect copyright.PENANAQ46WGxPez4
GET /zen.iso HTTP/1.1353Please respect copyright.PENANAX67SEj2vCd
Range: bytes=287718144-353Please respect copyright.PENANAdOigdgnuiz
If-Match: "534231200_1732277116"353Please respect copyright.PENANAUlIw7mr3BZ
-----------------------------------353Please respect copyright.PENANAyqi7D9cMA1
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:353Please respect copyright.PENANA01Evn83U23
-------------range response-------------------353Please respect copyright.PENANASdCHKLAynE
HTTP/1.1 206 Partial Content353Please respect copyright.PENANAXuTQlihDNO
accept-ranges: bytes353Please respect copyright.PENANAL77TgpKs6B
content-range: bytes 287718144-534231199/534231200353Please respect copyright.PENANAH2ewSFlylt
ETag: "534231200_1732277116"353Please respect copyright.PENANAZgJpkx8dh7
Content-Length: 246513056353Please respect copyright.PENANAj8yJUdo1lk
Content-Type: application/octet-stream353Please respect copyright.PENANABO2rlko8tZ
Content-Disposition: attachment; filename="zen.iso"
(http body is the remain file content)353Please respect copyright.PENANAPnTf4unyY0
-------------------------------------------
If the etag changes, response 412 like this:353Please respect copyright.PENANALWAhLcSqei
-----------http response for changed resource------353Please respect copyright.PENANAdtNgyIT4tC
HTTP/1.1 412 Precondition Failed
(http body canbe empty, the client will stop downloading the resumed file)353Please respect copyright.PENANA8lLHpfCGCv
---------------------------------------------------
353Please respect copyright.PENANAuuDoN27TT5
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---353Please respect copyright.PENANA5aSr6b2az5
GET /zen.iso HTTP/1.1353Please respect copyright.PENANAuZ7Jc4HF4d
Range: bytes=287718144-353Please respect copyright.PENANAV2cNKwTbCo
If-Range: "534231200_1732277116"353Please respect copyright.PENANAVTnBQtWv9C
------------------------------------353Please respect copyright.PENANAZ6yasbkpRz
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:353Please respect copyright.PENANAoiIv1erD5P
-------------range response-------------------353Please respect copyright.PENANARDwnz6qwLV
HTTP/1.1 206 Partial Content353Please respect copyright.PENANA3zSuR6hR4s
accept-ranges: bytes353Please respect copyright.PENANAEfYicy0gcu
content-range: bytes 287718144-534231199/534231200353Please respect copyright.PENANA0OCYmZHaQB
ETag: "534231200_1732277116"353Please respect copyright.PENANAPSCnRmWPTL
Content-Length: 246513056353Please respect copyright.PENANAZtN6nnfZ4o
Content-Type: application/octet-stream353Please respect copyright.PENANAi1CSQ7TyPy
Content-Disposition: attachment; filename="zen.iso"
(http body is the remain file content)353Please respect copyright.PENANAFqo62nJz2Z
-------------------------------------------
If the etag changes, response 200 from beginning like this:353Please respect copyright.PENANAadyZD4tzJp
-----------http response from the beginning------353Please respect copyright.PENANAGlrdQ8UJe4
HTTP/1.1 200 OK353Please respect copyright.PENANAWCyr30IhCs
Date: Mon, 25 Nov 2024 10:56:12 GMT353Please respect copyright.PENANANVQt9E1KZO
accept-ranges: bytes353Please respect copyright.PENANANxVgZhfFNi
ETag: "534231200_1732277116"353Please respect copyright.PENANAoi6eMDCAjA
Content-Length: 534231200353Please respect copyright.PENANAxeanF7W7s6
Content-Type: application/octet-stream353Please respect copyright.PENANAVvM2U8bTft
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)353Please respect copyright.PENANAntUS7cbTg1
---------------------------------------------------
353Please respect copyright.PENANACzbENpvnMX
Section X. Thanks
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match353Please respect copyright.PENANAYeft79kWcK
https://blog.csdn.net/catoop/article/details/134174653353Please respect copyright.PENANAqtEF1O7RnA
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]353Please respect copyright.PENANAKpD9WrCkuo
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: 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.134da2


