Skip to content

Commit

Permalink
Fix cache invalid manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Sep 23, 2024
1 parent 27f7c6e commit c4af01a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion crproxy_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (c *CRProxy) cacheBlobContent(ctx context.Context, r *http.Request, blobPat
return 0, errcode.ErrorCodeDenied
}

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
return 0, errcode.ErrorCodeUnknown.WithMessage(fmt.Sprintf("Source response code %d", resp.StatusCode))
}

Expand Down
31 changes: 18 additions & 13 deletions crproxy_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (c *CRProxy) cacheManifestResponse(rw http.ResponseWriter, r *http.Request,
return
}

if resp.StatusCode >= http.StatusInternalServerError {
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest {
if c.cachedManifest(rw, r, info, false) {
if c.logger != nil {
c.logger.Println("origin manifest response 5xx, but hit caches", info.Host, info.Image, err, dumpResponse(resp))
Expand All @@ -81,23 +81,28 @@ func (c *CRProxy) cacheManifestResponse(rw http.ResponseWriter, r *http.Request,
header[key] = v
}

rw.WriteHeader(resp.StatusCode)

if r.Method == http.MethodHead {
rw.WriteHeader(resp.StatusCode)
return
}

body, err := io.ReadAll(resp.Body)
if err != nil {
c.errorResponse(rw, r, err)
return
}
err = c.cacheManifestContent(context.Background(), info, body)
if err != nil {
c.errorResponse(rw, r, err)
return
if resp.StatusCode >= http.StatusOK || resp.StatusCode < http.StatusMultipleChoices {
body, err := io.ReadAll(resp.Body)
if err != nil {
c.errorResponse(rw, r, err)
return
}

err = c.cacheManifestContent(context.Background(), info, body)
if err != nil {
c.errorResponse(rw, r, err)
return
}
rw.Write(body)
} else {
io.Copy(rw, resp.Body)
}
rw.WriteHeader(resp.StatusCode)
rw.Write(body)
}

func (c *CRProxy) cacheManifestContent(ctx context.Context, info *PathInfo, content []byte) error {
Expand Down

0 comments on commit c4af01a

Please sign in to comment.