Home » Questions » Computers [ Ask a new question ]

HTTP: Generating ETag Header

HTTP: Generating ETag Header

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

Asked by: Guest | Views: 363
Total answers/comments: 4
Guest [Entry]

"An etag is an arbitrary string that the server sends to the client that the client will send back to the server the next time the file is requested.
The etag should be computable on the server based on the file. Sort of like a checksum, but you might not want to checksum every file sending it out.
server client

<------------- request file foo

file foo etag: ""xyz"" -------->

<------------- request file foo
etag: ""xyz"" (what the server just sent)

(the etag is the same, so the server can send a 304)

I built up a string in the format ""datestamp-file size-file inode number"". So, if a file is changed on the server after it has been served out to the client, the newly regenerated etag won't match if the client re-requests it.
char *mketag(char *s, struct stat *sb)
{
sprintf(s, ""%d-%d-%d"", sb->st_mtime, sb->st_size, sb->st_ino);
return s;
}"
Guest [Entry]

"An etag is an arbitrary string that the server sends to the client that the client will send back to the server the next time the file is requested.
The etag should be computable on the server based on the file. Sort of like a checksum, but you might not want to checksum every file sending it out.
server client

<------------- request file foo

file foo etag: ""xyz"" -------->

<------------- request file foo
etag: ""xyz"" (what the server just sent)

(the etag is the same, so the server can send a 304)

I built up a string in the format ""datestamp-file size-file inode number"". So, if a file is changed on the server after it has been served out to the client, the newly regenerated etag won't match if the client re-requests it.
char *mketag(char *s, struct stat *sb)
{
sprintf(s, ""%d-%d-%d"", sb->st_mtime, sb->st_size, sb->st_ino);
return s;
}"
Guest [Entry]

"From http://developer.yahoo.com/performance/rules.html#etags:

By default, both Apache and IIS embed data in the ETag that dramatically reduces the odds of the validity test succeeding on web sites with multiple servers.
...
If you're not taking advantage of the flexible validation model that ETags provide, it's better to just remove the ETag altogether."
Guest [Entry]

"How to generate the default apache etag in bash

for file in *; do printf ""%x-%x-%x\t$file\n"" `stat -c%i $file` `stat -c%s $file` $((`stat -c%Y $file`*1000000)) ; done

Even when i was looking for something exactly like the etag (the browser asks for a file only if it has changed on the server), it never worked and i ended using a GET trick (adding a timestamp as a get argument to the js files)."