@marcel91200 / caddy Mirror · read-only / commits / 8e5aafa5
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 01:30 UTC

fastcgi: Fix a TODO, prevent zap using reflection for logging env (#4437)

* fastcgi: Fix a TODO, prevent zap using reflection for logging env * Update modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go Co-authored-by: Mohammed Al Sahaf <msaa1990@gmail.com> Co-authored-by: Mohammed Al Sahaf <msaa1990@gmail.com>

Francis Lavoie committed Dec 2, 2021 at 15:23 UTC 8e5aafa5cdb0bd6ad062014172ed21fdc1012cc1
1 file changed +17 -4
modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go
+17 -4
@@ -29,6 +29,7 @@ import (
29 "github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy"
30 "github.com/caddyserver/caddy/v2/modules/caddytls"
31 "go.uber.org/zap"
32 + "go.uber.org/zap/zapcore"
33
34 "github.com/caddyserver/caddy/v2"
35 )
@@ -132,7 +133,7 @@ func (t Transport) RoundTrip(r *http.Request) (*http.Response, error) {
133 t.logger.Debug("roundtrip",
134 zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}),
135 zap.String("dial", address),
135 - zap.Any("env", env), // TODO: this uses reflection I think
136 + zap.Object("env", env),
137 )
138
139 fcgiBackend, err := DialContext(ctx, network, address)
@@ -171,10 +172,10 @@ func (t Transport) RoundTrip(r *http.Request) (*http.Response, error) {
172 }
173
174 // buildEnv returns a set of CGI environment variables for the request.
174 -func (t Transport) buildEnv(r *http.Request) (map[string]string, error) {
175 +func (t Transport) buildEnv(r *http.Request) (envVars, error) {
176 repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
177
177 - var env map[string]string
178 + var env envVars
179
180 // Separate remote IP and port; more lenient than net.SplitHostPort
181 var ip, port string
@@ -259,7 +260,7 @@ func (t Transport) buildEnv(r *http.Request) (map[string]string, error) {
260
261 // Some variables are unused but cleared explicitly to prevent
262 // the parent environment from interfering.
262 - env = map[string]string{
263 + env = envVars{
264 // Variables defined in CGI 1.1 spec
265 "AUTH_TYPE": "", // Not used
266 "CONTENT_LENGTH": r.Header.Get("Content-Length"),
@@ -353,6 +354,16 @@ func (t Transport) splitPos(path string) int {
354 return -1
355 }
356
357 +// envVars is a simple type to allow for speeding up zap log encoding.
358 +type envVars map[string]string
359 +
360 +func (env envVars) MarshalLogObject(enc zapcore.ObjectEncoder) error {
361 + for k, v := range env {
362 + enc.AddString(k, v)
363 + }
364 + return nil
365 +}
366 +
367 // Map of supported protocols to Apache ssl_mod format
368 // Note that these are slightly different from SupportedProtocols in caddytls/config.go
369 var tlsProtocolStrings = map[uint16]string{
@@ -366,6 +377,8 @@ var headerNameReplacer = strings.NewReplacer(" ", "_", "-", "_")
377
378 // Interface guards
379 var (
380 + _ zapcore.ObjectMarshaler = (*envVars)(nil)
381 +
382 _ caddy.Provisioner = (*Transport)(nil)
383 _ http.RoundTripper = (*Transport)(nil)
384 )