Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package caddyhttp |
| 16 | |
| 17 | import ( |
| 18 | "crypto/tls" |
| 19 | "net" |
| 20 | "net/http" |
| 21 | |
| 22 | "go.uber.org/zap/zapcore" |
| 23 | |
| 24 | "github.com/caddyserver/caddy/v2/internal" |
| 25 | ) |
| 26 | |
| 27 | // LoggableHTTPRequest makes an HTTP request loggable with zap.Object(). |
| 28 | type LoggableHTTPRequest struct { |
| 29 | *http.Request |
| 30 | |
| 31 | ShouldLogCredentials bool |
| 32 | } |
| 33 | |
| 34 | // MarshalLogObject satisfies the zapcore.ObjectMarshaler interface. |
| 35 | func (r LoggableHTTPRequest) MarshalLogObject(enc zapcore.ObjectEncoder) error { |
| 36 | ip, port, err := net.SplitHostPort(r.RemoteAddr) |
| 37 | if err != nil { |
| 38 | ip = r.RemoteAddr |
| 39 | port = "" |
| 40 | } |
| 41 | |
| 42 | enc.AddString("remote_ip", ip) |
| 43 | enc.AddString("remote_port", port) |
| 44 | if ip, ok := GetVar(r.Context(), ClientIPVarKey).(string); ok { |
| 45 | enc.AddString("client_ip", ip) |
| 46 | } |
| 47 | enc.AddString("proto", r.Proto) |
| 48 | enc.AddString("method", r.Method) |
| 49 | enc.AddString("host", r.Host) |
| 50 | enc.AddString("uri", r.RequestURI) |
| 51 | enc.AddObject("headers", internal.LoggableHTTPHeader{ |
| 52 | Header: r.Header, |
| 53 | ShouldLogCredentials: r.ShouldLogCredentials, |
| 54 | }) |
| 55 | if r.TransferEncoding != nil { |
| 56 | enc.AddArray("transfer_encoding", internal.LoggableStringArray(r.TransferEncoding)) |
| 57 | } |
| 58 | if r.TLS != nil { |
| 59 | enc.AddObject("tls", LoggableTLSConnState(*r.TLS)) |
| 60 | } |
| 61 | return nil |
| 62 | } |
| 63 | |
| 64 | // LoggableHTTPHeader makes an HTTP header loggable with zap.Object(). |
| 65 | type LoggableHTTPHeader = internal.LoggableHTTPHeader |
| 66 | |
| 67 | // LoggableStringArray makes a slice of strings marshalable for logging. |
| 68 | type LoggableStringArray = internal.LoggableStringArray |
| 69 | |
| 70 | // LoggableTLSConnState makes a TLS connection state loggable with zap.Object(). |
| 71 | type LoggableTLSConnState tls.ConnectionState |
| 72 | |
| 73 | // MarshalLogObject satisfies the zapcore.ObjectMarshaler interface. |
| 74 | func (t LoggableTLSConnState) MarshalLogObject(enc zapcore.ObjectEncoder) error { |
| 75 | enc.AddBool("resumed", t.DidResume) |
| 76 | enc.AddUint16("version", t.Version) |
| 77 | enc.AddUint16("cipher_suite", t.CipherSuite) |
| 78 | enc.AddString("proto", t.NegotiatedProtocol) |
| 79 | enc.AddString("server_name", t.ServerName) |
| 80 | enc.AddBool("ech", t.ECHAccepted) |
| 81 | if len(t.PeerCertificates) > 0 { |
| 82 | enc.AddString("client_common_name", t.PeerCertificates[0].Subject.CommonName) |
| 83 | enc.AddString("client_serial", t.PeerCertificates[0].SerialNumber.String()) |
| 84 | } |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | // Interface guards |
| 89 | var ( |
| 90 | _ zapcore.ObjectMarshaler = (*LoggableHTTPRequest)(nil) |
| 91 | _ zapcore.ObjectMarshaler = (*LoggableTLSConnState)(nil) |
| 92 | ) |