接上一篇讨论实现静态资源访问的问题,除了可部分获取资源,还有条件参数,服务器端判断当前资源状况满足这些条件才处理请求。
这些条件有If-Match
,If-Modified-Since
,If-Range
,If-Unmodified-Since
,If-None-Match
等。最常见的If-None-Match
这个header,用来在获取资源时比较本地的etag,如果服务器端不一致才获取。
先获取一个资源的etag:
$ curl -I http://localhost:8080/chain.jpg
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"4932-1448300760000"
Last-Modified: Tue, 24 Nov 2015 01:46:00 CST
Content-Type: image/jpeg
Content-Length: 4932
Date: Mon, 23 Nov 2015 18:12:49 GMT
然后在请求header里增加If-None-Match
内容:
$ curl -v -H 'If-None-Match: W/"4932-1448300760000"' http://localhost:8080/chain.jpg
* Hostname was NOT found in DNS cache
* Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> GET /_files_/img/chain.jpg HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:8080
> Accept: */*
> If-None-Match: W/"4932-1448300760000"
>
< HTTP/1.1 304 Not Modified
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< ETag: W/"4932-1448300760000"
< Date: Mon, 23 Nov 2015 18:13:01 GMT
<
* Connection #0 to host localhost left intact
etag相等的话,条件不符合返回304。