MCPcopy
hub / github.com/caddyserver/caddy / directRequest

Method directRequest

modules/caddyhttp/reverseproxy/reverseproxy.go:1402–1426  ·  modules/caddyhttp/reverseproxy/reverseproxy.go::Handler.directRequest

directRequest modifies only req.URL so that it points to the upstream in the given DialInfo. It must modify ONLY the request URL.

(req *http.Request, di DialInfo)

Source from the content-addressed store, hash-verified

1400// directRequest modifies only req.URL so that it points to the upstream
1401// in the given DialInfo. It must modify ONLY the request URL.
1402func (h *Handler) directRequest(req *http.Request, di DialInfo) {
1403 // we need a host, so set the upstream's host address
1404 reqHost := di.Address
1405
1406 // if the port equates to the scheme, strip the port because
1407 // it's weird to make a request like http://example.com:80/.
1408 if (req.URL.Scheme == "http" && di.Port == "80") ||
1409 (req.URL.Scheme == "https" && di.Port == "443") {
1410 reqHost = di.Host
1411 }
1412
1413 // add client address to the host to let transport differentiate requests from different clients
1414 if ppt, ok := h.Transport.(ProxyProtocolTransport); ok && ppt.ProxyProtocolEnabled() {
1415 if proxyProtocolInfo, ok := caddyhttp.GetVar(req.Context(), proxyProtocolInfoVarKey).(ProxyProtocolInfo); ok {
1416 // encode the request so it plays well with h2 transport, it's unnecessary for h1 but anyway
1417 // The issue is that h2 transport will use the address to determine if new connections are needed
1418 // to roundtrip requests but the without escaping, new connections are constantly created and closed until
1419 // file descriptors are exhausted.
1420 // see: https://github.com/caddyserver/caddy/issues/7529
1421 reqHost = url.QueryEscape(proxyProtocolInfo.AddrPort.String() + "->" + reqHost)
1422 }
1423 }
1424
1425 req.URL.Host = reqHost
1426}
1427
1428func (h Handler) provisionUpstream(upstream *Upstream, dynamic bool) {
1429 // create or get the host representation for this upstream;

Callers 1

reverseProxyMethod · 0.95

Calls 3

GetVarFunction · 0.92
ProxyProtocolEnabledMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected