HTTP Request headers and caching

2019-06-25 05:33发布

问题:

How should an HTTP Agent make decisions about using cached response when a request has the same path but different headers?

Take for example this HTTP request/response:

GET /resource HTTP/1.1
Host: example.org
X-Filter: foo=bar

HTTP/1.1 200 OK
Cache-Control: max-age=3600
Content-Type: application/json
Content-Length: 13

{"foo":"bar"}

Should the agent consider the response valid for a second request with a different X-Filter header? For example:

GET /resource HTTP/1.1
Host: example.org
X-Filter: foo=baz

then within an hour from the first request, should the agent request a fresh response since the request header differs, or should use the cached response from the first request, ignoring the header?

I'm asking this because I noticed that Google Chrome makes a new request, Microsoft Edge instead use the cached response.

回答1:

You should use the cached version unless changed header appears in the list provided by the (optional) Vary response header.

For example, a response that contains

 Vary: accept-encoding, accept-language

indicates that the origin server might have used the request's
Accept-Encoding and Accept-Language fields (or lack thereof) as
determining factors while choosing the content for this response.