diff --git a/middleware/content_charset.go b/middleware/content_charset.go index 8e75fe8e4..18f8c76f9 100644 --- a/middleware/content_charset.go +++ b/middleware/content_charset.go @@ -30,6 +30,8 @@ func contentEncoding(ce string, charsets ...string) bool { _, ce = split(strings.ToLower(ce), ";") _, ce = split(ce, "charset=") ce, _ = split(ce, ";") + // Clients often quote the charset parameter: charset="utf-8" + ce = strings.Trim(ce, `"'`) return slices.Contains(charsets, ce) } diff --git a/middleware/content_charset_test.go b/middleware/content_charset_test.go index aee06a2dc..c746c673d 100644 --- a/middleware/content_charset_test.go +++ b/middleware/content_charset_test.go @@ -1,6 +1,7 @@ package middleware import ( + "strings" "net/http" "net/http/httptest" "testing" @@ -125,3 +126,19 @@ func TestContentEncoding(t *testing.T) { t.Error("Want true, got false") } } + +func TestContentCharsetQuoted(t *testing.T) { + t.Parallel() + mw := ContentCharset("utf-8") + body := strings.NewReader(`{}`) + r := httptest.NewRequest("POST", "/", body) + r.Header.Set("Content-Type", `application/json; charset="utf-8"`) + w := httptest.NewRecorder() + router := chi.NewRouter() + router.Use(mw) + router.Post("/", func(w http.ResponseWriter, r *http.Request) {}) + router.ServeHTTP(w, r) + if w.Code != 200 { + t.Fatalf("got %d want 200 for quoted charset", w.Code) + } +}