From a27c2c6017afc9022b95cb24d376b156d0b40843 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Fri, 15 Dec 2023 13:31:40 +0900 Subject: [PATCH] For SNI compatibility, also compare req.Host --- rfc9111/shared.go | 3 ++- testutil/cacher.go | 2 +- testutil/rcutil.go | 3 +++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/rfc9111/shared.go b/rfc9111/shared.go index 9217f34..8be9dfe 100644 --- a/rfc9111/shared.go +++ b/rfc9111/shared.go @@ -178,7 +178,8 @@ func (s *Shared) Handle(req *http.Request, cachedReq *http.Request, cachedRes *h // When presented with a request, a cache MUST NOT reuse a stored response unless: // - the presented target URI (https://httpwg.org/specs/rfc9110.html#rfc.section.7.1 of [HTTP]) and that of the stored response match, and - if req.URL.String() != cachedReq.URL.String() { + if req.Host != cachedReq.Host || req.URL.String() != cachedReq.URL.String() { + // For SNI compatibility, also compare req.Host res, err := do(req) return false, res, err } diff --git a/testutil/cacher.go b/testutil/cacher.go index f1726a5..d7f6aca 100644 --- a/testutil/cacher.go +++ b/testutil/cacher.go @@ -141,7 +141,7 @@ func (c *GetOnlyCache) Hit() int { func reqToKey(req *http.Request) string { const sep = "|" - seed := req.Method + sep + req.URL.Path + sep + req.URL.RawQuery + seed := req.Method + sep + req.Host + sep + req.URL.Host + sep + req.URL.Path + sep + req.URL.RawQuery sha1 := sha1.New() _, _ = io.WriteString(sha1, strings.ToLower(seed)) //nostyle:handlerrors return hex.EncodeToString(sha1.Sum(nil)) diff --git a/testutil/rcutil.go b/testutil/rcutil.go index 2a8c428..93aa7f0 100644 --- a/testutil/rcutil.go +++ b/testutil/rcutil.go @@ -11,6 +11,7 @@ import ( type cachedReqRes struct { Method string `json:"method"` + Host string `json:"host"` URL string `json:"url"` ReqHeader http.Header `json:"req_header"` ReqBody []byte `json:"req_body"` @@ -24,6 +25,7 @@ type cachedReqRes struct { func encodeReqRes(req *http.Request, res *http.Response) (*cachedReqRes, error) { c := &cachedReqRes{ Method: req.Method, + Host: req.Host, URL: req.URL.String(), ReqHeader: req.Header, @@ -61,6 +63,7 @@ func decodeReqRes(c *cachedReqRes) (*http.Request, *http.Response, error) { } req := &http.Request{ Method: c.Method, + Host: c.Host, URL: u, Header: c.ReqHeader, Body: io.NopCloser(bytes.NewReader(c.ReqBody)),