Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f4d4455

Browse files
author
Peter Kieltyka
committed
README: include new middlewares
1 parent 1cbc3a2 commit f4d4455

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ with `net/http` can be used with chi's mux.
320320
-----------------------------------------------------------------------------------------------------------
321321
| chi/middlware Handler | description |
322322
|:----------------------|:---------------------------------------------------------------------------------
323+
| AllowContentType | Explicit whitelist of accepted request Content-Types |
323324
| Compress | Gzip compression for clients that accept compressed responses |
324325
| GetHead | Automatically route undefined HEAD requests to GET handlers |
325326
| Heartbeat | Monitoring endpoint to check the servers pulse |
@@ -330,6 +331,7 @@ with `net/http` can be used with chi's mux.
330331
| Recoverer | Gracefully absorb panics and prints the stack trace |
331332
| RequestID | Injects a request ID into the context of each request |
332333
| RedirectSlashes | Redirect slashes on routing paths |
334+
| SetHeader | Short-hand middleware to set a response header key/value |
333335
| StripSlashes | Strip slashes on routing paths |
334336
| Throttle | Puts a ceiling on the number of concurrent requests |
335337
| Timeout | Signals to the request context when the timeout deadline is reached |

middleware/content_type.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,19 @@ func AllowContentType(contentTypes ...string) func(next http.Handler) http.Handl
2626

2727
return func(next http.Handler) http.Handler {
2828
fn := func(w http.ResponseWriter, r *http.Request) {
29-
s := strings.ToLower(strings.TrimSpace(strings.Split(r.Header.Get("Content-Type"), ";")[0]))
29+
s := strings.ToLower(strings.TrimSpace(r.Header.Get("Content-Type")))
30+
if i := strings.Index(s, ";"); i > -1 {
31+
s = s[0:i]
32+
}
3033

31-
f := false
3234
for _, t := range cT {
3335
if t == s {
34-
f = true
35-
break
36+
next.ServeHTTP(w, r)
37+
return
3638
}
3739
}
3840

39-
if !f {
40-
w.WriteHeader(http.StatusUnsupportedMediaType)
41-
return
42-
}
43-
44-
next.ServeHTTP(w, r)
41+
w.WriteHeader(http.StatusUnsupportedMediaType)
4542
}
4643
return http.HandlerFunc(fn)
4744
}

0 commit comments

Comments
 (0)