Skip to content

Commit 882c15e

Browse files
authored
Update content_type.go (#880)
1 parent 1f927a8 commit 882c15e

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

middleware/content_type.go

+9-13
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,40 @@ import (
66
)
77

88
// SetHeader is a convenience handler to set a response header key/value
9-
func SetHeader(key, value string) func(next http.Handler) http.Handler {
9+
func SetHeader(key, value string) func(http.Handler) http.Handler {
1010
return func(next http.Handler) http.Handler {
11-
fn := func(w http.ResponseWriter, r *http.Request) {
11+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1212
w.Header().Set(key, value)
1313
next.ServeHTTP(w, r)
14-
}
15-
return http.HandlerFunc(fn)
14+
})
1615
}
1716
}
1817

1918
// AllowContentType enforces a whitelist of request Content-Types otherwise responds
2019
// with a 415 Unsupported Media Type status.
21-
func AllowContentType(contentTypes ...string) func(next http.Handler) http.Handler {
20+
func AllowContentType(contentTypes ...string) func(http.Handler) http.Handler {
2221
allowedContentTypes := make(map[string]struct{}, len(contentTypes))
2322
for _, ctype := range contentTypes {
2423
allowedContentTypes[strings.TrimSpace(strings.ToLower(ctype))] = struct{}{}
2524
}
2625

2726
return func(next http.Handler) http.Handler {
28-
fn := func(w http.ResponseWriter, r *http.Request) {
27+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2928
if r.ContentLength == 0 {
30-
// skip check for empty content body
29+
// Skip check for empty content body
3130
next.ServeHTTP(w, r)
3231
return
3332
}
3433

35-
s := strings.ToLower(strings.TrimSpace(r.Header.Get("Content-Type")))
36-
if i := strings.Index(s, ";"); i > -1 {
37-
s = s[0:i]
38-
}
34+
s := strings.ToLower(strings.TrimSpace(strings.Split(r.Header.Get("Content-Type"), ";")[0]))
3935

4036
if _, ok := allowedContentTypes[s]; ok {
4137
next.ServeHTTP(w, r)
4238
return
4339
}
4440

4541
w.WriteHeader(http.StatusUnsupportedMediaType)
46-
}
47-
return http.HandlerFunc(fn)
42+
})
4843
}
4944
}
45+

0 commit comments

Comments
 (0)