Browse Source

s3: pass through s3 presigned headers

fix https://github.com/chrislusf/seaweedfs/discussions/2502
pull/2515/head
chrislu 3 years ago
parent
commit
7210558c7b
  1. 9
      weed/s3api/http/header.go
  2. 32
      weed/s3api/s3api_object_handlers.go
  3. 16
      weed/server/common.go
  4. 2
      weed/server/filer_server_handlers_read.go
  5. 2
      weed/server/volume_server_handlers_read.go

9
weed/s3api/http/header.go

@ -52,3 +52,12 @@ func GetBucketAndObject(r *http.Request) (bucket, object string) {
return
}
var PassThroughHeaders = map[string]string{
"response-cache-control": "Cache-Control",
"response-content-disposition": "Content-Disposition",
"response-content-encoding": "Content-Encoding",
"response-content-language": "Content-Language",
"response-content-type": "Content-Type",
"response-expires": "Expires",
}

32
weed/s3api/s3api_object_handlers.go

@ -306,15 +306,6 @@ func (s3a *S3ApiServer) doDeleteEmptyDirectories(client filer_pb.SeaweedFilerCli
return
}
var passThroughHeaders = []string{
"response-cache-control",
"response-content-disposition",
"response-content-encoding",
"response-content-language",
"response-content-type",
"response-expires",
}
func (s3a *S3ApiServer) proxyToFiler(w http.ResponseWriter, r *http.Request, destUrl string, responseFn func(proxyResponse *http.Response, w http.ResponseWriter) (statusCode int)) {
glog.V(3).Infof("s3 proxying %s to %s", r.Method, destUrl)
@ -328,25 +319,14 @@ func (s3a *S3ApiServer) proxyToFiler(w http.ResponseWriter, r *http.Request, des
}
proxyReq.Header.Set("X-Forwarded-For", r.RemoteAddr)
for header, values := range r.Header {
// handle s3 related headers
passed := false
for _, h := range passThroughHeaders {
if strings.ToLower(header) == h && len(values) > 0 {
proxyReq.Header.Add(header[len("response-"):], values[0])
passed = true
break
}
}
if passed {
continue
}
// handle other headers
for _, value := range values {
proxyReq.Header.Add(header, value)
for k, v := range r.URL.Query() {
if _, ok := xhttp.PassThroughHeaders[strings.ToLower(k)]; ok {
proxyReq.Header[k] = v
}
}
for header, values := range r.Header {
proxyReq.Header[header] = values
}
resp, postErr := client.Do(proxyReq)

16
weed/server/common.go

@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
"io"
"io/fs"
"mime/multipart"
@ -250,13 +251,16 @@ func handleStaticResources2(r *mux.Router) {
r.PathPrefix("/seaweedfsstatic/").Handler(http.StripPrefix("/seaweedfsstatic", http.FileServer(http.FS(StaticFS))))
}
func adjustHeaderContentDisposition(w http.ResponseWriter, r *http.Request, filename string) {
responseContentDisposition := r.FormValue("response-content-disposition")
if responseContentDisposition != "" {
w.Header().Set("Content-Disposition", responseContentDisposition)
return
func adjustPassthroughHeaders(w http.ResponseWriter, r *http.Request, filename string) {
for header, values := range r.Header {
if normalizedHeader, ok := xhttp.PassThroughHeaders[strings.ToLower(header)]; ok {
w.Header()[normalizedHeader] = values
}
}
if w.Header().Get("Content-Disposition") != "" {
adjustHeaderContentDisposition(w, r, filename)
}
func adjustHeaderContentDisposition(w http.ResponseWriter, r *http.Request, filename string) {
if contentDisposition := w.Header().Get("Content-Disposition"); contentDisposition != "" {
return
}
if filename != "" {

2
weed/server/filer_server_handlers_read.go

@ -130,7 +130,7 @@ func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request)
setEtag(w, etag)
filename := entry.Name()
adjustHeaderContentDisposition(w, r, filename)
adjustPassthroughHeaders(w, r, filename)
totalSize := int64(entry.Size())

2
weed/server/volume_server_handlers_read.go

@ -301,7 +301,7 @@ func writeResponseContent(filename, mimeType string, rs io.ReadSeeker, w http.Re
}
w.Header().Set("Accept-Ranges", "bytes")
adjustHeaderContentDisposition(w, r, filename)
adjustPassthroughHeaders(w, r, filename)
if r.Method == "HEAD" {
w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))

Loading…
Cancel
Save