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

Method ApplyTo

modules/caddyhttp/headers/headers.go:220–339  ·  view source on GitHub ↗

ApplyTo applies ops to hdr using repl.

(hdr http.Header, repl *caddy.Replacer)

Source from the content-addressed store, hash-verified

218
219// ApplyTo applies ops to hdr using repl.
220func (ops *HeaderOps) ApplyTo(hdr http.Header, repl *caddy.Replacer) {
221 if ops == nil {
222 return
223 }
224 // before manipulating headers in other ways, check if there
225 // is configuration to delete all headers, and do that first
226 // because if a header is to be added, we don't want to delete
227 // it also
228 for _, fieldName := range ops.Delete {
229 fieldName = repl.ReplaceKnown(fieldName, "")
230 if fieldName == "*" {
231 clear(hdr)
232 }
233 }
234
235 // add
236 for fieldName, vals := range ops.Add {
237 fieldName = repl.ReplaceKnown(fieldName, "")
238 for _, v := range vals {
239 hdr.Add(fieldName, repl.ReplaceKnown(v, ""))
240 }
241 }
242
243 // set
244 for fieldName, vals := range ops.Set {
245 fieldName = repl.ReplaceKnown(fieldName, "")
246 var newVals []string
247 for i := range vals {
248 // append to new slice so we don't overwrite
249 // the original values in ops.Set
250 newVals = append(newVals, repl.ReplaceKnown(vals[i], ""))
251 }
252 hdr.Set(fieldName, strings.Join(newVals, ","))
253 }
254
255 // delete
256 for _, fieldName := range ops.Delete {
257 fieldName = strings.ToLower(repl.ReplaceKnown(fieldName, ""))
258 if fieldName == "*" {
259 continue // handled above
260 }
261 switch {
262 case strings.HasPrefix(fieldName, "*") && strings.HasSuffix(fieldName, "*"):
263 for existingField := range hdr {
264 if strings.Contains(strings.ToLower(existingField), fieldName[1:len(fieldName)-1]) {
265 delete(hdr, existingField)
266 }
267 }
268 case strings.HasPrefix(fieldName, "*"):
269 for existingField := range hdr {
270 if strings.HasSuffix(strings.ToLower(existingField), fieldName[1:]) {
271 delete(hdr, existingField)
272 }
273 }
274 case strings.HasSuffix(fieldName, "*"):
275 for existingField := range hdr {
276 if strings.HasPrefix(strings.ToLower(existingField), fieldName[:len(fieldName)-1]) {
277 delete(hdr, existingField)

Callers 5

ApplyToRequestMethod · 0.95
initializePushHeadersMethod · 0.80
finalizeResponseMethod · 0.80
ServeHTTPMethod · 0.80
WriteHeaderMethod · 0.80

Calls 6

ReplaceKnownMethod · 0.80
DelMethod · 0.80
ReplaceAllMethod · 0.80
AddMethod · 0.45
SetMethod · 0.45
CompileMethod · 0.45

Tested by

no test coverage detected