Skip to content

Commit

Permalink
Add referer to storage
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Nov 20, 2024
1 parent eeae385 commit b0e7bb5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
5 changes: 4 additions & 1 deletion crproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,14 +692,17 @@ 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) redirect(rw http.ResponseWriter, r *http.Request, blobPath string, info *PathInfo) error {
options := map[string]interface{}{
"method": r.Method,
}
linkExpires := c.linkExpires
if linkExpires > 0 {
options["expiry"] = time.Now().Add(linkExpires)
}
if info != nil {
options["referer"] = fmt.Sprintf("%s/%s", info.Host, info.Image)
}
u, err := c.storageDriver.URLFor(r.Context(), blobPath, options)
if err != nil {
return err
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.redirect(rw, r, blobPath, info)
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.redirect(rw, r, blobPath, info)
if err != nil {
if c.logger != nil {
c.logger.Println("failed to redirect", blobPath, err)
Expand Down
20 changes: 16 additions & 4 deletions storage/driver/obs/obs.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,11 +748,23 @@ func (d *driver) URLFor(ctx context.Context, path string, options map[string]int
expiresIn = time.Until(et).Seconds()
}
}

var q map[string]string
referer, ok := options["referer"]
if ok {
refererString, ok := referer.(string)
if ok {
q = map[string]string{
"referer": refererString,
}
}
}
output, err := d.Client.CreateSignedUrl(&obs.CreateSignedUrlInput{
Bucket: d.Bucket,
Key: d.OBSPath(path),
Method: obs.HttpMethodType(methodString),
Expires: int(expiresIn),
Bucket: d.Bucket,
Key: d.OBSPath(path),
Method: obs.HttpMethodType(methodString),
Expires: int(expiresIn),
QueryParams: q,
})
if err != nil {
return "", err
Expand Down
15 changes: 14 additions & 1 deletion storage/driver/oss/oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"io"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -502,7 +503,19 @@ func (d *driver) URLFor(ctx context.Context, path string, options map[string]int
expiresTime = et
}
}
signedURL := d.Bucket.SignedURLWithMethod(methodString, d.ossPath(path), expiresTime, nil, nil)

var q url.Values
referer, ok := options["referer"]
if ok {
refererString, ok := referer.(string)
if ok {
q = url.Values{
"referer": []string{refererString},
}
}
}

signedURL := d.Bucket.SignedURLWithMethod(methodString, d.ossPath(path), expiresTime, q, nil)
return signedURL, nil
}

Expand Down

0 comments on commit b0e7bb5

Please sign in to comment.