caddyhttp: use canonical header key casing to avoid re-canonicalization (#7911)

Header.Get/Set re-canonicalize and allocate whenever the passed key
isn't already in canonical MIME header form. Sec-WebSocket-Key,
WWW-Authenticate, and content-type all miss the fast path; switch to
their canonical forms (Sec-Websocket-Key, Www-Authenticate,
Content-Type).

Enable the canonicalheader linter to catch future regressions,
excluded in test files since those already use non-canonical casing
in several places without a perf-sensitive path behind them.
This commit is contained in:
Kévin Dunglas 2026-07-30 12:40:35 +02:00 committed by GitHub
parent c96bca1269
commit 3be8dabc89
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 7 additions and 3 deletions

View file

@ -19,6 +19,7 @@ linters:
- asciicheck
- bidichk
- bodyclose
- canonicalheader
- decorder
- dogsled
- dupl
@ -97,6 +98,9 @@ linters:
- linters:
- errcheck
path: _test\.go
- linters:
- canonicalheader
path: _test\.go
paths:
- third_party$
- builtin$

View file

@ -212,7 +212,7 @@ func (hba HTTPBasicAuth) promptForCredentials(w http.ResponseWriter, err error)
if realm == "" {
realm = "restricted"
}
w.Header().Set("WWW-Authenticate", fmt.Sprintf(`Basic realm="%s"`, realm))
w.Header().Set("Www-Authenticate", fmt.Sprintf(`Basic realm="%s"`, realm))
return User{}, false, err
}

View file

@ -499,10 +499,10 @@ func hasVaryValue(hdr http.Header, target string) bool {
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html.
func AcceptedEncodings(r *http.Request, preferredOrder []string) []string {
acceptEncHeader := r.Header.Get("Accept-Encoding")
websocketKey := r.Header.Get("Sec-WebSocket-Key")
if acceptEncHeader == "" {
return []string{}
}
websocketKey := r.Header.Get("Sec-Websocket-Key")
prefs := []encodingPreference{}

View file

@ -1368,7 +1368,7 @@ func (m MatchProtocol) Match(r *http.Request) bool {
func (m MatchProtocol) MatchWithError(r *http.Request) (bool, error) {
switch string(m) {
case "grpc":
return strings.HasPrefix(r.Header.Get("content-type"), "application/grpc"), nil
return strings.HasPrefix(r.Header.Get("Content-Type"), "application/grpc"), nil
case "https":
return r.TLS != nil, nil
case "http":