From 4d0a3f9ff3c3af25dbd601253363d16670885d75 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Sun, 13 Feb 2022 21:21:38 -0800 Subject: [PATCH] http: handle weak etags on comparisons When server returns a weak etag, the prefix can cause a mismatch on direct comparison with old value. Signed-off-by: Tonis Tiigi --- source/http/httpsource.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/source/http/httpsource.go b/source/http/httpsource.go index bd54c91e..968c6356 100644 --- a/source/http/httpsource.go +++ b/source/http/httpsource.go @@ -181,7 +181,7 @@ func (hs *httpSourceHandler) CacheKey(ctx context.Context, g session.Group, inde resp, err := client.Do(req) if err == nil { if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusNotModified { - respETag := resp.Header.Get("ETag") + respETag := etagValue(resp.Header.Get("ETag")) // If a 304 is returned without an ETag and we had only sent one ETag, // the response refers to the ETag we asked about. @@ -212,7 +212,7 @@ func (hs *httpSourceHandler) CacheKey(ctx context.Context, g session.Group, inde return "", "", nil, false, errors.Errorf("invalid response status %d", resp.StatusCode) } if resp.StatusCode == http.StatusNotModified { - respETag := resp.Header.Get("ETag") + respETag := etagValue(resp.Header.Get("ETag")) if respETag == "" && onlyETag != "" { respETag = onlyETag @@ -350,6 +350,7 @@ func (hs *httpSourceHandler) save(ctx context.Context, resp *http.Response, s se dgst = digest.NewDigest(digest.SHA256, h) if respETag := resp.Header.Get("ETag"); respETag != "" { + respETag = etagValue(respETag) if err := md.setETag(respETag); err != nil { return nil, "", err } @@ -471,3 +472,8 @@ func (md cacheRefMetadata) getHTTPModTime() string { func (md cacheRefMetadata) setHTTPModTime(s string) error { return md.SetString(keyModTime, s, "") } + +func etagValue(v string) string { + // remove weak for direct comparison + return strings.TrimPrefix(v, "W/") +}