caddy/modules/caddyhttp/reverseproxy
Kévin Dunglas 857d5731f3
caddyhttp: optimise header access and fix encoding negotiation (#8025)
* perf: bypass header key normalization for canonical literals

http.Header's Get, Set, Add and Del run textproto.CanonicalMIMEHeaderKey
on every call. When the key is a canonical literal the result is the key
itself, so the scan is pure overhead. Access the map directly instead.

Only hot, allocation-free paths are converted, and only where the key is
a canonical literal. Call sites taking a key from config, a template or
the request itself still go through the normalizing methods, since they
need it for correctness.

allHeaderValues and lastHeaderValue dropped their http.CanonicalHeaderKey
call for the same reason: all callers pass canonical literals.

benchstat, Apple M5 Pro, go1.27.1:

    PrepareRequest-15      659.4n ± 2%  ->  457.7n ± 2%  -30.58% (p=0.000 n=12)
    AcceptedEncodings-15   330.8n ± 5%  ->  309.4n ± 7%   -6.44% (p=0.019 n=10)

    ResponseWriterWriteHeader/event-stream-15    1007.5n ± 3%  ->  732.5n ± 1%  -27.30%
    ResponseWriterWriteHeader/informational-15     60.0n ± 3%  ->   48.0n ± 2%  -20.01%
    ResponseWriterWriteHeader/not-modified-15     214.3n ± 6%  ->  175.3n ± 4%  -18.20%
    ResponseWriterWriteHeader/connect-15           60.2n ± 3%  ->   51.9n ± 4%  -13.85%
    ResponseWriterWriteHeader/html-15             206.5n ± 4%  ->  197.9n ± 2%   -4.19%
    geomean                                                                    -14.38%

Allocations are unchanged.

* tests: verify http.Header map keys are canonical

Indexing http.Header directly skips the canonicalization Get/Set/Add/Del
perform, which also puts the key outside what CI checks. canonicalheader
only inspects method calls. staticcheck's SA1008 covers reads, but stops
descending as soon as an assignment writes to a header, so it misses the
write, the delete builtin and the read-modify-write append. A key that is
not canonical in those positions is a silent lookup miss, not a compile
error.

Walk the loaded packages and verify every string constant used as a key in
an http.Header map operation matches http.CanonicalHeaderKey. A helper that
takes the key from its caller would hide the literal again, so a parameter
named with a canonical prefix opts its arguments into the same check.

The WebSocket headers Caddy deliberately writes with RFC 6455 casing are
allowed explicitly; they are only ever written out, never looked up against
a parsed header map.

Covers 64 operations, runs in under a second, and promotes golang.org/x/tools
from an indirect to a direct dependency at the version already in the module
graph.

* headercheck: resolve aliases of http.Header

* headercheck: check http.Header composite literal keys

* headercheck: note overlap with canonicalheader

* headercheck: drop parameter-name check

Helpers now take the indexed values, so every key sits in an index expression the analyzer and the linter already check.

* headercheck: scope RFC 6455 exceptions to their call sites

A key-based allow-list also accepted reads such as h["Sec-WebSocket-Key"], which miss because parsed headers are canonical. Honor //nolint:canonicalheader on the line instead, which the linter will use too.

* encode: read every Accept-Encoding field line

AcceptedEncodings read only the first field line, so a client that split
the list across several lines had the rest ignored. RFC 9110 section 5.3
treats repeated list-field lines as one comma-joined list. Iterate over
the items of every line instead of joining them, and skip empty items,
which used to yield an empty encoding. file_server's precompressed
selection uses the same function and is fixed with it.

Fixes https://github.com/caddyserver/caddy/issues/8272

Co-authored-by: Kağan Küçük <kaankck@gmail.com>

* encode: read every Cache-Control and Content-Encoding field line

Only the first field line was read, but RFC 9110 section 5.3 makes
multiple lines of a list-based field equivalent to one comma-joined
value:

- Cache-Control: no-transform on a later line was ignored, so the
  request or response was compressed anyway.
- Content-Encoding: an empty first line followed by a real coding let
  the handler encode the body a second time.

* encode: parse Cache-Control no-transform per RFC 9111

The substring match missed No-Transform, though RFC 9111 section 5.2 compares directive names case-insensitively, and fired on text that is not the directive: longer tokens such as x-no-transform-ext, quoted arguments such as community="no-transform", and semicolon-separated values, which are not valid Cache-Control syntax. Split the field on commas outside quoted strings and match only a well-formed no-transform element.

* encode: keep accepted encoding preferences on the stack

AcceptedEncodings counted commas to size the preference slice, and the variable capacity put it on the heap. A constant capacity covers typical Accept-Encoding headers and lets the compiler keep the slice on the stack; append still moves it to the heap for longer ones. BenchmarkAcceptedEncodings: 151.8ns -> 120.5ns (-21%), 192 -> 64 B/op, 2 -> 1 allocs/op (n=8).

* encode: build the encoded ETag without fmt

fmt.Sprintf boxed both arguments and parsed the format on every encoded response with a strong ETag. Concatenation: 47ns -> 16.5ns, 56 -> 24 B, 3 -> 1 allocs.

* reverseproxy: access response Server and Via headers directly

finalizeResponse runs on every proxied response; skip the key normalization as prepareRequest already does on the request side.

* encode: access headers directly in Write, WriteHeader and Close

These run once per response; convert the remaining literal-key Get, Set and Add calls like the rest of the handler.

* encode: match Cache-Control no-transform with httpguts

httpguts.HeaderValuesContainsToken already compares list elements case-insensitively across field lines, so the hand-written list parser is unnecessary. A no-transform inside a comma-separated quoted argument now also disables encoding, which only errs toward not transforming.

* encode: inline the no-transform check

isEncodeAllowed only wrapped one httpguts call. Its unit test now drives the response directive through ServeHTTP instead.

---------

Co-authored-by: Kağan Küçük <kaankck@gmail.com>
2026-10-29 23:19:50 +10:00
..
fastcgi fastcgi: keep the php_fastcgi directory redirect on the same origin (#8285) 2026-10-28 09:45:37 +00:00
forwardauth forwardauth: error on duplicate uri subdirective (#7814) 2026-06-10 23:31:04 -04:00
active_health_test.go Revert #8223 2026-10-17 15:28:01 +08:00
addresses.go
addresses_test.go
admin.go reverseproxy: Track dynamic upstreams, enable passive healthchecking (#7539) 2026-03-04 15:05:26 -05:00
admin_test.go reverseproxy: Track dynamic upstreams, enable passive healthchecking (#7539) 2026-03-04 15:05:26 -05:00
ascii.go
ascii_test.go
buffering_test.go reverseproxy: Don't drop read errors with unlimited buffers (#8207) 2026-10-14 00:25:34 +10:00
caddyfile.go reverseproxy: Optionally detach stream (websockets) from config lifecycle (#7649) 2026-10-04 07:45:10 -04:00
client_disconnect_test.go reverseproxy: log HTTP/3 client cancellations at debug level (#8135) 2026-10-04 12:48:06 +00:00
command.go
copyresponse.go reverseproxy: strip hop-by-hop headers when copying response headers (#8270) 2026-10-26 09:21:29 +00:00
copyresponse_test.go reverseproxy: strip hop-by-hop headers when copying response headers (#8270) 2026-10-26 09:21:29 +00:00
dynamic_upstreams_test.go reverseproxy: Track dynamic upstreams, enable passive healthchecking (#7539) 2026-03-04 15:05:26 -05:00
extended_connect_test.go reverseproxy: Optionally detach stream (websockets) from config lifecycle (#7649) 2026-10-04 07:45:10 -04:00
headers_test.go reverseproxy: strip hop-by-hop headers from interim responses (#8256) 2026-10-20 17:11:30 +10:00
healthchecks.go Revert #8223 2026-10-17 15:28:01 +08:00
hopheaders_test.go reverseproxy: Optionally detach stream (websockets) from config lifecycle (#7649) 2026-10-04 07:45:10 -04:00
hosts.go reverseproxy: isolate active health-check state per distinct check config (#7916) 2026-08-12 15:20:15 +10:00
httptransport.go caddytls: validate curve and protocol names, compare protocol IDs (#8266) 2026-10-26 20:14:59 +00:00
httptransport_test.go caddytls: validate curve and protocol names, compare protocol IDs (#8266) 2026-10-26 20:14:59 +00:00
incremental_test.go feat: implement the Incremental header field (RFC 10036) (#8020) 2026-10-02 20:59:34 -04:00
metrics.go reverseproxy: Optionally detach stream (websockets) from config lifecycle (#7649) 2026-10-04 07:45:10 -04:00
metrics_test.go reverseproxy: Optionally detach stream (websockets) from config lifecycle (#7649) 2026-10-04 07:45:10 -04:00
passive_health_test.go caddyhttp, reverseproxy: experimental WebTransport passthrough (#7669) 2026-10-12 23:24:10 -04:00
prepare_request_bench_test.go reverseproxy: reduce allocation/CPU overhead in request hot paths (#7925) 2026-08-10 15:49:55 +10:00
request_trailers_test.go reverseproxy: preserve request trailer values (#8246) 2026-10-19 09:16:58 +00:00
retries_test.go reverseproxy: retry dial errors wrapped by a forward proxy (#8264) 2026-10-21 11:49:32 +00:00
reverseproxy.go caddyhttp: optimise header access and fix encoding negotiation (#8025) 2026-10-29 23:19:50 +10:00
selectionpolicies.go reverseproxy: Don't panic in weighted_round_robin with fewer weights than upstreams (#8159) 2026-10-07 04:36:10 +00:00
selectionpolicies_test.go reverseproxy: Don't panic in weighted_round_robin with fewer weights than upstreams (#8159) 2026-10-07 04:36:10 +00:00
streaming.go caddyhttp: optimise header access and fix encoding negotiation (#8025) 2026-10-29 23:19:50 +10:00
streaming_test.go reverseproxy: close backend body and hijacked socket on upgrade setup failures (#8269) 2026-10-24 22:19:19 +00:00
tracing_test.go reverseproxy: emit OpenTelemetry client spans (#8177) 2026-10-23 16:28:55 +10:00
upstreams.go reverseproxy: deduplicate DNS SRV+A discovery into modules/internal/network (#7790) 2026-10-12 10:37:55 +00:00
upstreams_bench_test.go reverseproxy: preallocate upstream slices with known sizes (#7926) 2026-08-10 15:51:50 +10:00
upstreams_test.go
webtransport_pump.go caddyhttp, reverseproxy: experimental WebTransport passthrough (#7669) 2026-10-12 23:24:10 -04:00
webtransport_pump_test.go caddyhttp, reverseproxy: experimental WebTransport passthrough (#7669) 2026-10-12 23:24:10 -04:00
webtransport_transport.go caddyhttp, reverseproxy: experimental WebTransport passthrough (#7669) 2026-10-12 23:24:10 -04:00
webtransport_transport_test.go caddyhttp, reverseproxy: experimental WebTransport passthrough (#7669) 2026-10-12 23:24:10 -04:00