2020-11-13 12:51:07 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SetHeader is a convenience handler to set a response header key/value
|
|
|
|
func SetHeader(key, value string) func(next http.Handler) http.Handler {
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set(key, value)
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllowContentType enforces a whitelist of request Content-Types otherwise responds
|
|
|
|
// with a 415 Unsupported Media Type status.
|
|
|
|
func AllowContentType(contentTypes ...string) func(next http.Handler) http.Handler {
|
2021-01-05 13:05:40 +00:00
|
|
|
allowedContentTypes := make(map[string]struct{}, len(contentTypes))
|
|
|
|
for _, ctype := range contentTypes {
|
|
|
|
allowedContentTypes[strings.TrimSpace(strings.ToLower(ctype))] = struct{}{}
|
2020-11-13 12:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.ContentLength == 0 {
|
|
|
|
// skip check for empty content body
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s := strings.ToLower(strings.TrimSpace(r.Header.Get("Content-Type")))
|
|
|
|
if i := strings.Index(s, ";"); i > -1 {
|
|
|
|
s = s[0:i]
|
|
|
|
}
|
|
|
|
|
2021-01-05 13:05:40 +00:00
|
|
|
if _, ok := allowedContentTypes[s]; ok {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
2020-11-13 12:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusUnsupportedMediaType)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|
|
|
|
}
|