Skip to content

Commit

Permalink
fix: Retain the original request header
Browse files Browse the repository at this point in the history
  • Loading branch information
mumoshu committed Mar 12, 2022
1 parent 70d975e commit 197a8a3
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
transport = http.DefaultTransport
}

// We have to retain the original request header because some implementations of http.RoundTripper
// modifies the request header.
//
// That is, there might be cases that the X-Varied-* header of the cached response generated from
// this request can differ from the original request header, which makes
// the next request that should be served from cache doesn't pass varyMatches,
// resulting in subsequent requests that shold be served from the cached don't get served from cache.
//
// An example of such http.RoundTripper implementation is ghinstallation.Transport.
// https://github.com/bradleyfalzon/ghinstallation/blob/f6d76acbaf3dfd732504d10cfc3d296ddb96b13a/transport.go#L120
reqHeader := req.Header.Clone()

if cacheable && cachedResp != nil && err == nil {
if t.MarkCachedResponses {
cachedResp.Header.Set(XFromCache, "1")
Expand Down Expand Up @@ -232,11 +244,11 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
}
}

if cacheable && canStore(parseCacheControl(req.Header), parseCacheControl(resp.Header)) {
if cacheable && canStore(parseCacheControl(reqHeader), parseCacheControl(resp.Header)) {
for _, varyKey := range headerAllCommaSepValues(resp.Header, "vary") {
varyKey = http.CanonicalHeaderKey(varyKey)
fakeHeader := "X-Varied-" + varyKey
reqValues := req.Header.Values(varyKey)
reqValues := reqHeader.Values(varyKey)
resp.Header.Del(fakeHeader)
for _, v := range reqValues {
resp.Header.Add(fakeHeader, v)
Expand Down

0 comments on commit 197a8a3

Please sign in to comment.