本文描述了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.
35Please respect copyright.PENANAndWNCijLUM
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:35Please respect copyright.PENANATuKF5sc3My
---------------http response with etag----------------35Please respect copyright.PENANAb7vZzTccX2
HTTP/1.1 200 OK35Please respect copyright.PENANA3CuiCAEVcS
Date: Mon, 25 Nov 2024 10:56:12 GMT35Please respect copyright.PENANAG6Ble6XghJ
accept-ranges: bytes35Please respect copyright.PENANA5zVj1JSm8g
ETag: "534231200_1732277116"35Please respect copyright.PENANAsz9lxgmJ8G
Content-Length: 53423120035Please respect copyright.PENANAr1yCcw0xxq
Content-Type: application/octet-stream35Please respect copyright.PENANASA0RQPAcOU
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)35Please respect copyright.PENANAHs6haRV4Dr
-----------------------------------------------------
35Please respect copyright.PENANAWtDxdoaVSp
Section B. if-none-match
The browser send back the etag to ask server if it has been changed by header like this:35Please respect copyright.PENANAu0CTbRjeNH
-----http request for a cached resource------35Please respect copyright.PENANAfp24a709zW
GET /zen.iso HTTP/1.135Please respect copyright.PENANAHIfPbS6GpU
if-none-match: "534231200_1732277116"35Please respect copyright.PENANA4nZnsPk5Y8
---------------------------------------------
If the etag is unchanged, the server response like this:
1. for get and head requests:35Please respect copyright.PENANAxpnVpI8Ydv
-----------http response for unchanged resource------35Please respect copyright.PENANA5e6iXXZgh8
HTTP/1.1 304 Not Modified
(http body canbe empty, the client will used its cache)35Please respect copyright.PENANABic6QD0KRB
----------------------------------------------------
2. for other methods requests:35Please respect copyright.PENANAhTuxiRLCYB
-----------http response for unchanged resource------35Please respect copyright.PENANAq2zbN6qUZY
HTTP/1.1 412 Precondition Failed
(http body canbe empty, the client will used its cache)35Please respect copyright.PENANA1DNTK8tUiV
----------------------------------------------------
35Please respect copyright.PENANAu1qAUSfhcq
If the etag changes, the server response 200 OK with the new file content like this:35Please respect copyright.PENANAqr14aIIew6
---------------http response with etag----------------35Please respect copyright.PENANAt5M16Mrfpc
HTTP/1.1 200 OK35Please respect copyright.PENANANyjBly1C8E
Date: Mon, 25 Nov 2024 10:56:12 GMT35Please respect copyright.PENANA0441Zu1Tc2
accept-ranges: bytes35Please respect copyright.PENANAmYtMywpIo5
ETag: "534231200_1732277116"35Please respect copyright.PENANAZSuYYmhWQW
Content-Length: 53423120035Please respect copyright.PENANAx1TxKIx9kg
Content-Type: application/octet-stream35Please respect copyright.PENANAAGydmzFgLb
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)35Please respect copyright.PENANAjF0h4l9ns2
-----------------------------------------------------
35Please respect copyright.PENANAacK2ew39YC
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:35Please respect copyright.PENANApHeOpojFL5
------resume download request-------35Please respect copyright.PENANAuHsvXmvgAl
GET /zen.iso HTTP/1.135Please respect copyright.PENANA6wkg4rFsBc
Range: bytes=287718144-35Please respect copyright.PENANA4ZpjDxALpP
If-Match: "534231200_1732277116"35Please respect copyright.PENANAWYG0ERa67Y
-----------------------------------35Please respect copyright.PENANAzs1Yhi9oFo
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:35Please respect copyright.PENANANdsOT81dly
-------------range response-------------------35Please respect copyright.PENANAeLXHrkoFlZ
HTTP/1.1 206 Partial Content35Please respect copyright.PENANAYLXUW4Al8w
accept-ranges: bytes35Please respect copyright.PENANAKDCVDQ0lOR
content-range: bytes 287718144-534231199/53423120035Please respect copyright.PENANANnVEIWlMCo
ETag: "534231200_1732277116"35Please respect copyright.PENANAMPg0g2GsaV
Content-Length: 24651305635Please respect copyright.PENANAhAd5Cpnk4f
Content-Type: application/octet-stream35Please respect copyright.PENANANCzXxGObHJ
Content-Disposition: attachment; filename="zen.iso"
(http body is the remain file content)35Please respect copyright.PENANAztma7bL2uk
-------------------------------------------
If the etag changes, response 412 like this:35Please respect copyright.PENANA0UAlA0yc23
-----------http response for changed resource------35Please respect copyright.PENANAjSr6C1mDz7
HTTP/1.1 412 Precondition Failed
(http body canbe empty, the client will stop downloading the resumed file)35Please respect copyright.PENANAYrV2LxFcp5
---------------------------------------------------
35Please respect copyright.PENANAjd7wvwDKOQ
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---35Please respect copyright.PENANAzLDALNiGJk
GET /zen.iso HTTP/1.135Please respect copyright.PENANAlV3F7XQyeu
Range: bytes=287718144-35Please respect copyright.PENANAUjRst2p0Di
If-Range: "534231200_1732277116"35Please respect copyright.PENANAi0nokQtDWX
------------------------------------35Please respect copyright.PENANAPNDCYzSSE0
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:35Please respect copyright.PENANAfOvNwkbC7J
-------------range response-------------------35Please respect copyright.PENANABxt6JJU5he
HTTP/1.1 206 Partial Content35Please respect copyright.PENANAfjBlGhES40
accept-ranges: bytes35Please respect copyright.PENANA2cAD4wdtcV
content-range: bytes 287718144-534231199/53423120035Please respect copyright.PENANA1qFTjiBjBb
ETag: "534231200_1732277116"35Please respect copyright.PENANAO7clJHpYC6
Content-Length: 24651305635Please respect copyright.PENANANytDzdrVT5
Content-Type: application/octet-stream35Please respect copyright.PENANAUUNkRBU1Z9
Content-Disposition: attachment; filename="zen.iso"
(http body is the remain file content)35Please respect copyright.PENANAwhba6PkZay
-------------------------------------------
If the etag changes, response 200 from beginning like this:35Please respect copyright.PENANAkLAuhff7Uq
-----------http response from the beginning------35Please respect copyright.PENANADfYXpztnL8
HTTP/1.1 200 OK35Please respect copyright.PENANAGLbFiTwTCE
Date: Mon, 25 Nov 2024 10:56:12 GMT35Please respect copyright.PENANAPCWdAs7xcq
accept-ranges: bytes35Please respect copyright.PENANAUe8ZuMzA4z
ETag: "534231200_1732277116"35Please respect copyright.PENANA2Naey5E78B
Content-Length: 53423120035Please respect copyright.PENANAoZ1R7fsy3C
Content-Type: application/octet-stream35Please respect copyright.PENANAEm7blLocbv
Content-Disposition: attachment; filename="zen.iso"
(http body for the resource)35Please respect copyright.PENANAp8H0w6l7L7
---------------------------------------------------
35Please respect copyright.PENANA9PUaHJfFkK
Section X. Thanks
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match35Please respect copyright.PENANAHQtI98JFqd
https://blog.csdn.net/catoop/article/details/13417465335Please respect copyright.PENANAW8HM1wF8Wr
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: si.jinmin@gmail.com35Please respect copyright.PENANAJrX2EIflys
如果您发现本文有任何错误,或者对本文有好的建议,欢迎与我联系探讨,我的微信: si_jinmin, 我的email: si.jinmin@gmail.com
如果您對C/C++ Programming, Linux, HTTP Protocol, Website Development, Vue, Git, VsCode感興趣,邀請您加入「Linux/C/C++ Website Development」微信群,請加我的微信(si_jinmin)以便拉您进群。
ns 15.158.61.6da2