@@ -6,44 +6,40 @@ import (
6
6
)
7
7
8
8
// 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 {
10
10
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 ) {
12
12
w .Header ().Set (key , value )
13
13
next .ServeHTTP (w , r )
14
- }
15
- return http .HandlerFunc (fn )
14
+ })
16
15
}
17
16
}
18
17
19
18
// AllowContentType enforces a whitelist of request Content-Types otherwise responds
20
19
// 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 {
22
21
allowedContentTypes := make (map [string ]struct {}, len (contentTypes ))
23
22
for _ , ctype := range contentTypes {
24
23
allowedContentTypes [strings .TrimSpace (strings .ToLower (ctype ))] = struct {}{}
25
24
}
26
25
27
26
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 ) {
29
28
if r .ContentLength == 0 {
30
- // skip check for empty content body
29
+ // Skip check for empty content body
31
30
next .ServeHTTP (w , r )
32
31
return
33
32
}
34
33
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 ]))
39
35
40
36
if _ , ok := allowedContentTypes [s ]; ok {
41
37
next .ServeHTTP (w , r )
42
38
return
43
39
}
44
40
45
41
w .WriteHeader (http .StatusUnsupportedMediaType )
46
- }
47
- return http .HandlerFunc (fn )
42
+ })
48
43
}
49
44
}
45
+
0 commit comments