Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: Handle missing but unambiguous ETags in response #1159

Merged
merged 2 commits into from
Sep 5, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions source/http/httpsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ func (hs *httpSourceHandler) CacheKey(ctx context.Context, index int) (string, b
req = req.WithContext(ctx)
m := map[string]*metadata.StorageItem{}

// If we request a single ETag in 'If-None-Match', some servers omit the
// unambiguous ETag in their response.
// See: https://github.com/moby/buildkit/issues/905
var onlyETag string

if len(sis) > 0 {
for _, si := range sis {
// if metaDigest := getMetaDigest(si); metaDigest == hs.formatCacheKey("") {
Expand All @@ -160,6 +165,10 @@ func (hs *httpSourceHandler) CacheKey(ctx context.Context, index int) (string, b
etags = append(etags, t)
}
req.Header.Add("If-None-Match", strings.Join(etags, ", "))

if len(etags) == 1 {
onlyETag = etags[0]
}
}
}

Expand All @@ -172,6 +181,9 @@ func (hs *httpSourceHandler) CacheKey(ctx context.Context, index int) (string, b
if err == nil {
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusNotModified {
respETag := resp.Header.Get("ETag")
if respETag == "" && onlyETag != "" {
rwe marked this conversation as resolved.
Show resolved Hide resolved
respETag = onlyETag
}
si, ok := m[respETag]
if ok {
hs.refID = si.ID()
Expand All @@ -197,6 +209,13 @@ func (hs *httpSourceHandler) CacheKey(ctx context.Context, index int) (string, b
}
if resp.StatusCode == http.StatusNotModified {
respETag := resp.Header.Get("ETag")
if respETag == "" && onlyETag != "" {
respETag = onlyETag

// Set the missing ETag header on the response so that it's available
// to .save()
resp.Header.Set("ETag", onlyETag)
}
si, ok := m[respETag]
if !ok {
return "", false, errors.Errorf("invalid not-modified ETag: %v", respETag)
Expand Down