本文描述了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.
209Please respect copyright.PENANAwbs0sU90EB
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:209Please respect copyright.PENANAxyhgPNZOpn
---------------http response with etag----------------209Please respect copyright.PENANAtOSjcg3Ajt
HTTP/1.1 200 OK209Please respect copyright.PENANAGa9vdj3Hnu
Date: Mon, 25 Nov 2024 10:56:12 GMT209Please respect copyright.PENANAwktNzi9CH7
accept-ranges: bytes209Please respect copyright.PENANApt2dIlIs1S
ETag: "534231200_1732277116"209Please respect copyright.PENANAItC2JOK3xT
Content-Length: 534231200209Please respect copyright.PENANAGcPtwjOgkP
Content-Type: application/octet-stream209Please respect copyright.PENANAGAfXekd1Gp
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)209Please respect copyright.PENANAkdpoS9etCE
-----------------------------------------------------
209Please respect copyright.PENANAnMFi77onYT
Section B. if-none-match
The browser send back the etag to ask server if it has been changed by header like this:209Please respect copyright.PENANA3X1O72ZET7
-----http request for a cached resource------209Please respect copyright.PENANAxPdR7FNX0h
GET /zen.iso HTTP/1.1209Please respect copyright.PENANAQJijLg8z3v
if-none-match: "534231200_1732277116"209Please respect copyright.PENANAvTbK922njm
---------------------------------------------
If the etag is unchanged, the server response like this:
1. for get and head requests:209Please respect copyright.PENANAP3laAdmPfp
-----------http response for unchanged resource------209Please respect copyright.PENANAvEeh5aCGn0
HTTP/1.1 304 Not Modified
(http body canbe empty, the client will used its cache)209Please respect copyright.PENANAmqzEeHmIgN
----------------------------------------------------
2. for other methods requests:209Please respect copyright.PENANAk5ZwjDG1gC
-----------http response for unchanged resource------209Please respect copyright.PENANAUTvTgpet7h
HTTP/1.1 412 Precondition Failed
(http body canbe empty, the client will used its cache)209Please respect copyright.PENANAdOsOKtoC5e
----------------------------------------------------
209Please respect copyright.PENANAWAVf62jUmq
If the etag changes, the server response 200 OK with the new file content like this:209Please respect copyright.PENANA4duLjtEA0c
---------------http response with etag----------------209Please respect copyright.PENANAJd3koqdNou
HTTP/1.1 200 OK209Please respect copyright.PENANAwTS90HR4Vw
Date: Mon, 25 Nov 2024 10:56:12 GMT209Please respect copyright.PENANAPSmPqVhHtM
accept-ranges: bytes209Please respect copyright.PENANAFMobhNSdMc
ETag: "534231200_1732277116"209Please respect copyright.PENANAVwqJnMFiIa
Content-Length: 534231200209Please respect copyright.PENANAMOCoRpGm5g
Content-Type: application/octet-stream209Please respect copyright.PENANAIvG6fpcIc1
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)209Please respect copyright.PENANAlGg1UhBmHn
-----------------------------------------------------
209Please respect copyright.PENANA5vJFPn9jXA
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:209Please respect copyright.PENANAXmYJb8ffPB
------resume download request-------209Please respect copyright.PENANAnFrEX1wInJ
GET /zen.iso HTTP/1.1209Please respect copyright.PENANAEMkAfFdtok
Range: bytes=287718144-209Please respect copyright.PENANAtE3qHOQmad
If-Match: "534231200_1732277116"209Please respect copyright.PENANAZBPwJXybGS
-----------------------------------209Please respect copyright.PENANAxl6ajPvUql
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:209Please respect copyright.PENANA4sFtBC8Ytt
-------------range response-------------------209Please respect copyright.PENANABSgLBHYrhG
HTTP/1.1 206 Partial Content209Please respect copyright.PENANAuSLFFfpsRm
accept-ranges: bytes209Please respect copyright.PENANAX6QSEyrqTf
content-range: bytes 287718144-534231199/534231200209Please respect copyright.PENANAvfaBJQZt0G
ETag: "534231200_1732277116"209Please respect copyright.PENANAGTF983ZNBs
Content-Length: 246513056209Please respect copyright.PENANAfnwe4Eko8Z
Content-Type: application/octet-stream209Please respect copyright.PENANAY7aQ8iFW2B
Content-Disposition: attachment; filename="zen.iso"
(http body is the remain file content)209Please respect copyright.PENANAtlieija1mm
-------------------------------------------
If the etag changes, response 412 like this:209Please respect copyright.PENANAN2i1jZ4zQ8
-----------http response for changed resource------209Please respect copyright.PENANAh0bWEsPnsS
HTTP/1.1 412 Precondition Failed
(http body canbe empty, the client will stop downloading the resumed file)209Please respect copyright.PENANA3wdn7UgLu2
---------------------------------------------------
209Please respect copyright.PENANA9QDFwuvqH8
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---209Please respect copyright.PENANAfGSgADpFJD
GET /zen.iso HTTP/1.1209Please respect copyright.PENANAjJspkCcPwN
Range: bytes=287718144-209Please respect copyright.PENANA5o6r6IBMLS
If-Range: "534231200_1732277116"209Please respect copyright.PENANArqjsfVPzw4
------------------------------------209Please respect copyright.PENANA2FYoq1sfsb
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:209Please respect copyright.PENANAT1an01Bzl8
-------------range response-------------------209Please respect copyright.PENANAEu89t7AAZe
HTTP/1.1 206 Partial Content209Please respect copyright.PENANAcXUwXG5IIF
accept-ranges: bytes209Please respect copyright.PENANAVhmxwBHnZ5
content-range: bytes 287718144-534231199/534231200209Please respect copyright.PENANAZ7XWPOjmLA
ETag: "534231200_1732277116"209Please respect copyright.PENANARckcAiqjMA
Content-Length: 246513056209Please respect copyright.PENANAXrMPysE1n0
Content-Type: application/octet-stream209Please respect copyright.PENANAfWX2r83Ki2
Content-Disposition: attachment; filename="zen.iso"
(http body is the remain file content)209Please respect copyright.PENANAMXaB6kmSMq
-------------------------------------------
If the etag changes, response 200 from beginning like this:209Please respect copyright.PENANADiEsPAsMvk
-----------http response from the beginning------209Please respect copyright.PENANAV8eJwHbUkW
HTTP/1.1 200 OK209Please respect copyright.PENANAbyNMsTIYZP
Date: Mon, 25 Nov 2024 10:56:12 GMT209Please respect copyright.PENANAPJtU86xaGu
accept-ranges: bytes209Please respect copyright.PENANAyTQpteDbP7
ETag: "534231200_1732277116"209Please respect copyright.PENANAcBpeyXYAoU
Content-Length: 534231200209Please respect copyright.PENANArhVKLLIuCE
Content-Type: application/octet-stream209Please respect copyright.PENANATOilr7y0W2
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)209Please respect copyright.PENANAwxp5MqMgZ1
---------------------------------------------------
209Please respect copyright.PENANAso0oZpvsnt
Section X. Thanks
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match209Please respect copyright.PENANAd3uzISlt6b
https://blog.csdn.net/catoop/article/details/134174653209Please respect copyright.PENANA4WKUm6HgRv
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]209Please respect copyright.PENANAVJDNDCO8Sx
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: 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