Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP: Generating ETag Header

How do I generate an ETag HTTP header for a resource file?

like image 830
tags2k Avatar asked Aug 07 '08 08:08

tags2k


People also ask

How do I get ETag headers?

First, you retrieve the current entity data by using a GET request that includes the If-Match request header. The ETag information is returned along with the entity content. Then, you send a PUT update request that includes the If-Match request header with the ETag information from the previous GET request.

What is ETag in HTTP response header?

An ETag (entity tag) is an HTTP header that is used to validate that the client (such as a mobile device) has the most recent version of a record. When a GET request is made, the ETag is returned as a response header. The ETag also allows the client to make conditional requests.

How is ETag generated?

ETag generation The method by which ETags are generated has never been specified in the HTTP specification. Common methods of ETag generation include using a collision-resistant hash function of the resource's content, a hash of the last modification timestamp, or even just a revision number.

What is ETag in cache?

The ETag (or entity tag) HTTP response header is an identifier for a specific version of a resource. It lets caches be more efficient and save bandwidth, as a web server does not need to resend a full response if the content was not changed.


1 Answers

As long as it changes whenever the resource representation changes, how you produce it is completely up to you.

You should try to produce it in a way that additionally:

  1. doesn't require you to re-compute it on each conditional GET, and
  2. doesn't change if the resource content hasn't changed

Using hashes of content can cause you to fail at #1 if you don't store the computed hashes along with the files.

Using inode numbers can cause you to fail at #2 if you rearrange your filesystem or you serve content from multiple servers.

One mechanism that can work is to use something entirely content dependent such as a SHA-1 hash or a version string, computed and stored once whenever your resource content changes.

like image 134
Justin Sheehy Avatar answered Sep 19 '22 19:09

Justin Sheehy