Skip to content

Commit

Permalink
Merge pull request #29 from k1LoW/fix-url-match
Browse files Browse the repository at this point in the history
For SNI compatibility, also compare req.Host
  • Loading branch information
k1LoW authored Dec 15, 2023
2 parents 89d106b + a27c2c6 commit 664d17b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
3 changes: 2 additions & 1 deletion rfc9111/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion testutil/cacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 3 additions & 0 deletions testutil/rcutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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,

Expand Down Expand Up @@ -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)),
Expand Down

0 comments on commit 664d17b

Please sign in to comment.