chi/middleware/content_charset.go
Vojtech Vitek (golang.cz) 6075570612
middleware: parse quoted ContentCharset media parameters (#1218)
* fix: parse HTTP charset parameters with quoted values

* middleware: simplify ContentCharset media parameter parsing

Drop the hand-written quoted-pair normalizer and the httptest-based
test from the original change. mime.ParseMediaType handles quoted
charset values on its own, and the table test covers the remaining
cases in-process.

Co-Authored-By: tianrking <10758833+tianrking@users.noreply.github.com>
Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Vojtech Vitek <vojtech.vitek@golang.cz>

* middleware: cover charset review edge cases

Rename the backslash row to describe what it tests, and add rows for
xcharset (not a charset parameter) and a malformed parameter that
falls back to the legacy parser. Document quoted charset values on
ContentCharset.

Co-Authored-By: tianrking <10758833+tianrking@users.noreply.github.com>
Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Vojtech Vitek <vojtech.vitek@golang.cz>

---------

Co-authored-by: tianrking <10758833+tianrking@users.noreply.github.com>
Co-authored-by: Claude Haiku 5.5 <noreply@anthropic.com>
2026-10-14 01:23:36 +02:00

59 lines
1.6 KiB
Go

package middleware
import (
"mime"
"net/http"
"slices"
"strings"
)
// ContentCharset generates a handler that writes a 415 Unsupported Media Type response if none of the charsets match.
// An empty charset will allow requests with no Content-Type header or no specified charset.
// Requests without a body (ContentLength == 0) are always allowed.
// Quoted charset values, such as charset="UTF-8", are accepted.
func ContentCharset(charsets ...string) func(next http.Handler) http.Handler {
for i, c := range charsets {
charsets[i] = strings.ToLower(c)
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ContentLength == 0 {
next.ServeHTTP(w, r)
return
}
if !contentEncoding(r.Header.Get("Content-Type"), charsets...) {
w.WriteHeader(http.StatusUnsupportedMediaType)
return
}
next.ServeHTTP(w, r)
})
}
}
// Check the content encoding against a list of acceptable values.
func contentEncoding(ce string, charsets ...string) bool {
_, params, err := mime.ParseMediaType(ce)
if err == nil {
return slices.Contains(charsets, strings.ToLower(params["charset"]))
}
// Keep the previous lenient handling when the header is not a valid media type.
_, ce = split(strings.ToLower(ce), ";")
_, ce = split(ce, "charset=")
ce, _ = split(ce, ";")
return slices.Contains(charsets, ce)
}
// Split a string in two parts, cleaning any whitespace.
func split(str, sep string) (string, string) {
a, b, found := strings.Cut(str, sep)
a = strings.TrimSpace(a)
if found {
b = strings.TrimSpace(b)
}
return a, b
}