Skip to content

Commit

Permalink
opt: preflight cache max age
Browse files Browse the repository at this point in the history
  • Loading branch information
imstevez committed Sep 11, 2023
1 parent 510e7fe commit b5f5e5f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions s3/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ const (
AccessControlAllowHeaders = "Access-Control-Allow-Headers"
AccessControlExposeHeaders = "Access-Control-Expose-Headers"
AccessControlAllowCredentials = "Access-Control-Allow-Credentials"
AccessControlMaxAge = "Access-Control-Max-Age"
)

// object const
Expand Down
3 changes: 2 additions & 1 deletion s3/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
"github.com/bittorrent/go-btfs/s3/services/object"
"github.com/bittorrent/go-btfs/s3/services/sign"
"github.com/bittorrent/go-btfs/s3/utils/hash"
"net/http"
"net/url"
"runtime"
)

var _ Handlerser = (*Handlers)(nil)

type Handlers struct {
headers map[string][]string
headers http.Header
acksvc accesskey.Service
sigsvc sign.Service
objsvc object.Service
Expand Down
14 changes: 8 additions & 6 deletions s3/handlers/handlers_middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import (
"github.com/bittorrent/go-btfs/s3/services/accesskey"
rscors "github.com/rs/cors"
"net/http"
"strconv"
"time"
)

func (h *Handlers) Cors(handler http.Handler) http.Handler {
headers := h.headers
cred := len(headers[consts.AccessControlAllowCredentials]) > 0 &&
headers[consts.AccessControlAllowCredentials][0] == "true"
cred := headers.Get(consts.AccessControlAllowCredentials) == "true"
maxAge, _ := strconv.Atoi(headers.Get(consts.AccessControlMaxAge))
ch := rscors.New(rscors.Options{
AllowedOrigins: headers[consts.AccessControlAllowOrigin],
AllowedMethods: headers[consts.AccessControlAllowMethods],
AllowedHeaders: headers[consts.AccessControlExposeHeaders],
ExposedHeaders: headers[consts.AccessControlAllowHeaders],
AllowedOrigins: headers.Values(consts.AccessControlAllowOrigin),
AllowedMethods: headers.Values(consts.AccessControlAllowMethods),
AllowedHeaders: headers.Values(consts.AccessControlAllowHeaders),
ExposedHeaders: headers.Values(consts.AccessControlExposeHeaders),
MaxAge: maxAge,
AllowCredentials: cred,
})
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
3 changes: 3 additions & 0 deletions s3/handlers/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ var defaultCorsHeaders = []string{
"*",
}

const defaultCorsMaxAge = "36000"

var defaultHeaders = map[string][]string{
consts.AccessControlAllowOrigin: {"*"},
consts.AccessControlAllowMethods: defaultCorsMethods,
consts.AccessControlAllowHeaders: defaultCorsHeaders,
consts.AccessControlExposeHeaders: defaultCorsHeaders,
consts.AccessControlAllowCredentials: {"true"},
consts.AccessControlMaxAge: {defaultCorsMaxAge},
}

type Option func(handlers *Handlers)
Expand Down

0 comments on commit b5f5e5f

Please sign in to comment.