1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00

caddyhttp: Allow matching Transfer-Encoding

This commit is contained in:
Francis Lavoie 2024-10-15 11:22:28 -04:00
parent a211c656f1
commit 4091e93eb0
No known key found for this signature in database
GPG key ID: 52BC55A211F19186
2 changed files with 12 additions and 8 deletions

View file

@ -941,7 +941,7 @@ func (m *MatchHeader) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
// Match returns true if r matches m. // Match returns true if r matches m.
func (m MatchHeader) Match(r *http.Request) bool { func (m MatchHeader) Match(r *http.Request) bool {
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer) repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
return matchHeaders(r.Header, http.Header(m), r.Host, repl) return matchHeaders(r.Header, http.Header(m), r.Host, r.TransferEncoding, repl)
} }
// CELLibrary produces options that expose this matcher for use in CEL // CELLibrary produces options that expose this matcher for use in CEL
@ -967,22 +967,26 @@ func (MatchHeader) CELLibrary(_ caddy.Context) (cel.Library, error) {
} }
// getHeaderFieldVals returns the field values for the given fieldName from input. // getHeaderFieldVals returns the field values for the given fieldName from input.
// The host parameter should be obtained from the http.Request.Host field since // The host parameter should be obtained from the http.Request.Host field, and the
// net/http removes it from the header map. // transferEncoding from http.Request.TransferEncoding, since net/http removes them
func getHeaderFieldVals(input http.Header, fieldName, host string) []string { // from the header map.
func getHeaderFieldVals(input http.Header, fieldName, host string, transferEncoding []string) []string {
fieldName = textproto.CanonicalMIMEHeaderKey(fieldName) fieldName = textproto.CanonicalMIMEHeaderKey(fieldName)
if fieldName == "Host" && host != "" { if fieldName == "Host" && host != "" {
return []string{host} return []string{host}
} }
if fieldName == "Transfer-Encoding" && input[fieldName] == nil {
return transferEncoding
}
return input[fieldName] return input[fieldName]
} }
// matchHeaders returns true if input matches the criteria in against without regex. // matchHeaders returns true if input matches the criteria in against without regex.
// The host parameter should be obtained from the http.Request.Host field since // The host parameter should be obtained from the http.Request.Host field since
// net/http removes it from the header map. // net/http removes it from the header map.
func matchHeaders(input, against http.Header, host string, repl *caddy.Replacer) bool { func matchHeaders(input, against http.Header, host string, transferEncoding []string, repl *caddy.Replacer) bool {
for field, allowedFieldVals := range against { for field, allowedFieldVals := range against {
actualFieldVals := getHeaderFieldVals(input, field, host) actualFieldVals := getHeaderFieldVals(input, field, host, transferEncoding)
if allowedFieldVals != nil && len(allowedFieldVals) == 0 && actualFieldVals != nil { if allowedFieldVals != nil && len(allowedFieldVals) == 0 && actualFieldVals != nil {
// a non-nil but empty list of allowed values means // a non-nil but empty list of allowed values means
// match if the header field exists at all // match if the header field exists at all
@ -1076,7 +1080,7 @@ func (m *MatchHeaderRE) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
// Match returns true if r matches m. // Match returns true if r matches m.
func (m MatchHeaderRE) Match(r *http.Request) bool { func (m MatchHeaderRE) Match(r *http.Request) bool {
for field, rm := range m { for field, rm := range m {
actualFieldVals := getHeaderFieldVals(r.Header, field, r.Host) actualFieldVals := getHeaderFieldVals(r.Header, field, r.Host, r.TransferEncoding)
match := false match := false
fieldVal: fieldVal:
for _, actualFieldVal := range actualFieldVals { for _, actualFieldVal := range actualFieldVals {

View file

@ -41,7 +41,7 @@ func (rm ResponseMatcher) Match(statusCode int, hdr http.Header) bool {
if !rm.matchStatusCode(statusCode) { if !rm.matchStatusCode(statusCode) {
return false return false
} }
return matchHeaders(hdr, rm.Headers, "", nil) return matchHeaders(hdr, rm.Headers, "", []string{}, nil)
} }
func (rm ResponseMatcher) matchStatusCode(statusCode int) bool { func (rm ResponseMatcher) matchStatusCode(statusCode int) bool {