Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit 4082b08

Browse files
committed
Don't store responses from uncacheable range requests.
Previously, this library would correctly detect range requests and not serve cached responses. However, it would _store_ responses to partial range requests, and then incorrectly serve them for non-range requests, which would result in partial response provided when a full response is expected. This change fixes that. It tracks all uncacheable requests with a single boolean and does not serve, not store responses from cache in those cases. In theory, it is possible to cache (to various degree) or serve from cache requests with range, but that is currently not implemented by this library and the current behavior was resulting in incorrect responses. This commit focuses on fixing invalid behavior first. Optional followup PRs can focus on optimizations by adding support for storing/serving range requests/responses. However, the implementation may not be trivial, and there may be other trade-offs at play.
1 parent 01b2fc8 commit 4082b08

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

‎httpcache.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ func (t *Transport) setModReq(orig, mod *http.Request) {
180180
// will be returned.
181181
func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
182182
cacheKey := cacheKey(req)
183-
cacheableMethod := req.Method == "GET" || req.Method == "HEAD"
183+
cacheable := (req.Method == "GET" || req.Method == "HEAD") && req.Header.Get("range") == ""
184184
var cachedResp *http.Response
185-
if cacheableMethod {
185+
if cacheable {
186186
cachedResp, err = CachedResponse(t.Cache, req)
187187
} else {
188188
// Need to invalidate an existing value
@@ -194,7 +194,7 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
194194
transport = http.DefaultTransport
195195
}
196196

197-
if cachedResp != nil && err == nil && cacheableMethod && req.Header.Get("range") == "" {
197+
if cacheable && cachedResp != nil && err == nil {
198198
if t.MarkCachedResponses {
199199
cachedResp.Header.Set(XFromCache, "1")
200200
}
@@ -281,7 +281,7 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
281281
}
282282
}
283283

284-
if cacheableMethod && canStore(parseCacheControl(req.Header), parseCacheControl(resp.Header)) {
284+
if cacheable && canStore(parseCacheControl(req.Header), parseCacheControl(resp.Header)) {
285285
for _, varyKey := range headerAllCommaSepValues(resp.Header, "vary") {
286286
varyKey = http.CanonicalHeaderKey(varyKey)
287287
fakeHeader := "X-Varied-" + varyKey

0 commit comments

Comments
 (0)