REST note (5): the HTTP header you should know-ETag

Source: Internet
Author: User

In the HTTP1.1 specification, an HTTP header is added: ETag. For Web developers, it is a very important information. It is used for caching

One of the two main headers used (the other is Expires ). In addition, in the REST architecture, it can also be used to control concurrent operations (

To introduce the process for controlling concurrency in AtomPub ). So what is ETag? What other types does it have? What is the difference between strong ETag and weak ETag .? How to calculate

ETag value? What is the difference between it and the Last-Modified header information? This section describes your understanding about these aspects.

Directory:

  1. What is ETag?
  2. Calculate the ETag Value
  3. Types of ETag and their differences
  4. Difference between ETag and Last-Modified header information

What is ETag?

ETag: Abbreviation of Entity Tag. Generally, ETag is not sent to the client in plaintext format. In each lifecycle of a resource

The same value is used to identify the resource status. When a resource changes, if one or more of its header information changes, or the message entity changes

ETag also changes.

The change of the ETag value indicates that the resource status has been modified. The ETag header information can be obtained cheaply through timestamps. If

The consumer is controlled by ETag from the very beginning, so it can ensure that the more fine-grained ETag upgrade is completely controlled by the Service. The service calculates the ETag value,

And return it to the client when the client requests it.

Calculate the ETag Value

In HTTP1.1, the calculation of ETag is not standardized. The ETag value can be anything that uniquely identifies a resource, such as a resource Association in persistent storage.

Version, one or more file attributes, object header information and check value, (CheckSum), you can also calculate the hash value of object information. Sometimes

Calculating an ETag value may be costly. In this case, you can generate a unique value (such as a common GUID ). In any case, the service should be as much as possible

Return the ETag value to the client. The client does not need to care about how the ETag value is generated, as long as the Service sends the ETag value to the service when the resource status changes.

. Set the ETag value in the OutgoingResponse class in MSDN:

In the REST architecture, the ETag value can be passed into the SetETag method through four types of parameters: Guid, integer, Length Integer, and string,

The HTTP Response Header sent from the WCF Service to the client contains the ETag value. In addition, the OutgoingResponse class also has the string attribute: ETag directly

It can also write ETag values in the HTTP response header.

Calculation of ETag using file attributes is as follows:

Public class ETag: IHeader {private string Value; public ETag (string value) {Value = value; WebOperationContext. current. outgoingResponse. ETag} # region IHeader member public void AddHTTPHeader (ResponseContext context) {context. writeHttpHeader (Value) ;}# endregion}
Get ETag:
ETag eTag = new ETag(fileInfo.Name+fileInfo.LastWriteTimeUtc.ToString())

When calculating the ETag value, you need to consider two issues: computing and storage. If an ETag value only requires a small cost and occupies a low storage space

You can calculate the ETag value every time you send it to the client. On the contrary, we need to store the previously computed data.

The ETag value of is sent to the client. Previously, the timestamp is used as a string to obtain the ETag value as a cheap method. For messages that are not constantly changing,

It is a good enough solution. Note: If the timestamp is used as the ETag value, the Last-Modified value is usually not used. Because of the HTTP mechanism

When we verify the resource status through the service, the client does not need to make any changes. Generally, the hash algorithm is used to calculate the maximum ETag overhead.

Obtain the resource expression value. You can only calculate the resource hash value, or include the header information and header information value. If the header information is included, note that

Do not include the header information of the computer ID. Do not include Expires, Cache-Control, and Vary header information. Note: In the hash algorithm

When calculating the ETag value, you must first assemble the resource expression. If the Assembly is time-consuming, you can generate a GUID. Optimized the acquisition of ETag values.

Types of ETag and their differences

ETag has two types: strong ETag (strong ETag) and weak ETag (weak ETag ).

Strong ETag representation: "22FAA065-2664-4197-9C5E-C92EA03D0A16 ".

Weak ETag: w/"22FAA065-2664-4197-9C5E-C92EA03D0A16 ".

The emergence of strong and weak ETag types is related to the ETag Calculation Method on the Apache server. Apache uses FileEtag INode Mtime Siz in FileEtag by default.

E configuration automatically generates ETag (you can also customize it ). Assume that the server resources are frequently modified (for example, modified N times in 1 second ).

If you change Apache configuration to MTime, since MTime can only be accurate to seconds, you can avoid the difference in ETag values of strong ETag within 1 second.

Frequently refresh the Cache (if the resource is frequently Modified in seconds, it can also be solved through Last-Modified ).

Difference between ETag and Last-Modified header information

According to the HTTP standard, Last-Modified can only be accurate to seconds. The emergence of ETag can solve this problem well. In terms of usage, ETag often corresponds

If-None-Match or If-Match, the client sends the HTTP header information (including the ETag value) to the server for processing. ETag is used as follows:

Get/Order/36 Http1.1

If-Match: "22FAA065-2664-4197-9C5E-C92EA03D0A16"

Or If-None-Match: "22FAA065-2664-4197-9C5E-C92EA03D0A16"

Last-Modified is usually used together with If-Modified-Since. The client includes the Last-Modified value in the HTTP header and sends it to the server for processing.

The usage is as follows:

If-Modified-Since: Sat, 24 Dec 2011 11:55:36 GMT

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.