github.com/icyphox/x@v0.0.355-0.20220311094250-029bd783e8b8/httpx/content_type.go (about) 1 package httpx 2 3 import ( 4 "mime" 5 "net/http" 6 "strings" 7 8 "github.com/ory/x/stringslice" 9 ) 10 11 // HasContentType determines whether the request `content-type` includes a 12 // server-acceptable mime-type 13 // 14 // Failure should yield an HTTP 415 (`http.StatusUnsupportedMediaType`) 15 func HasContentType(r *http.Request, mimetypes ...string) bool { 16 contentType := r.Header.Get("Content-Type") 17 if contentType == "" { 18 return stringslice.Has(mimetypes, "application/octet-stream") 19 } 20 21 for _, v := range strings.Split(contentType, ",") { 22 t, _, err := mime.ParseMediaType(strings.TrimSpace(v)) 23 if err != nil { 24 break 25 } 26 if stringslice.Has(mimetypes, t) { 27 return true 28 } 29 } 30 return false 31 }