What is, and how it works, a Etag

Asked

Viewed 6,768 times

15

I have been making socket requests to my Apache server in order to create a prototype of Framework, and for each different file that was accessed, returned a Etag in the header.

What good is a Etag? How can I find the Etag file?

  • 1

    Socket or websocket? Etag is usually only for HTTP and not websockets (as far as I understand) ... about your doubt: in a short reply Etag is a hash to compare the change with the server file compacting with the header if-none-match, however an example of how to use Etag for static files and thus improve server performance: http://answall.com/q/44141/3635 - PS: If no one answers, tomorrow I try to take the time to make a very complete response ;)

  • @Guilhermenascimento, I’m using the same normal socket

2 answers

22


Succinctly, Etag is an HTTP mechanism for conditional cache validation.

The idea is to serve content to be curled with an identifier (usually a hash or version number). The client then uses this identifier to ask the server if the content has changed.

Example

The request:

GET /Trj17.png HTTP/1.1
Host: i.stack.imgur.com

Receives the image with the header ETag:

HTTP/1.1 200 OK   
Content-Type: image/png
Content-Length: 61404
ETag: "a5ea8bbcc437de9787e9a87ef6ef690a"

corpo da imagem

The client then caches this image. When the user requests the image again the client can use the identifier to "ask" the server if the cached version is still valid:

GET /Trj17.png HTTP/1.1
Host: i.stack.imgur.com
If-None-Match: "a5ea8bbcc437de9787e9a87ef6ef690a"

If the version is still valid the server returns a response with 304 code, indicating that it is safe for the client to use the cached version:

HTTP/1.1 304 Not Modified
ETag: "a5ea8bbcc437de9787e9a87ef6ef690a"

In case the image has changed the server responds with code 200, the new version of the image and a new Etag.

Thus, we consider ETag a content-based cache validation engine as opposed to headers pair Last-Modified and If-Modified-Since setting up a time-based validation mechanism.

Both validation mechanisms are complementary to headers Cache-Control and Expires which basically tell the customer whether or not to cachet a resource and how long.

5

In etag is basically an identifier for a response to a content negotiation. If the content varies, the Etag varies, not necessarily the address of the requested resource varies.

When you make a request, it can vary the response according to the context. A simple example would be if you make a request to a text file, time with Gzip support and another time without support, the requested file as well as the resource URL are the same, but the etag will vary because the request has varied.

If you want to have a more formal definition, you can refer to the Rfcs that implement the protocols:

https://tools.ietf.org/html/rfc7232#Section-2.3

Note: The example was taken from there (in English).

If you want to work directly with the implementation of the protocols I strongly recommend starting with RFC’s, each webserver implements support for specific versions of the protocols, you can check this in the documentation of the webserver itself. There are always additional settings on the server to enable or not a certain resource, so sniffar a set of requests does not necessarily make you discover the support of a particular webserver, actually, shows you what the current configuration of the particular instance of webserver supports.

Taking into account that the etag of a particular file (which you did not specify the type) may vary, you will not find a etag for a file, you will find a etag for a particular request context to a file.

Unless mistaken, this may vary even depending on the OS where the webserver is installed, even for the same version of Webserver, since the file status is returned to the webserver by the OS which only then returns the changes to the request, see apache documentation and search for etag.

https://httpd.apache.org/docs/2.4/pt-br/

Since I don’t know the purpose of your framework, I can’t suggest anything specific.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.