问题
I have a webserver that issues the ETag
header on responses and checks the If-None-Match
header from the client, if present. The client in this case, is not a web browser but an extension of go's builtin net/http
http.Client
type.
Here is my code
package util
import "net/http"
import "net/url"
type HttpClient struct {
http.Client
etags map[url.URL]string
}
func (hc *HttpClient) Do(req *http.Request) (*http.Response, error) {
const ETAG_SERVER_HEADER = "ETag"
const ETAG_CLIENT_HEADER = "If-None-Match"
//Do not attempt to use ETags on non-GET requests
if req.Method != "GET" {
return hc.Client.Do(req)
}
//Check for an existing etag
etag, ok := hc.etags[*req.URL]
if ok { //If an ETag is present, send it along
if req.Header == nil {
req.Header = http.Header{}
}
req.Header.Add(ETAG_CLIENT_HEADER, etag)
}
//Do the response
response, err := hc.Client.Do(req)
//If the response is ok
if err == nil {
if hc.etags == nil {
hc.etags = make(map[url.URL]string)
}
//Check for an ETAG from the server, store it if present
etag = response.Header.Get(ETAG_SERVER_HEADER)
if len(etag) != 0 {
hc.etags[*req.URL] = etag
}
}
return response, err
}
It is working without issue as of present.
I am only storing and sending the ETag for GET
requests. While it is valid to send them for other requests, it is not in my use case as of current so I'm not bothering with it. The ETags are stored by mapping the url.URL
object to a string.
My question is this. I request "http://foo.com/bar.html". The server redirects me using 302 Found
and the Location
header to "http://foo.com/qux.html". I then request "http://foo.com/qux.html" and get a 200 OK
along with an ETag
header.
With what URL do I associate the ETag
header from the last response?
Could the 302 Found
itself include an ETag
header?
回答1:
An ETag is associated with the "selected representation" of the current request. The selected representation for a 302 Found response "usually contains a short hypertext note with a hyperlink to the different URI(s)." Therefore, if a 302 response contains an ETag, the ETag is associated with that hypertext.
However, if you include an If-None-Match (or other precondition) in a request to a resource that responds with a redirect, the server will ignore the precondition. According to Section 5 of RFC 7232 (my emphasis):
A server MUST ignore all received preconditions if its response to the same request without those conditions would have been a status code other than a 2xx (Successful) or 412 (Precondition Failed). In other words, redirects and failures take precedence over the evaluation of preconditions in conditional requests.
Thus, while a 302 response can contain an ETag, it is not useful because the server may not use it in further requests to that resource.
回答2:
As you can see in /src/pkg/net/http/client.go on line 266 the request-response cycle will continue until either or:
- The
http.Client
's redirect checker (CheckRedirect
field) returns an error - The response received is not a redirect (according to shouldRedirectGet)
During the cycle , previous responses will be discarded and the headers the response that client.Do
returns are those of the last response.
This means that you should associate the ETag
with qux.html
in your example.
Could the 302 Found itself include an ETag header?
Yes it could but since it's a redirect, the client will follow it and acquire a new set of response headers from the target URL.
来源:https://stackoverflow.com/questions/23014106/http-etags-and-http-redirects