Documentation
¶
Index ¶
- func HijackHandlerWithDialer(dialer upstream.RequestDialer, inactivityTimeout time.Duration, ...) http.Handler
- func HijackShutdown(ctx context.Context) <-chan struct{}
- func IsAlwaysStreamingPath(method, path string) bool
- func IsStreamingRequest(w http.ResponseWriter, r *http.Request) bool
- func IsStreamingRequestForFlavor(w http.ResponseWriter, r *http.Request, podmanUpstream bool) bool
- func MayStreamForApproval(w http.ResponseWriter, r *http.Request, podmanUpstream bool) bool
- func NewWithOptions(upstreamSocket string, logger *slog.Logger, opts Options) *httputil.ReverseProxy
- func NewWithTransport(rt http.RoundTripper, logger *slog.Logger, opts Options) *httputil.ReverseProxy
- func PassClientUpgrade(next http.Handler) http.Handler
- func WithFullDuplex(next http.Handler) http.Handler
- func WithHijackShutdown(ctx context.Context, done <-chan struct{}) context.Context
- func WithRequestTimeoutForFlavor(next http.Handler, timeout time.Duration, podmanUpstream bool) http.Handler
- func WithUpstreamDeadlines(next http.Handler, requestTimeout time.Duration, podmanUpstream bool) http.Handler
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HijackHandlerWithDialer ¶
func HijackHandlerWithDialer(dialer upstream.RequestDialer, inactivityTimeout time.Duration, logger *slog.Logger, next http.Handler) http.Handler
HijackHandlerWithDialer wraps a standard handler and intercepts Docker API endpoints that use HTTP connection upgrades (attach, exec start). For these endpoints it dials the upstream directly through an upstream.RequestDialer (typically an *upstream.Resolver) and performs a native bidirectional hijack with optimized buffers and proper TCP half-close signaling, rather than relying on the stdlib reverse proxy's generic upgrade handling. The hijack path dials the same active endpoint — local socket or remote TCP+TLS — and fails over together with the rest of the proxy.
inactivityTimeout bounds how long a hijacked connection may pass no bytes in either direction before it is torn down; it is the caller-resolved value of upstream.hijack_inactivity_timeout, not a package constant, so a configured override reaches every connection this handler upgrades.
func HijackShutdown ¶
HijackShutdown returns the shutdown channel installed by WithHijackShutdown, or nil (which never fires) when none is present.
func IsAlwaysStreamingPath ¶ added in v3.2.0
IsAlwaysStreamingPath reports whether the method and normalized path alone require the streaming or upgrade path, regardless of query or engine flavor.
func IsStreamingRequest ¶
func IsStreamingRequest(w http.ResponseWriter, r *http.Request) bool
IsStreamingRequest reports whether r is an open-ended stream that holds a connection for as long as a client cares to keep it: events, followed container, service and task logs, streaming stats (including Podman's collection GET /libpod/containers/stats), Podman top?stream, wait, attach, and exec start (which hijacks). The rate-limit middleware exempts these from the concurrency caps and bounds them with the separate stream cap instead.
It is deliberately narrower than the upstream-timeout classifier. Build, prune, image pull/push/load, export and archive are long but finite, heavy operations, and max_inflight is exactly what should bound how many of them run at once, so they keep counting. Libpod paths are normalized to their Docker form before matching.
func IsStreamingRequestForFlavor ¶
IsStreamingRequestForFlavor is IsStreamingRequest with the upstream flavor known. A Podman upstream also streams the compat GET /containers/{id}/top when stream is true, which a Docker daemon ignores.
func MayStreamForApproval ¶ added in v3.2.0
MayStreamForApproval excludes a hold if either supported query interpretation can produce a stream. This wider check never changes concurrency accounting.
func NewWithOptions ¶
func NewWithOptions(upstreamSocket string, logger *slog.Logger, opts Options) *httputil.ReverseProxy
NewWithOptions creates a reverse proxy that forwards requests to the upstream Docker socket and optionally enforces response-side policy. It is the single-local-socket shorthand: callers with a plain socket path get a one-endpoint resolver. The multi-endpoint/remote path uses NewWithTransport.
func NewWithTransport ¶
func NewWithTransport(rt http.RoundTripper, logger *slog.Logger, opts Options) *httputil.ReverseProxy
NewWithTransport creates a reverse proxy that forwards requests through rt — typically an *upstream.Resolver, which owns endpoint selection, per-endpoint connection pooling (MaxIdleConns 100, IdleConnTimeout 90s, ResponseHeader timeout 30s, matching the historical single-socket transport), client TLS, and automatic failover. Streaming endpoints (logs follow, events, stats) send headers promptly and stream the body, so the header timeout does not cap long-lived responses; hijacked attach/exec-start connections bypass this pooled transport entirely.
func PassClientUpgrade ¶
PassClientUpgrade wraps a hijack handler so it forwards the client's upgrade choice instead of always asking the upstream to upgrade. Only a positively probed Docker daemon is known to answer the plain form with a 200 raw stream; Podman's libpod endpoints don't, so every other upstream keeps the forced `Connection: Upgrade` / `Upgrade: tcp` request and its 101 answer.
func WithFullDuplex ¶ added in v3.2.0
WithFullDuplex lets the proxied handler keep reading the request body after it has started writing the response.
net/http's HTTP/1 server is half-duplex by default: once the handler writes response headers, the server drains up to 256 KiB of the unread request body and closes the rest. ReverseProxy flushes the daemon's headers the moment they arrive (FlushInterval -1), so a daemon that answers while it is still reading the upload cuts the client's stream off mid-body. Docker Engine 29.5 does exactly that for POST /build (moby/moby#52364): it writes progress while it is still reading the context, and a streamed build upload fails with "unexpected EOF".
Apply this innermost, directly around the reverse proxy, so it runs only after every filter, inspector, ownership and rate-limit layer has admitted the request. The error is ignored on purpose: HTTP/2 connections are already full-duplex and report http.ErrNotSupported, and writers that do not unwrap to the connection (httptest recorders) report the same. In both cases there is nothing to enable.
func WithHijackShutdown ¶
WithHijackShutdown returns a context carrying a channel that is closed when the server begins shutting down. Hijacked sessions watch it to close both legs promptly. It is a dedicated signal rather than request-context cancellation because net/http cancels a request context on any client EOF, which would wrongly kill a session whose client merely half-closed stdin while the container is still producing output.
func WithRequestTimeoutForFlavor ¶
func WithRequestTimeoutForFlavor(next http.Handler, timeout time.Duration, podmanUpstream bool) http.Handler
WithRequestTimeoutForFlavor wraps next so that ordinary finite upstream requests are bounded by a total per-request deadline. When the deadline fires, the proxy transport aborts the upstream connection. This is the body-phase backstop that ResponseHeaderTimeout cannot provide: a daemon that sends headers promptly and then hangs the body would otherwise pin the request until the client gives up.
What the client sees depends on whether response headers were already committed, and only the first case is a 504:
- Deadline fires BEFORE response headers arrive: the error surfaces from RoundTrip, the ReverseProxy ErrorHandler runs, and the client gets a 504 (reasonCodeUpstreamRequestTimeout).
- Deadline fires AFTER response headers are committed: Go's ReverseProxy does not route a copyResponse read error to ErrorHandler, and HTTP does not allow replacing a sent status anyway. The client keeps the committed status and sees a truncated body. The request is still aborted, which is the point, but there is no 504 and cannot be one.
The second case is the one this wrapper exists for, which makes it easy to describe backwards; both this comment and the docs did until 2026-08-29. TestWithRequestTimeout_BodyPhaseTruncatesRatherThanReturning504 pins it.
A non-positive timeout disables the wrapper entirely — next is returned unchanged. Long-lived endpoints (event streams, follow/stream reads including service and task logs, streaming native Podman container/pod top, image pull/create/export/build/push, plugin create/pull/push/upgrade, container export/get, container archive i.e. docker cp, websocket attach, the BuildKit tunnel endpoints POST /session and POST /grpc, and the blocking container wait and the prune endpoints) are exempt, because a deadline would sever a legitimately long response. Container and pod stop/restart skip the response-header deadline but get a request deadline of max(timeout, grace+30s), grace being the clamped (at most 1h) ?t= value (default 10s). GET /system/df skips only the response-header deadline. Hijacked endpoints (attach, exec start) never reach this handler: HijackHandlerWithDialer short-circuits them earlier in the chain.
podmanUpstream must be true only when the upstream has been resolved as Podman; Docker-compatible top can stream on Podman but is always finite on Docker.
func WithUpstreamDeadlines ¶
func WithUpstreamDeadlines(next http.Handler, requestTimeout time.Duration, podmanUpstream bool) http.Handler
WithUpstreamDeadlines applies both upstream time bounds to next: the total per-request deadline (WithRequestTimeoutForFlavor) and the response-header deadline. Requests with any exemption skip the header deadline; the header deadline lives on the transport, so those requests are marked with upstream.MarkLongLived for the resolver to route over its header-deadline-free transport. The mark is set even when the request timeout is disabled, because the header deadline is a separate setting. The total deadline is skipped only for fully exempt requests; stop/restart get a deadline derived from their grace period and /system/df keeps the configured request timeout.