HTTP ETag和HTTP重定向 [英] HTTP ETags and HTTP Redirects

查看:81
本文介绍了HTTP ETag和HTTP重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络服务器,该服务器在响应中发出ETag标头,并从客户端检查If-None-Match标头(如果存在).在这种情况下,客户端不是Web浏览器,而是go的内置net/http http.Client类型的扩展.

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.

这是我的代码

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.

我仅存储和发送GET请求的ETag.虽然可以将它们发送给其他请求是有效的,但在我的用例中,目前还不可用,因此我不会为之烦恼.通过将url.URL对象映射到字符串来存储ETag.

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.

我的问题是这个.我请求" http://foo.com/bar.html ".服务器使用302 FoundLocation标头将我重定向到" http://foo.com/qux.html ".然后,我请求" http://foo.com/qux.html "并获得一个200 OK带有ETag标头.

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.

我将上次响应中的ETag标头与哪个URL相关联?

With what URL do I associate the ETag header from the last response?

302 Found本身可以包含ETag标头吗?

Could the 302 Found itself include an ETag header?

推荐答案

一个 ETag 与当前请求的选定的表示形式"相关联. 302找到响应的选定表示形式通常包含简短的超文本请注意,并带有指向不同URI的超链接."因此,如果302响应包含ETag,则ETag与该超文本相关联.

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.

但是,如果在对使用重定向进行响应的资源的请求中包含If-None-Match(或其他前提条件),则服务器将忽略该前提条件.根据 RFC 7232的第5部分(我强调):

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):

如果服务器对没有这些条件的相同请求的响应将是2xx(成功)或412(失败的前提条件)以外的状态码,则必须忽略所有接收到的前提条件.换句话说,在条件请求中,重定向和失败优先于前提条件的评估.

因此,尽管302响应可以包含ETag,但它没有用,因为服务器可能不会在对该资源的进一步请求中使用它.

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.

这篇关于HTTP ETag和HTTP重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆