Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 10, 23:45 UTC
| 1 | package internal |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strings" |
| 6 | |
| 7 | "go.uber.org/zap/zapcore" |
| 8 | ) |
| 9 | |
| 10 | // LoggableHTTPHeader makes an HTTP header loggable with zap.Object(). |
| 11 | // Headers with potentially sensitive information (Cookie, Set-Cookie, |
| 12 | // Authorization, and Proxy-Authorization) are logged with empty values. |
| 13 | type LoggableHTTPHeader struct { |
| 14 | http.Header |
| 15 | |
| 16 | ShouldLogCredentials bool |
| 17 | } |
| 18 | |
| 19 | // MarshalLogObject satisfies the zapcore.ObjectMarshaler interface. |
| 20 | func (h LoggableHTTPHeader) MarshalLogObject(enc zapcore.ObjectEncoder) error { |
| 21 | if h.Header == nil { |
| 22 | return nil |
| 23 | } |
| 24 | for key, val := range h.Header { |
| 25 | if !h.ShouldLogCredentials { |
| 26 | switch strings.ToLower(key) { |
| 27 | case "cookie", "set-cookie", "authorization", "proxy-authorization": |
| 28 | val = []string{"REDACTED"} // see #5669. I still think ▒▒▒▒ would be cool. |
| 29 | } |
| 30 | } |
| 31 | enc.AddArray(key, LoggableStringArray(val)) |
| 32 | } |
| 33 | return nil |
| 34 | } |
| 35 | |
| 36 | // LoggableStringArray makes a slice of strings marshalable for logging. |
| 37 | type LoggableStringArray []string |
| 38 | |
| 39 | // MarshalLogArray satisfies the zapcore.ArrayMarshaler interface. |
| 40 | func (sa LoggableStringArray) MarshalLogArray(enc zapcore.ArrayEncoder) error { |
| 41 | if sa == nil { |
| 42 | return nil |
| 43 | } |
| 44 | for _, s := range sa { |
| 45 | enc.AppendString(s) |
| 46 | } |
| 47 | return nil |
| 48 | } |
| 49 | |
| 50 | // Interface guards |
| 51 | var ( |
| 52 | _ zapcore.ObjectMarshaler = (*LoggableHTTPHeader)(nil) |
| 53 | _ zapcore.ArrayMarshaler = (*LoggableStringArray)(nil) |
| 54 | ) |