Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 08:30 UTC
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.
Kévin Dunglas committed
Jul 30, 2026 at 12:40 UTC
3be8dabc89315f2f2a40855efe0a4d6e9e7acc9b
4 files changed
+7
-3
.golangci.yml
+4
@@ -19,6 +19,7 @@ linters:
19
- asciicheck
20
- bidichk
21
- bodyclose
22
+ - canonicalheader
23
- decorder
24
- dogsled
25
- dupl
@@ -97,6 +98,9 @@ linters:
98
- linters:
99
- errcheck
100
path: _test\.go
101
+ - linters:
102
+ - canonicalheader
103
+ path: _test\.go
104
paths:
105
- third_party$
106
- builtin$
modules/caddyhttp/caddyauth/basicauth.go
+1
-1
@@ -212,7 +212,7 @@ func (hba HTTPBasicAuth) promptForCredentials(w http.ResponseWriter, err error)
212
if realm == "" {
213
realm = "restricted"
214
}
215
- w.Header().Set("WWW-Authenticate", fmt.Sprintf(`Basic realm="%s"`, realm))
215
+ w.Header().Set("Www-Authenticate", fmt.Sprintf(`Basic realm="%s"`, realm))
216
return User{}, false, err
217
}
218
modules/caddyhttp/encode/encode.go
+1
-1
@@ -499,10 +499,10 @@ func hasVaryValue(hdr http.Header, target string) bool {
499
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html.
500
func AcceptedEncodings(r *http.Request, preferredOrder []string) []string {
501
acceptEncHeader := r.Header.Get("Accept-Encoding")
502
- websocketKey := r.Header.Get("Sec-WebSocket-Key")
502
if acceptEncHeader == "" {
503
return []string{}
504
}
505
+ websocketKey := r.Header.Get("Sec-Websocket-Key")
506
507
prefs := []encodingPreference{}
508
modules/caddyhttp/matchers.go
+1
-1
@@ -1368,7 +1368,7 @@ func (m MatchProtocol) Match(r *http.Request) bool {
1368
func (m MatchProtocol) MatchWithError(r *http.Request) (bool, error) {
1369
switch string(m) {
1370
case "grpc":
1371
- return strings.HasPrefix(r.Header.Get("content-type"), "application/grpc"), nil
1371
+ return strings.HasPrefix(r.Header.Get("Content-Type"), "application/grpc"), nil
1372
case "https":
1373
return r.TLS != nil, nil
1374
case "http":