HTTP Etag details

Source: Internet
Author: User
Tags http etag

The HTTP specification defines ETag as the object Value of the requested variable ".

In other words, ETag is a token that can be associated with Web resources ). A typical Web resource can be a Web page, but it may also be a JSON or XML document. The server is solely responsible for determining what the mark is and its meaning, and transmitting it to the client in the HTTP Response Header. The following is the format returned by the server:

ETag: "50b1c1d4f775c61: df3"

The query update format of the client is as follows:

If-None-Match: W/"50b1c1d4f775c61: df3"

If ETag does not change, status 304 is returned and no result is returned, which is the same as Last-Modified.

The Etag test is mainly useful for resumable download.

How does Etag-Last-Modified and Etags help improve performance?

Smart developers will use the http header of the Last-Modified and ETags requests together, so that the cache of the client (such as the browser) can be used. Because the server first generates the Last-Modified/Etag mark, the server can use it later to determine whether the page has been Modified. Essentially, the client sends this mark back to the server and requires the server to verify its (client) cache.
The process is as follows:
1. The client requests A page ().
2. The server returns to page A and adds A Last-Modified/ETag to page.
3. The client displays the page and caches the page together with Last-Modified/ETag.
4. The customer requests page A again and passes the Last-Modified/ETag returned by the server in the Last request to the server.
5. The server checks the Last-Modified or ETag and determines that the page has not been Modified since the Last client request. The server returns the response 304 and an empty response body.

Etag-Function

Etag is mainly used to solve some problems that Last-Modified cannot solve.

1. Some files may be changed cyclically, but their content does not change (only the modification time). At this time, we do not want the client to think that the file has been modified, and GET again;

2. Some files are frequently modified, for example, modified within seconds (for example, modified N times within 1 s ), if-Modified-Since can check that the granularity is s-level, which cannot be determined (or the UNIX record MTIME can only be accurate to seconds)

3. Some servers cannot accurately obtain the last file modification time;

Therefore, Etag (Entity Tags) is introduced in HTTP/1.1 ). etag is only a file-related identifier. It can be a version identifier, such as v1.0.0 or "2e681a-6-5d044840. However, the HTTP/1.1 Standard does not specify what the Etag content is or how to implement it. The only rule is that the Etag needs to be placed in.

Etag-Working Principle

The Etag is generated by the server. The client checks whether the request modifies the resource through the condition of If-Match or If-None-Match. It is common to use If-None-Match. The process for requesting a file may be as follows:

==== First request ====
1. The client initiates an http get request for a file;
2. The server processes the request, returns the file content and a bunch of headers, including Etag (for example, "2e681a-6-5d044840") (assuming that the server supports Etag generation and Etag has been enabled). Status Code 200

=== Second request ===
1. The client initiates an http get request for a file. Note that the client sends an If-None-Match header at the same time. The content of this header is the Etag: 2e681a-6-5d044840 returned by the server during the first request.
2. The server determines that the sent Etag matches the calculated Etag. Therefore, If-None-Match is False, 200 is not returned, and 304 is returned. The client continues to use the local cache;

The process is very simple. The problem is, what if the server has set Cache-Control: max-age and Expires?
The answer is to use it at the same time. That is to say, If-Modified-Since and If-None-Match are completely matched. That is, after checking the modification time and Etag, the server can return 304. (do not fall into the strange circle of who is using it)

Etag-Etag implementation in Apache

1. Apache first checks whether Etag is weak. If not, go to the second scenario:

Strong Etag sets the Etag value according to the configuration in the configuration file. The default FileEtag of Apache is set:

FileEtag INode Mtime Size

That is, the Etag value is generated based on these three attributes, which are implemented through some algorithms and output to the hex format. Adjacent attributes are separated by-, for example:

Etag "2e681a-6-5d044840"

The three sections here represent the Hex format of INode, MTime, and Size values calculated based on the algorithm. (If the characters (that is, 0-f) in non-Hex are displayed here ), you may have seen it :))

Of course, you can change the FileEtag settings of Apache, for example, to FileEtagSize. The resulting Etag may be:

Etag "6"

In short, if several segments are set, the Etag value has several segments. (Do not mistakenly assume that Etag is a fixed 3-segment)

Description
All of this is the etag implementation in apache2.2, because HTTP/1.1 does not specify the implementation or format of etag. Therefore, you can also modify or completely write your own algorithms to get etag, for example, "2e681a65d044840". The client will remember and cache the etag (where is the etag stored in windows, this value is directly used for the next access to compare with the etag generated by the server.

Note:
No matter what the algorithm is, the server needs to perform computation, resulting in overhead and performance loss. For this reason, many websites have completely disabled etag (such as Yahoo !), This does not actually comply with HTTP/1.1, because HTTP/1.1 always encourages the server to enable etag as much as possible.

Etag-weak verification (weak Etag)

Reconsider the three questions mentioned above:

Problem 1: some files may be changed cyclically, but their content does not change (only the modification time). At this time, we do not want the client to think that the file has been modified, and get again;

Solution: If you use strong etag, you must re-get the page each time. If you use etag, for example, set it to file etag size, you can ignore the last-modified time modification caused by mtime, thus affecting the IF-modified-since (IMS) verification. This is irrelevant to weak etag.

Problem 2: some files are modified frequently, for example, within seconds (for example, n times in 1 s ), if-modified-since can check that the granularity is s-level, which cannot be determined (or the Unix record mtime can only be accurate to seconds)

Solution: in this case, Apache automatically determines the difference between the request time and the modification time,If the file size is less than 1 s, Apache considers that the file may be modified again within this 1 second.Therefore, a weak Etag (WeakEtag) is generated based on MTime. Therefore, the MTime can only be accurate to s, so the Etag generated within 1 s is always the same, this avoids frequent Cache refresh in 1 s due to the use of strong Etag. (It seems that Etag is not needed, and only Last-Modified can be used to solve the problem. However, this only applies to situations where the modification is extremely frequent, and many files may also use strong Etag verification ). Weak Etag starts with W/, for example, W/"2e681a"

Problem 3: Some servers cannot accurately obtain the last modification time of files;

Solution: generate Etag because Etag can combine Inode, MTime, and Size to avoid this problem.

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.