| 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 headersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/" |
| 19 | "net/httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/" |
| 20 | "reflectscip-go gomod github.com/golang/go/src go1.25.1 reflect/" |
| 21 | "stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/" |
| 22 | |
| 23 | "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/" |
| 24 | "github.com/caddyserver/caddy/v2/modules/caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/" |
| 25 | ) |
| 26 | |
| 27 | func initscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/init().() { |
| 28 | httpcaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/.RegisterDirectivescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/RegisterDirective().("header", parseCaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/parseCaddyfile().) |
| 29 | httpcaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/.RegisterDirectivescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/RegisterDirective().("request_header", parseReqHdrCaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/parseReqHdrCaddyfile().) |
| 30 | } |
| 31 | |
| 32 | // parseCaddyfile sets up the handler for response headers from |
| 33 | // Caddyfile tokens. Syntax: |
| 34 | // |
| 35 | // header [<matcher>] [[+|-|?|>]<field> [<value|regexp>] [<replacement>]] { |
| 36 | // [+]<field> [<value|regexp> [<replacement>]] |
| 37 | // ?<field> <default_value> |
| 38 | // -<field> |
| 39 | // ><field> |
| 40 | // [defer] |
| 41 | // } |
| 42 | // |
| 43 | // Either a block can be opened or a single header field can be configured |
| 44 | // in the first line, but not both in the same directive. Header operations |
| 45 | // are deferred to write-time if any headers are being deleted or if the |
| 46 | // 'defer' subdirective is used. + appends a header value, - deletes a field, |
| 47 | // ? conditionally sets a value only if the header field is not already set, |
| 48 | // and > sets a field with defer enabled. |
| 49 | func parseCaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/parseCaddyfile().(h httpcaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/.Helperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#) ([]httpcaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/.ConfigValuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#, error) { |
| 50 | h.Nextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Next().() // consume directive name |
| 51 | matcherSet, err := h.ExtractMatcherSetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#ExtractMatcherSet().() |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | h.Nextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Next().() // consume the directive name again (matcher parsing resets) |
| 56 | |
| 57 | makeHandler := func() Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Handler# { |
| 58 | return Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Handler#{ |
| 59 | Responsescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Handler#Response.: &RespHeaderOpsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/RespHeaderOps#{ |
| 60 | HeaderOpsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/RespHeaderOps#HeaderOps.: &HeaderOpsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#{}, |
| 61 | }, |
| 62 | } |
| 63 | } |
| 64 | handler, handlerWithRequire := makeHandler(), makeHandler() |
| 65 | |
| 66 | // first see if headers are in the initial line |
| 67 | var hasArgs bool |
| 68 | if h.NextArgscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextArg().() { |
| 69 | hasArgs = true |
| 70 | field := h.Valscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Val().() |
| 71 | var value string |
| 72 | var replacement *string |
| 73 | if h.NextArgscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextArg().() { |
| 74 | value = h.Valscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Val().() |
| 75 | } |
| 76 | if h.NextArgscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextArg().() { |
| 77 | arg := h.Valscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Val().() |
| 78 | replacement = &arg |
| 79 | } |
| 80 | err := applyHeaderOpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/applyHeaderOp().( |
| 81 | handler.Responsescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Handler#Response..HeaderOpsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/RespHeaderOps#HeaderOps., |
| 82 | handler.Responsescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Handler#Response., |
| 83 | field, |
| 84 | value, |
| 85 | replacement, |
| 86 | ) |
| 87 | if err != nil { |
| 88 | return nil, h.Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Err().(err.Error()) |
| 89 | } |
| 90 | if len(handler.Responsescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Handler#Response..HeaderOpsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/RespHeaderOps#HeaderOps..Deletescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Delete.) > 0 { |
| 91 | handler.Responsescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Handler#Response..Deferredscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/RespHeaderOps#Deferred. = true |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // if not, they should be in a block |
| 96 | for h.NextBlockscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextBlock().(0) { |
| 97 | field := h.Valscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Val().() |
| 98 | if field == "defer" { |
| 99 | handler.Responsescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Handler#Response..Deferredscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/RespHeaderOps#Deferred. = true |
| 100 | continue |
| 101 | } |
| 102 | if field == "match" { |
| 103 | responseMatchers := make(map[string]caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.ResponseMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseMatcher#) |
| 104 | err := caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.ParseNamedResponseMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ParseNamedResponseMatcher().(h.NewFromNextSegmentscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NewFromNextSegment().(), responseMatchers) |
| 105 | if err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | matcher := responseMatchers["match"] |
| 109 | handler.Responsescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Handler#Response..Requirescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/RespHeaderOps#Require. = &matcher |
| 110 | continue |
| 111 | } |
| 112 | if hasArgs { |
| 113 | return nil, h.Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Err().("cannot specify headers in both arguments and block") // because it would be weird |
| 114 | } |
| 115 | |
| 116 | // sometimes it is habitual for users to suffix a field name with a colon, |
| 117 | // as if they were writing a curl command or something; see |
| 118 | // https://caddy.community/t/v2-reverse-proxy-please-add-cors-example-to-the-docs/7349/19 |
| 119 | field = stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.25.1 strings/TrimSuffix().(field, ":") |
| 120 | |
| 121 | var value string |
| 122 | var replacement *string |
| 123 | if h.NextArgscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextArg().() { |
| 124 | value = h.Valscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Val().() |
| 125 | } |
| 126 | if h.NextArgscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextArg().() { |
| 127 | arg := h.Valscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Val().() |
| 128 | replacement = &arg |
| 129 | } |
| 130 | |
| 131 | handlerToUse := handler |
| 132 | if stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasPrefix().(field, "?") { |
| 133 | handlerToUse = handlerWithRequire |
| 134 | } |
| 135 | |
| 136 | err := applyHeaderOpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/applyHeaderOp().( |
| 137 | handlerToUse.Responsescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Handler#Response..HeaderOpsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/RespHeaderOps#HeaderOps., |
| 138 | handlerToUse.Responsescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Handler#Response., |
| 139 | field, |
| 140 | value, |
| 141 | replacement, |
| 142 | ) |
| 143 | if err != nil { |
| 144 | return nil, h.Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Err().(err.Error()) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | var configValues []httpcaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/.ConfigValuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue# |
| 149 | if !reflectscip-go gomod github.com/golang/go/src go1.25.1 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.1 reflect/DeepEqual().(handler, makeHandler()) { |
| 150 | configValues = append(configValues, h.NewRoutescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#NewRoute().(matcherSet, handler)...) |
| 151 | } |
| 152 | if !reflectscip-go gomod github.com/golang/go/src go1.25.1 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.1 reflect/DeepEqual().(handlerWithRequire, makeHandler()) { |
| 153 | configValues = append(configValues, h.NewRoutescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#NewRoute().(matcherSet, handlerWithRequire)...) |
| 154 | } |
| 155 | |
| 156 | return configValues, nil |
| 157 | } |
| 158 | |
| 159 | // parseReqHdrCaddyfile sets up the handler for request headers |
| 160 | // from Caddyfile tokens. Syntax: |
| 161 | // |
| 162 | // request_header [<matcher>] [[+|-]<field> [<value|regexp>] [<replacement>]] |
| 163 | func parseReqHdrCaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/parseReqHdrCaddyfile().(h httpcaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/.Helperscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#) ([]httpcaddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/.ConfigValuescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/ConfigValue#, error) { |
| 164 | h.Nextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Next().() // consume directive name |
| 165 | matcherSet, err := h.ExtractMatcherSetscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#ExtractMatcherSet().() |
| 166 | if err != nil { |
| 167 | return nil, err |
| 168 | } |
| 169 | h.Nextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Next().() // consume the directive name again (matcher parsing resets) |
| 170 | |
| 171 | if !h.NextArgscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextArg().() { |
| 172 | return nil, h.ArgErrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#ArgErr().() |
| 173 | } |
| 174 | field := h.Valscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Val().() |
| 175 | |
| 176 | hdr := Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Handler#{ |
| 177 | Requestscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Handler#Request.: &HeaderOpsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#{}, |
| 178 | } |
| 179 | |
| 180 | // sometimes it is habitual for users to suffix a field name with a colon, |
| 181 | // as if they were writing a curl command or something; see |
| 182 | // https://caddy.community/t/v2-reverse-proxy-please-add-cors-example-to-the-docs/7349/19 |
| 183 | field = stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.25.1 strings/TrimSuffix().(field, ":") |
| 184 | |
| 185 | var value string |
| 186 | var replacement *string |
| 187 | if h.NextArgscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextArg().() { |
| 188 | value = h.Valscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Val().() |
| 189 | } |
| 190 | if h.NextArgscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextArg().() { |
| 191 | arg := h.Valscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Val().() |
| 192 | replacement = &arg |
| 193 | if h.NextArgscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextArg().() { |
| 194 | return nil, h.ArgErrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#ArgErr().() |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if hdr.Requestscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Handler#Request. == nil { |
| 199 | hdr.Requestscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Handler#Request. = new(HeaderOpsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#) |
| 200 | } |
| 201 | if err := CaddyfileHeaderOpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/CaddyfileHeaderOp().(hdr.Requestscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Handler#Request., field, value, replacement); err != nil { |
| 202 | return nil, h.Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#Err().(err.Error()) |
| 203 | } |
| 204 | |
| 205 | configValues := h.NewRoutescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile`/Helper#NewRoute().(matcherSet, hdr) |
| 206 | |
| 207 | if h.NextArgscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#NextArg().() { |
| 208 | return nil, h.ArgErrscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Dispenser#ArgErr().() |
| 209 | } |
| 210 | return configValues, nil |
| 211 | } |
| 212 | |
| 213 | // CaddyfileHeaderOp applies a new header operation according to |
| 214 | // field, value, and replacement. The field can be prefixed with |
| 215 | // "+" or "-" to specify adding or removing; otherwise, the value |
| 216 | // will be set (overriding any previous value). If replacement is |
| 217 | // non-nil, value will be treated as a regular expression which |
| 218 | // will be used to search and then replacement will be used to |
| 219 | // complete the substring replacement; in that case, any + or - |
| 220 | // prefix to field will be ignored. |
| 221 | func CaddyfileHeaderOpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/CaddyfileHeaderOp().(ops *HeaderOpsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#, field, value string, replacement *string) error { |
| 222 | return applyHeaderOpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/applyHeaderOp().(ops, nil, field, value, replacement) |
| 223 | } |
| 224 | |
| 225 | func applyHeaderOpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/applyHeaderOp().(ops *HeaderOpsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#, respHeaderOps *RespHeaderOpsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/RespHeaderOps#, field, value string, replacement *string) error { |
| 226 | switch { |
| 227 | case stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasPrefix().(field, "+"): // append |
| 228 | if ops.Addscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Add. == nil { |
| 229 | ops.Addscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Add. = make(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#) |
| 230 | } |
| 231 | ops.Addscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Add..Addscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Add().(field[1:], value) |
| 232 | |
| 233 | case stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasPrefix().(field, "-"): // delete |
| 234 | ops.Deletescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Delete. = append(ops.Deletescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Delete., field[1:]) |
| 235 | if respHeaderOps != nil { |
| 236 | respHeaderOps.Deferredscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/RespHeaderOps#Deferred. = true |
| 237 | } |
| 238 | |
| 239 | case stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasPrefix().(field, "?"): // default (conditional on not existing) - response headers only |
| 240 | if respHeaderOps == nil { |
| 241 | return fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("%v: the default header modifier ('?') can only be used on response headers; for conditional manipulation of request headers, use matchers", field) |
| 242 | } |
| 243 | if respHeaderOps.Requirescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/RespHeaderOps#Require. == nil { |
| 244 | respHeaderOps.Requirescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/RespHeaderOps#Require. = &caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.ResponseMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseMatcher#{ |
| 245 | Headersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseMatcher#Headers.: make(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#), |
| 246 | } |
| 247 | } |
| 248 | field = stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.25.1 strings/TrimPrefix().(field, "?") |
| 249 | respHeaderOps.Requirescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/RespHeaderOps#Require..Headersscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseMatcher#Headers.[field] = nil |
| 250 | if respHeaderOps.Setscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Set. == nil { |
| 251 | respHeaderOps.Setscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Set. = make(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#) |
| 252 | } |
| 253 | respHeaderOps.Setscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Set..Setscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Set().(field, value) |
| 254 | |
| 255 | case replacement != nil: // replace |
| 256 | // allow defer shortcut for replace syntax |
| 257 | if stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasPrefix().(field, ">") && respHeaderOps != nil { |
| 258 | respHeaderOps.Deferredscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/RespHeaderOps#Deferred. = true |
| 259 | } |
| 260 | if ops.Replacescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Replace. == nil { |
| 261 | ops.Replacescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Replace. = make(map[string][]Replacementscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Replacement#) |
| 262 | } |
| 263 | field = stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.TrimLeftscip-go gomod github.com/golang/go/src go1.25.1 strings/TrimLeft().(field, "+-?>") |
| 264 | ops.Replacescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Replace.[field] = append( |
| 265 | ops.Replacescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Replace.[field], |
| 266 | Replacementscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Replacement#{ |
| 267 | SearchRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Replacement#SearchRegexp.: value, |
| 268 | Replacescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/Replacement#Replace.: *replacement, |
| 269 | }, |
| 270 | ) |
| 271 | |
| 272 | case stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.1 strings/HasPrefix().(field, ">"): // set (overwrite) with defer |
| 273 | if ops.Setscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Set. == nil { |
| 274 | ops.Setscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Set. = make(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#) |
| 275 | } |
| 276 | ops.Setscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Set..Setscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Set().(field[1:], value) |
| 277 | if respHeaderOps != nil { |
| 278 | respHeaderOps.Deferredscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/RespHeaderOps#Deferred. = true |
| 279 | } |
| 280 | |
| 281 | default: // set (overwrite) |
| 282 | if ops.Setscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Set. == nil { |
| 283 | ops.Setscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Set. = make(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#) |
| 284 | } |
| 285 | ops.Setscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/headers`/HeaderOps#Set..Setscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Set().(field, value) |
| 286 | } |
| 287 | |
| 288 | return nil |
| 289 | } |