From 9d73af694153926f28e0960ec3a7a52d7368519a Mon Sep 17 00:00:00 2001 From: Shiming Zhang Date: Wed, 11 Sep 2024 13:37:08 +0800 Subject: [PATCH] Head requests are no longer redirect --- crproxy.go | 15 ++++++++++++++- crproxy_blob.go | 4 ++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/crproxy.go b/crproxy.go index 70a5a05..c896d9b 100644 --- a/crproxy.go +++ b/crproxy.go @@ -10,6 +10,7 @@ import ( "net/http" "net/textproto" "net/url" + "strconv" "strings" "sync" "time" @@ -677,7 +678,19 @@ func (c *CRProxy) notFoundResponse(rw http.ResponseWriter, r *http.Request) { http.NotFound(rw, r) } -func (c *CRProxy) redirect(rw http.ResponseWriter, r *http.Request, blobPath string) error { +func (c *CRProxy) redirectBlob(rw http.ResponseWriter, r *http.Request, blobPath string) error { + if r.Method == http.MethodHead { + file, err := c.storageDriver.Stat(r.Context(), blobPath) + if err != nil { + return err + } + + rw.Header().Set("Content-Length", strconv.FormatInt(file.Size(), 10)) + rw.Header().Set("Last-Modified", file.ModTime().UTC().Format(http.TimeFormat)) + rw.WriteHeader(http.StatusOK) + return nil + } + options := map[string]interface{}{ "method": r.Method, } diff --git a/crproxy_blob.go b/crproxy_blob.go index 5ceed5a..8eb0380 100644 --- a/crproxy_blob.go +++ b/crproxy_blob.go @@ -68,7 +68,7 @@ func (c *CRProxy) cacheBlobResponse(rw http.ResponseWriter, r *http.Request, inf } } - err = c.redirect(rw, r, blobPath) + err = c.redirectBlob(rw, r, blobPath) if err == nil { return } @@ -120,7 +120,7 @@ func (c *CRProxy) cacheBlobResponse(rw http.ResponseWriter, r *http.Request, inf } } - err = c.redirect(rw, r, blobPath) + err = c.redirectBlob(rw, r, blobPath) if err != nil { if c.logger != nil { c.logger.Println("failed to redirect", blobPath, err)