HTTP:生成 ETag 标头 [英] HTTP: Generating ETag Header

查看:25
本文介绍了HTTP:生成 ETag 标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为资源文件生成 ETag HTTP 标头?

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

推荐答案

etag 是服务器发送给客户端的任意字符串,客户端将在下次请求文件时将其发送回服务器.

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.

etag 应该可以基于文件在服务器上计算.有点像校验和,但您可能不想对发送出去的每个文件进行校验和.

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)

我建立了一个格式为dateamp-file size-file inode number"的字符串.因此,如果一个文件在提供给客户端后在服务器上发生了更改,那么如果客户端重新请求它,新生成的 etag 将不匹配.

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;
}

这篇关于HTTP:生成 ETag 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆