如何修改“ 304未修改”确切地工作? [英] How does "304 Not Modified" work exactly?

查看:97
本文介绍了如何修改“ 304未修改”确切地工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 如何生成 304 Not Modified响应?

  • How are "304 Not Modified" responses generated?

浏览器如何确定响应是否为HTTP请求是304?

How does a browser determine whether the response to an HTTP request is 304?

是浏览器设置的还是服务器发送的?

Is it set by the browser or sent from the server?

如果由服务器发送,服务器如何知道高速缓存中的可用数据,以及如何将304设置为图像?

If sent by the server, how does the server know the data available in cache, also how does it set 304 to an image?

我的猜测,如果它是由浏览器生成的:

function is_modified()
{
    return get_data_from_cache() === get_data_from_url();
}

function get_data_from_cache()
{
    return some_hash_or_xxx_function(cache_data);
}

function get_data_from_url()
{
     return some_hash_or_xxx_function(new_data);
}

function some_hash_or_xxx_function(data)
{
     // Do something with the data.
     // What is that algorithm?
     return result;
}

console.log(is_modified());

我依靠第三方API提供程序来获取数据,进行解析和处理。将其推送到我的数据库。每次请求期间数据可能会更改,也可能不会更改,但是标头始终发送 200 。我不想解析,检查数据库中的最后一个唯一ID等,以便确定数据的变化,也不想直接比较结果,而是我 md5() sha1() crc32()对结果进行哈希处理并可以正常工作,但是我想知道该算法确定 304

I am relying on a third party API provider to get data, parse & push it to my database. The data may or may not change during every request, but the header always sends 200. I do not want to parse, check the last Unique ID in DB and so on... to determine the change in data, nor compare the result directly rather I md5(), sha1() and crc32() hashed the result and works fine, but I'm wondering about the algorithm to determine 304.

我想使用相同的算法来确定数据中的变化。

I want to use the same kind of algorithm to determine the change in my data.

推荐答案

当浏览器将某些内容放入其缓存时,它还会存储 Last-Modified ETag 标头。

When the browser puts something in its cache, it also stores the Last-Modified or ETag header from the server.

然后浏览器发送带有 If-Modified-Since 或 If-None-Match 标头,告诉服务器如果内容仍具有该日期或ETag,则发送304

The browser then sends a request with the If-Modified-Since or If-None-Match header, telling the server to send a 304 if the content still has that date or ETag.

服务器需要某种方式为每种资源的每个版本计算修改日期或ETag;通常来自文件系统或单独的数据库列。

The server needs some way of calculating a date-modified or ETag for each version of each resource; this typically comes from the filesystem or a separate database column.

这篇关于如何修改“ 304未修改”确切地工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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