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?