Skip to content

Commit

Permalink
Head requests are no longer redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Sep 11, 2024
1 parent 27f7c6e commit 9d73af6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 14 additions & 1 deletion crproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"net/textproto"
"net/url"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -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,
}
Expand Down
4 changes: 2 additions & 2 deletions crproxy_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9d73af6

Please sign in to comment.