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)
| 342 | // header which the standard library does not include with the |
| 343 | // header map with all the others. This method mutates r.Host. |
| 344 | func (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. |
no test coverage detected