Use a simpler approach append to existing headers
This commit is contained in:
parent
efba126367
commit
8dc4608309
|
@ -132,28 +132,6 @@ func NewUnroutedHandler(config Config) (*UnroutedHandler, error) {
|
||||||
return handler, nil
|
return handler, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func appendHeader(header http.Header, name, value string) {
|
|
||||||
valuesMap := make(map[string]struct{})
|
|
||||||
|
|
||||||
splitAndAddString := func(value string) {
|
|
||||||
for _, v := range strings.Split(value, ",") {
|
|
||||||
valuesMap[strings.TrimSpace(v)] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
splitAndAddString(header.Get(name))
|
|
||||||
splitAndAddString(value)
|
|
||||||
|
|
||||||
uniqValues := []string{}
|
|
||||||
for v, _ := range valuesMap {
|
|
||||||
if v != "" {
|
|
||||||
uniqValues = append(uniqValues, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
header.Set(name, strings.Join(uniqValues, ", "))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Middleware checks various aspects of the request and ensures that it
|
// Middleware checks various aspects of the request and ensures that it
|
||||||
// conforms with the spec. Also handles method overriding for clients which
|
// conforms with the spec. Also handles method overriding for clients which
|
||||||
// cannot make PATCH AND DELETE requests. If you are using the tusd handlers
|
// cannot make PATCH AND DELETE requests. If you are using the tusd handlers
|
||||||
|
@ -179,13 +157,13 @@ func (handler *UnroutedHandler) Middleware(h http.Handler) http.Handler {
|
||||||
|
|
||||||
if r.Method == "OPTIONS" {
|
if r.Method == "OPTIONS" {
|
||||||
// Preflight request
|
// Preflight request
|
||||||
appendHeader(header, "Access-Control-Allow-Methods", "POST, GET, HEAD, PATCH, DELETE, OPTIONS")
|
header.Add("Access-Control-Allow-Methods", "POST, GET, HEAD, PATCH, DELETE, OPTIONS")
|
||||||
appendHeader(header, "Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Upload-Length, Upload-Offset, Tus-Resumable, Upload-Metadata")
|
header.Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Upload-Length, Upload-Offset, Tus-Resumable, Upload-Metadata")
|
||||||
header.Set("Access-Control-Max-Age", "86400")
|
header.Set("Access-Control-Max-Age", "86400")
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Actual request
|
// Actual request
|
||||||
appendHeader(header, "Access-Control-Expose-Headers", "Upload-Offset, Location, Upload-Length, Tus-Version, Tus-Resumable, Tus-Max-Size, Tus-Extension, Upload-Metadata")
|
header.Add("Access-Control-Expose-Headers", "Upload-Offset, Location, Upload-Length, Tus-Version, Tus-Resumable, Tus-Max-Size, Tus-Extension, Upload-Metadata")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue