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

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

问题描述

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

解决方案

etag是服务器发送给客户端,下一次请求文件时,客户端将发送回服务器。

etag应该可以在服务器上根据文件进行计算。

 服务器客户端
$有点像校验和,但是您可能不希望校验每个文件。 b $ b< -------------请求文件foo

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

< -------------请求文件foo
etag:xyz(服务器刚发送的)

(etag是一样的,所以服务器可以发送一个304)

我建立了一个字符串格式文件索引节点号/日期戳/文件大小。因此,如果服务器上的文件在服务器上发送给客户端后发生更改,那么如果客户端重新请求它,则新重新生成的etag将不匹配。

 char * mketag(char * s,struct stat * sb)
{
sprintf(s,%d /%d /%d,sb-> st_ino, sb-> st_mtime,sb-> st_size);
return s;
}


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

解决方案

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 "file inode number/datestamp/file size". 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_ino, sb->st_mtime, sb->st_size);
    return s;
}

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

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