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

Function newProxyProtoBackend

caddytest/integration/proxyprotocol_test.go:79–127  ·  view source on GitHub ↗

newProxyProtoBackend starts a TCP listener wrapped with go-proxyproto on a random local port and serves requests with a simple "OK" body. The PROXY header source addresses are accumulated in headerAddrs so tests can inspect them.

(t *testing.T)

Source from the content-addressed store, hash-verified

77// header source addresses are accumulated in headerAddrs so tests can
78// inspect them.
79func newProxyProtoBackend(t *testing.T) *proxyProtoBackend {
80 t.Helper()
81
82 b := &proxyProtoBackend{}
83
84 rawLn, err := net.Listen("tcp", "127.0.0.1:0")
85 if err != nil {
86 t.Fatalf("backend: listen: %v", err)
87 }
88
89 // Wrap with go-proxyproto so the PROXY header is stripped and parsed
90 // before the HTTP server sees the connection. We use REQUIRE so that a
91 // missing header returns an error instead of silently passing through.
92 pLn := &goproxy.Listener{
93 Listener: rawLn,
94 Policy: func(_ net.Addr) (goproxy.Policy, error) {
95 return goproxy.REQUIRE, nil
96 },
97 }
98 b.ln = pLn
99
100 // Wrap the handler with h2c support so the backend can speak HTTP/2
101 // cleartext (h2c) as well as plain HTTP/1.1. Without this, Caddy's
102 // reverse proxy would receive a 'frame too large' error when the
103 // upstream transport is configured to use h2c.
104 h2Server := &http2.Server{}
105 handlerFn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
106 // go-proxyproto has already updated the net.Conn's remote
107 // address to the value from the PROXY header; the HTTP server
108 // surfaces it in r.RemoteAddr.
109 b.mu.Lock()
110 b.headerAddrs = append(b.headerAddrs, r.RemoteAddr)
111 b.mu.Unlock()
112 w.WriteHeader(http.StatusOK)
113 _, _ = fmt.Fprint(w, "OK")
114 })
115
116 b.srv = &http.Server{
117 Handler: h2c.NewHandler(handlerFn, h2Server),
118 }
119
120 go b.srv.Serve(pLn) //nolint:errcheck
121 t.Cleanup(func() {
122 _ = b.srv.Close()
123 _ = rawLn.Close()
124 })
125
126 return b
127}
128
129// addr returns the listening address (host:port) of the backend.
130func (b *proxyProtoBackend) addr() string {

Callers 1

testProxyProtocolMatrixFunction · 0.85

Calls 4

ListenMethod · 0.80
CleanupMethod · 0.65
WriteHeaderMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected