Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions middleware/content_charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
17 changes: 17 additions & 0 deletions middleware/content_charset_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package middleware

import (
"strings"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -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)
}
}