目录搜索
GuidesAccess control CORSAuthenticationBrowser detection using the user agentCachingCaching FAQCompressionConditional requestsConnection management in HTTP 1.xContent negotiationContent negotiation: List of default Accept valuesCookiesCSPMessagesOverviewProtocol upgrade mechanismProxy servers and tunnelingProxy servers and tunneling: Proxy Auto-Configuration (PAC) filePublic Key PinningRange requestsRedirectionsResources and specificationsResources and URIsResponse codesServer-Side Access ControlSessionGuides: BasicsBasics of HTTPChoosing between www and non-www URLsData URIsEvolution of HTTPIdentifying resources on the WebMIME TypesMIME types: Complete list of MIME typesCSPContent-Security-PolicyContent-Security-Policy-Report-OnlyCSP: base-uriCSP: block-all-mixed-contentCSP: child-srcCSP: connect-srcCSP: default-srcCSP: font-srcCSP: form-actionCSP: frame-ancestorsCSP: frame-srcCSP: img-srcCSP: manifest-srcCSP: media-srcCSP: object-srcCSP: plugin-typesCSP: referrerCSP: report-uriCSP: require-sri-forCSP: sandboxCSP: script-srcCSP: style-srcCSP: upgrade-insecure-requestsCSP: worker-srcHeadersAcceptAccept-CharsetAccept-EncodingAccept-LanguageAccept-RangesAccess-Control-Allow-CredentialsAccess-Control-Allow-HeadersAccess-Control-Allow-MethodsAccess-Control-Allow-OriginAccess-Control-Expose-HeadersAccess-Control-Max-AgeAccess-Control-Request-HeadersAccess-Control-Request-MethodAgeAllowAuthorizationCache-ControlConnectionContent-DispositionContent-EncodingContent-LanguageContent-LengthContent-LocationContent-RangeContent-TypeCookieCookie2DateDNTETagExpectExpiresForwardedFromHeadersHostIf-MatchIf-Modified-SinceIf-None-MatchIf-RangeIf-Unmodified-SinceKeep-AliveLarge-AllocationLast-ModifiedLocationOriginPragmaProxy-AuthenticateProxy-AuthorizationPublic-Key-PinsPublic-Key-Pins-Report-OnlyRangeRefererReferrer-PolicyRetry-AfterServerSet-CookieSet-Cookie2SourceMapStrict-Transport-SecurityTETkTrailerTransfer-EncodingUpgrade-Insecure-RequestsUser-AgentUser-Agent: FirefoxVaryViaWarningWWW-AuthenticateX-Content-Type-OptionsX-DNS-Prefetch-ControlX-Forwarded-ForX-Forwarded-HostX-Forwarded-ProtoX-Frame-OptionsX-XSS-ProtectionMethodsCONNECTDELETEGETHEADMethodsOPTIONSPATCHPOSTPUTStatus100 Continue101 Switching Protocols200 OK201 Created202 Accepted203 Non-Authoritative Information204 No Content205 Reset Content206 Partial Content300 Multiple Choices301 Moved Permanently302 Found303 See Other304 Not Modified307 Temporary Redirect308 Permanent Redirect400 Bad Request401 Unauthorized403 Forbidden404 Not Found405 Method Not Allowed406 Not Acceptable407 Proxy Authentication Required408 Request Timeout409 Conflict410 Gone411 Length Required412 Precondition Failed413 Payload Too Large414 URI Too Long415 Unsupported Media Type416 Range Not Satisfiable417 Expectation Failed426 Upgrade Required428 Precondition Required429 Too Many Requests431 Request Header Fields Too Large451 Unavailable For Legal Reasons500 Internal Server Error501 Not Implemented502 Bad Gateway503 Service Unavailable504 Gateway Timeout505 HTTP Version Not Supported511 Network Authentication RequiredStatus
文字

所述ETag HTTP 响应报头为资源的特定版本的标识符。它允许缓存更高效,并节省带宽,因为如果内容没有改变,Web 服务器不需要发送完整的响应。另一方面,如果内容发生了变化,etags 有助于防止资源的同时更新互相覆盖(“空中冲突”)。

如果给定 URL 处的资源发生更改,则Etag必须生成新值。因此,Etags 与指纹相似,也可能用于某些服务器的跟踪目的。它们的比较可以快速确定资源的两个表示是否相同,但它们也可能被设置为无限期地由跟踪服务器持续存在。

Header type

Response header

Forbidden header name

no

语法

ETag: W/"<etag_value>"ETag: "<etag_value>"

指令

W/可选'W/'(区分大小写)表示使用弱验证程序。弱验证器很容易生成,但对于比较来说却不太有用。强大的验证器是进行比较的理想选择,但可能非常难以高效生成。

Etag相同资源的两个表示的弱值可能在语义上是等价的,但不是字节一致的。“<etag_value>”实体标记唯一地表示请求的资源。它们是放在双引号(像是"675af34563dc-tr34")之间的 ASCII 字符串。

ETag没有指定生成值的方法。通常,使用内容的散列,最后修改时间戳的散列或仅修订版本号。例如,MDN 使用 wiki 内容的十六进制数字的散列。

例子

ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"ETag: W/"0815"

避免空中碰撞

借助标题ETagIf-Match头文件,您可以检测到空中编辑冲突。

例如,在编辑 MDN 时,当前的 wiki 内容被散列并放入Etag响应中:

ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"

当保存对 wiki 页面的更改(发布数据)时,POST请求将包含包含要检查新鲜度If-MatchETag值的标题。

If-Match: "33a64df551425fcc55e4d42a148795d9f25f89d4"

如果散列不匹配,则意味着文档已被编辑,并且412 Precondition Failed引发错误。

缓存不变的资源

ETag标题的另一个典型用例是缓存未更改的资源。如果用户再次访问一个给定的 URL(它有一个ETag集合),并且它已经过时了,那么这个 URL 太旧而不能被视为可用,客户端会将它的值发送到ETag一个If-None-Match标题字段中:

If-None-Match: "33a64df551425fcc55e4d42a148795d9f25f89d4"

服务器比较客户端ETag(与其If-None-Match一起发送)ETag与其当前版本的资源,并且如果两个值匹配(即资源没有改变),则服务器发回304 Not Modified状态,没有任何正文,告诉客户端缓存版本的响应仍然很好用(新鲜)。

规范

Specification

Title

RFC 7232, section 2.3: ETag

Hypertext Transfer Protocol (HTTP/1.1): Conditional Requests

浏览器兼容性

Feature

Chrome

Firefox

Edge

Internet Explorer

Opera

Safari

Basic Support

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

Feature

Android

Chrome for Android

Edge mobile

Firefox for Android

IE mobile

Opera Android

iOS Safari

Basic Support

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

上一篇:下一篇: