Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Dec 26, 2024
1 parent 7c8c98a commit b693261
Showing 1 changed file with 12 additions and 35 deletions.
47 changes: 12 additions & 35 deletions gateway/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"net/textproto"
"net/url"
"slices"
"strconv"
"strings"
"time"
Expand All @@ -17,20 +16,7 @@ import (
)

func (c *Gateway) cacheManifestResponse(rw http.ResponseWriter, r *http.Request, info *PathInfo, t *token.Token) {
var acceptItems []string

if !info.IsDigestManifests {
list := strings.Split(r.Header.Get("Accept"), ",")
for _, item := range list {
item = strings.TrimSpace(item)
_, ok := c.accepts[item]
if ok {
acceptItems = append(acceptItems, item)
}
}
}

if c.tryFirstServeCachedManifest(rw, r, info, acceptItems) {
if c.tryFirstServeCachedManifest(rw, r, info) {
return
}

Expand All @@ -55,16 +41,12 @@ func (c *Gateway) cacheManifestResponse(rw http.ResponseWriter, r *http.Request,
if info.IsDigestManifests {
forwardReq.Header.Set("Accept", r.Header.Get("Accept"))
} else {
if len(acceptItems) != 0 {
forwardReq.Header.Set("Accept", strings.Join(acceptItems, ","))
} else {
forwardReq.Header.Set("Accept", r.Header.Get("Accept"))
}
forwardReq.Header.Set("Accept", strings.Join(c.acceptsItems, ","))
}

resp, err := c.httpClient.Do(forwardReq)
if err != nil {
if c.fallbackServeCachedManifest(rw, r, info, acceptItems) {
if c.fallbackServeCachedManifest(rw, r, info) {
return
}
c.logger.Error("failed to request", "url", u, "error", err)
Expand All @@ -77,7 +59,7 @@ func (c *Gateway) cacheManifestResponse(rw http.ResponseWriter, r *http.Request,

switch resp.StatusCode {
case http.StatusUnauthorized, http.StatusForbidden:
if c.fallbackServeCachedManifest(rw, r, info, acceptItems) {
if c.fallbackServeCachedManifest(rw, r, info) {
c.logger.Error("origin manifest response 40x, but hit caches", "url", u, "error", err, "response", dumpResponse(resp))
return
}
Expand All @@ -87,19 +69,19 @@ func (c *Gateway) cacheManifestResponse(rw http.ResponseWriter, r *http.Request,
}

if resp.StatusCode >= http.StatusBadRequest && resp.StatusCode < http.StatusInternalServerError {
if c.fallbackServeCachedManifest(rw, r, info, acceptItems) {
if c.fallbackServeCachedManifest(rw, r, info) {
c.logger.Error("origin manifest response 4xx, but hit caches", "url", u, "error", err, "response", dumpResponse(resp))
return
}
c.logger.Error("origin manifest response 4xx", "url", u, "error", err, "response", dumpResponse(resp))
} else if resp.StatusCode >= http.StatusInternalServerError {
if c.fallbackServeCachedManifest(rw, r, info, acceptItems) {
if c.fallbackServeCachedManifest(rw, r, info) {
c.logger.Error("origin manifest response 5xx, but hit caches", "url", u, "error", err, "response", dumpResponse(resp))
return
}
c.logger.Error("origin manifest response 5xx", "url", u, "error", err, "response", dumpResponse(resp))
} else if resp.StatusCode < http.StatusOK {
if c.fallbackServeCachedManifest(rw, r, info, acceptItems) {
if c.fallbackServeCachedManifest(rw, r, info) {
c.logger.Error("origin manifest response 1xx, but hit caches", "url", u, "error", err, "response", dumpResponse(resp))
return
}
Expand Down Expand Up @@ -143,7 +125,7 @@ func (c *Gateway) cacheManifestResponse(rw http.ResponseWriter, r *http.Request,
}
}

func (c *Gateway) tryFirstServeCachedManifest(rw http.ResponseWriter, r *http.Request, info *PathInfo, acceptItems []string) bool {
func (c *Gateway) tryFirstServeCachedManifest(rw http.ResponseWriter, r *http.Request, info *PathInfo) bool {
if !info.IsDigestManifests && c.manifestCacheDuration > 0 {
last, ok := c.manifestCache.Load(manifestCacheKey(info))
if !ok {
Expand All @@ -155,18 +137,18 @@ func (c *Gateway) tryFirstServeCachedManifest(rw http.ResponseWriter, r *http.Re
}
}

return c.serveCachedManifest(rw, r, info, acceptItems)
return c.serveCachedManifest(rw, r, info)
}

func (c *Gateway) fallbackServeCachedManifest(rw http.ResponseWriter, r *http.Request, info *PathInfo, acceptItems []string) bool {
func (c *Gateway) fallbackServeCachedManifest(rw http.ResponseWriter, r *http.Request, info *PathInfo) bool {
if info.IsDigestManifests {
return false
}

return c.serveCachedManifest(rw, r, info, acceptItems)
return c.serveCachedManifest(rw, r, info)
}

func (c *Gateway) serveCachedManifest(rw http.ResponseWriter, r *http.Request, info *PathInfo, acceptItems []string) bool {
func (c *Gateway) serveCachedManifest(rw http.ResponseWriter, r *http.Request, info *PathInfo) bool {
ctx := r.Context()

content, digest, mediaType, err := c.cache.GetManifestContent(ctx, info.Host, info.Image, info.Manifests)
Expand All @@ -175,11 +157,6 @@ func (c *Gateway) serveCachedManifest(rw http.ResponseWriter, r *http.Request, i
return false
}

if len(acceptItems) != 0 && !slices.Contains(acceptItems, mediaType) {
c.logger.Warn("Manifest cache hit, but type not match", "item", acceptItems, "mediaType", mediaType)
return false
}

c.logger.Info("Manifest cache hit", "digest", digest)
rw.Header().Set("Docker-Content-Digest", digest)
rw.Header().Set("Content-Type", mediaType)
Expand Down

0 comments on commit b693261

Please sign in to comment.