@marcel91200 / caddy Mirror · read-only / commits / c216cf55
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 12, 04:30 UTC

caddyhttp: Allow matching Transfer-Encoding, add to access logs (#6629)

* caddyhttp: Allow matching Transfer-Encoding * Log transfer_encoding on the request --------- Co-authored-by: Matt Holt <mholt@users.noreply.github.com>

Francis Lavoie committed Dec 20, 2024 at 13:16 UTC c216cf551dcbd2de1da1b9fe8a7e179b76827753
3 files changed +15 -8
modules/caddyhttp/marshalers.go
+3
@@ -51,6 +51,9 @@ func (r LoggableHTTPRequest) MarshalLogObject(enc zapcore.ObjectEncoder) error {
51 Header: r.Header,
52 ShouldLogCredentials: r.ShouldLogCredentials,
53 })
54 + if r.TransferEncoding != nil {
55 + enc.AddArray("transfer_encoding", LoggableStringArray(r.TransferEncoding))
56 + }
57 if r.TLS != nil {
58 enc.AddObject("tls", LoggableTLSConnState(*r.TLS))
59 }
modules/caddyhttp/matchers.go
+11 -7
@@ -978,7 +978,7 @@ func (m MatchHeader) Match(r *http.Request) bool {
978 // MatchWithError returns true if r matches m.
979 func (m MatchHeader) MatchWithError(r *http.Request) (bool, error) {
980 repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
981 - return matchHeaders(r.Header, http.Header(m), r.Host, repl), nil
981 + return matchHeaders(r.Header, http.Header(m), r.Host, r.TransferEncoding, repl), nil
982 }
983
984 // CELLibrary produces options that expose this matcher for use in CEL
@@ -1004,22 +1004,26 @@ func (MatchHeader) CELLibrary(_ caddy.Context) (cel.Library, error) {
1004 }
1005
1006 // getHeaderFieldVals returns the field values for the given fieldName from input.
1007 -// The host parameter should be obtained from the http.Request.Host field since
1008 -// net/http removes it from the header map.
1009 -func getHeaderFieldVals(input http.Header, fieldName, host string) []string {
1007 +// The host parameter should be obtained from the http.Request.Host field, and the
1008 +// transferEncoding from http.Request.TransferEncoding, since net/http removes them
1009 +// from the header map.
1010 +func getHeaderFieldVals(input http.Header, fieldName, host string, transferEncoding []string) []string {
1011 fieldName = textproto.CanonicalMIMEHeaderKey(fieldName)
1012 if fieldName == "Host" && host != "" {
1013 return []string{host}
1014 }
1015 + if fieldName == "Transfer-Encoding" && input[fieldName] == nil {
1016 + return transferEncoding
1017 + }
1018 return input[fieldName]
1019 }
1020
1021 // matchHeaders returns true if input matches the criteria in against without regex.
1022 // The host parameter should be obtained from the http.Request.Host field since
1023 // net/http removes it from the header map.
1020 -func matchHeaders(input, against http.Header, host string, repl *caddy.Replacer) bool {
1024 +func matchHeaders(input, against http.Header, host string, transferEncoding []string, repl *caddy.Replacer) bool {
1025 for field, allowedFieldVals := range against {
1022 - actualFieldVals := getHeaderFieldVals(input, field, host)
1026 + actualFieldVals := getHeaderFieldVals(input, field, host, transferEncoding)
1027 if allowedFieldVals != nil && len(allowedFieldVals) == 0 && actualFieldVals != nil {
1028 // a non-nil but empty list of allowed values means
1029 // match if the header field exists at all
@@ -1119,7 +1123,7 @@ func (m MatchHeaderRE) Match(r *http.Request) bool {
1123 // MatchWithError returns true if r matches m.
1124 func (m MatchHeaderRE) MatchWithError(r *http.Request) (bool, error) {
1125 for field, rm := range m {
1122 - actualFieldVals := getHeaderFieldVals(r.Header, field, r.Host)
1126 + actualFieldVals := getHeaderFieldVals(r.Header, field, r.Host, r.TransferEncoding)
1127 match := false
1128 fieldVal:
1129 for _, actualFieldVal := range actualFieldVals {
modules/caddyhttp/responsematchers.go
+1 -1
@@ -41,7 +41,7 @@ func (rm ResponseMatcher) Match(statusCode int, hdr http.Header) bool {
41 if !rm.matchStatusCode(statusCode) {
42 return false
43 }
44 - return matchHeaders(hdr, rm.Headers, "", nil)
44 + return matchHeaders(hdr, rm.Headers, "", []string{}, nil)
45 }
46
47 func (rm ResponseMatcher) matchStatusCode(statusCode int) bool {