3.3. If-Modified-Since

2 阅读3分钟

3.3. If-Modified-Since

rfc 7232 3.3. If-Modified-Since

recipient忽略If-Modified-Since的情况

  • 请求包含了If-None-Match时;但是当intermediaries没有实现If-None-Match时,If-Modified-Since依然有效。
  • 请求包含的If-Modified-Since的值不是一个有效的HTTP-date时
  • 请求方法不是GET/HEAD时

If-Modified-Since的时间参照

A recipient MUST interpret an If-Modified-Since field-value's timestamp in terms of the origin server's clock.

If-Modified-Since的值是指origin server上的一个时刻。另外Last-Modified的值也是指origin server上的一个时刻。

使用场景

1.更新cache

本地版本v1+Last-Modified:t1,使用If-Modified-Since:t1去服务器验证v1是否过期。

侧重点在于:用户向验证v1是否更新。

2.获取指定时间窗口的更新

to limit the scope of a web traversal to resources that have recently changed
  • 用户从服务器获取t1之前的数据
  • 用户手动指定时间窗口(t1之后的时间),使用If-Modified-Since:t1向服务器查询:从t1开始数据是否有变化。
  • 如果有变化,服务器返回从t1开始的更新
  • 如果无变化,服务器返回304

侧重点在于:用户不关心t1之前数据是否变化,用户只关心指定时间窗口(从t1开始的时间)是否有更新。

注意:使用“精确匹配”的服务器无法处理这种场景

当用于更新cache时的取值

取值

格式

If-Modified-Since = HTTP-date

An example of the field is:
If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT
1.使用Last-Modified field来生成

当本地时间同步不是很好或者在服务器在evaluates阶段使用"精确匹配"时,使用这种取值。

如果不使用精确匹配

  • 假设当前时间10:00:00,但是服务器时间快了5分钟,当前显示10:05:00
  • 管理员修改abc.css为v1,Last-Modified为10:05:00
  • 用户访问abc.css,服务器将v1返回给用户,其中Last-Modified为10:05:00
  • 管理员发现时间不对,校准时间为10:00:00
  • 管理员在10:01:00的时候修改abc.css为v2,Last-Modified为10:01:00
  • 用户使用If-Modified-Since:10:05:00访问服务器
  • 由于10:01:00 < 10:05:00,所以服务器返回304,这导致了用户看不到最新的v2版本

当使用精确匹配

“精确匹配” 是指:If-Modified-Since的值与abc.css的Last-Modified指向同一时刻.

  • 10:01:00 不等于 10:05:00,服务器执行(SHOULD)继续执行GET方法,返回v2版本,code 200。于是用户看到了最新的v2版本。
2.使用基于其他时间来生成

当cache message中没有包含Last-Modified field时,使用这种取值。例如:cache message 的Data Header/收到message的 local clock time

当用于获取指定时间窗口的更新时

  • 使用cache message中的Date Header(报文生成时间)
  • 使用Local Clock

evaluates与method执行

参考5. Evaluation,但是这里有区别,If-Modified-Since这里并没有使用MUST/MUST NOT:

 An origin server that receives an If-Modified-Since header field
   SHOULD evaluate the condition prior to performing the method
   (Section 5).  The origin server SHOULD NOT perform the requested
   method if the selected representation's last modification date is
   earlier than or equal to the date provided in the field-value;
   
  • evaluate 应该(SHOULD) 在method之前执行;注意,这里不是MUST
  • last modification >= If-Modified-Since时,origin server 不应该(SHOULD NOT) 执行method;注意,这里不是MUST NOT

验证不通过时应该(SHOULD)返回304

instead the origin server SHOULD generate a 304 (Not Modified)
   response, including only those metadata that are useful for
   identifying or updating a previously cached response.

last modification >= If-Modified-Since时,origin server应该(SHOULD)返回304;当返回304时,header以及body的限制参考 4.1. 304 Not Modified

对于cache的要求

Requirements on cache handling of a received If-Modified-Since header
   field are defined in Section 4.3.2 of [RFC7234].