MCPcopy
hub / github.com/caddyserver/caddy / matchHeaders

Function matchHeaders

modules/caddyhttp/matchers.go:1041–1082  ·  view source on GitHub ↗

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 net/http removes it from the header map.

(input, against http.Header, host string, transferEncoding []string, repl *caddy.Replacer)

Source from the content-addressed store, hash-verified

1039// The host parameter should be obtained from the http.Request.Host field since
1040// net/http removes it from the header map.
1041func matchHeaders(input, against http.Header, host string, transferEncoding []string, repl *caddy.Replacer) bool {
1042 for field, allowedFieldVals := range against {
1043 actualFieldVals := getHeaderFieldVals(input, field, host, transferEncoding)
1044 if allowedFieldVals != nil && len(allowedFieldVals) == 0 && actualFieldVals != nil {
1045 // a non-nil but empty list of allowed values means
1046 // match if the header field exists at all
1047 continue
1048 }
1049 if allowedFieldVals == nil && actualFieldVals == nil {
1050 // a nil list means match if the header does not exist at all
1051 continue
1052 }
1053 var match bool
1054 fieldVals:
1055 for _, actualFieldVal := range actualFieldVals {
1056 for _, allowedFieldVal := range allowedFieldVals {
1057 if repl != nil {
1058 allowedFieldVal = repl.ReplaceAll(allowedFieldVal, "")
1059 }
1060 switch {
1061 case allowedFieldVal == "*":
1062 match = true
1063 case strings.HasPrefix(allowedFieldVal, "*") && strings.HasSuffix(allowedFieldVal, "*"):
1064 match = strings.Contains(actualFieldVal, allowedFieldVal[1:len(allowedFieldVal)-1])
1065 case strings.HasPrefix(allowedFieldVal, "*"):
1066 match = strings.HasSuffix(actualFieldVal, allowedFieldVal[1:])
1067 case strings.HasSuffix(allowedFieldVal, "*"):
1068 match = strings.HasPrefix(actualFieldVal, allowedFieldVal[:len(allowedFieldVal)-1])
1069 default:
1070 match = actualFieldVal == allowedFieldVal
1071 }
1072 if match {
1073 break fieldVals
1074 }
1075 }
1076 }
1077 if !match {
1078 return false
1079 }
1080 }
1081 return true
1082}
1083
1084// CaddyModule returns the Caddy module information.
1085func (MatchHeaderRE) CaddyModule() caddy.ModuleInfo {

Callers 2

MatchMethod · 0.85
MatchWithErrorMethod · 0.85

Calls 2

getHeaderFieldValsFunction · 0.85
ReplaceAllMethod · 0.80

Tested by

no test coverage detected