UnmarshalCaddyfile implements caddyfile.Unmarshaler.
(d *caddyfile.Dispenser)
| 946 | |
| 947 | // UnmarshalCaddyfile implements caddyfile.Unmarshaler. |
| 948 | func (m *MatchHeader) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { |
| 949 | if *m == nil { |
| 950 | *m = make(map[string][]string) |
| 951 | } |
| 952 | // iterate to merge multiple matchers into one |
| 953 | for d.Next() { |
| 954 | var field, val string |
| 955 | if !d.Args(&field) { |
| 956 | return d.Errf("malformed header matcher: expected field") |
| 957 | } |
| 958 | |
| 959 | if strings.HasPrefix(field, "!") { |
| 960 | if len(field) == 1 { |
| 961 | return d.Errf("malformed header matcher: must have field name following ! character") |
| 962 | } |
| 963 | |
| 964 | field = field[1:] |
| 965 | headers := *m |
| 966 | headers[field] = nil |
| 967 | m = &headers |
| 968 | if d.NextArg() { |
| 969 | return d.Errf("malformed header matcher: null matching headers cannot have a field value") |
| 970 | } |
| 971 | } else { |
| 972 | if !d.NextArg() { |
| 973 | return d.Errf("malformed header matcher: expected both field and value") |
| 974 | } |
| 975 | |
| 976 | // If multiple header matchers with the same header field are defined, |
| 977 | // we want to add the existing to the list of headers (will be OR'ed) |
| 978 | val = d.Val() |
| 979 | http.Header(*m).Add(field, val) |
| 980 | } |
| 981 | |
| 982 | if d.NextBlock(0) { |
| 983 | return d.Err("malformed header matcher: blocks are not supported") |
| 984 | } |
| 985 | } |
| 986 | return nil |
| 987 | } |
| 988 | |
| 989 | // Match returns true if r matches m. |
| 990 | func (m MatchHeader) Match(r *http.Request) bool { |