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

Method ApplyToRequest

modules/caddyhttp/headers/headers.go:344–375  ·  view source on GitHub ↗

ApplyToRequest applies ops to r, specially handling the Host header which the standard library does not include with the header map with all the others. This method mutates r.Host.

(r *http.Request)

Source from the content-addressed store, hash-verified

342// header which the standard library does not include with the
343// header map with all the others. This method mutates r.Host.
344func (ops HeaderOps) ApplyToRequest(r *http.Request) {
345 repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
346
347 // capture the current Host header so we can
348 // reset to it when we're done
349 origHost, hadHost := r.Header["Host"]
350
351 // append r.Host; this way, we know that our value
352 // was last in the list, and if an Add operation
353 // appended something else after it, that's probably
354 // fine because it's weird to have multiple Host
355 // headers anyway and presumably the one they added
356 // is the one they wanted
357 r.Header["Host"] = append(r.Header["Host"], r.Host)
358
359 // apply header operations
360 ops.ApplyTo(r.Header, repl)
361
362 // retrieve the last Host value (likely the one we appended)
363 if len(r.Header["Host"]) > 0 {
364 r.Host = r.Header["Host"][len(r.Header["Host"])-1]
365 } else {
366 r.Host = ""
367 }
368
369 // reset the Host header slice
370 if hadHost {
371 r.Header["Host"] = origHost
372 } else {
373 delete(r.Header, "Host")
374 }
375}
376
377// responseWriterWrapper defers response header
378// operations until WriteHeader is called.

Callers 2

proxyLoopIterationMethod · 0.95
ServeHTTPMethod · 0.80

Calls 2

ApplyToMethod · 0.95
ValueMethod · 0.45

Tested by

no test coverage detected