| 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 reverseproxyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/ |
| 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 | "strconvscip-go gomod github.com/golang/go/src go1.25.1 strconv/" |
| 21 | |
| 22 | "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/" |
| 23 | "github.com/caddyserver/caddy/v2/caddyconfig/caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/" |
| 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/reverseproxy`/init().() { |
| 28 | caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.RegisterModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/RegisterModule().(CopyResponseHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHandler#{}) |
| 29 | caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.RegisterModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/RegisterModule().(CopyResponseHeadersHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#{}) |
| 30 | } |
| 31 | |
| 32 | // CopyResponseHandler is a special HTTP handler which may |
| 33 | // only be used within reverse_proxy's handle_response routes, |
| 34 | // to copy the proxy response. EXPERIMENTAL, subject to change. |
| 35 | type CopyResponseHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHandler# struct { |
| 36 | // To write the upstream response's body but with a different |
| 37 | // status code, set this field to the desired status code. |
| 38 | StatusCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHandler#StatusCode. caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.WeakStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString# `json:"status_code,omitempty"` |
| 39 | |
| 40 | ctxscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHandler#ctx. caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context# |
| 41 | } |
| 42 | |
| 43 | // CaddyModule returns the Caddy module information. |
| 44 | func (CopyResponseHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHandler#) CaddyModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHandler#CaddyModule().() caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo# { |
| 45 | return caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#{ |
| 46 | IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#ID.: "http.handlers.copy_response", |
| 47 | Newscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#New.: func() caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Modulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Module# { return new(CopyResponseHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHandler#) }, |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // Provision ensures that h is set up properly before use. |
| 52 | func (h *CopyResponseHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHandler#) Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHandler#Provision().(ctx caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#) error { |
| 53 | h.ctxscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHandler#ctx. = ctx |
| 54 | return nil |
| 55 | } |
| 56 | |
| 57 | // ServeHTTP implements the Handler interface. |
| 58 | func (h CopyResponseHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHandler#) ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHandler#ServeHTTP().(rw httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, req *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#, _ caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#) error { |
| 59 | repl := req.Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().().Valuescip-go gomod github.com/golang/go/src go1.25.1 context/Context#Value().(caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ReplacerCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ReplacerCtxKey.).(*caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Replacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#) |
| 60 | hrc, ok := req.Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().().Valuescip-go gomod github.com/golang/go/src go1.25.1 context/Context#Value().(proxyHandleResponseContextCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/proxyHandleResponseContextCtxKey.).(*handleResponseContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/handleResponseContext#) |
| 61 | |
| 62 | // don't allow this to be used outside of handle_response routes |
| 63 | if !ok { |
| 64 | return caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.Errorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Error().(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusInternalServerErrorscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusInternalServerError., |
| 65 | 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().("cannot use 'copy_response' outside of reverse_proxy's handle_response routes")) |
| 66 | } |
| 67 | |
| 68 | // allow a custom status code to be written; otherwise the |
| 69 | // status code from the upstream response is written |
| 70 | if codeStr := h.StatusCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHandler#StatusCode..Stringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/WeakString#String().(); codeStr != "" { |
| 71 | intVal, err := strconvscip-go gomod github.com/golang/go/src go1.25.1 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.1 strconv/Atoi().(repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(codeStr, "")) |
| 72 | if err != nil { |
| 73 | return caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.Errorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Error().(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusInternalServerErrorscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusInternalServerError., err) |
| 74 | } |
| 75 | hrc.responsescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/handleResponseContext#response..StatusCodescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Response#StatusCode. = intVal |
| 76 | } |
| 77 | |
| 78 | // make sure the reverse_proxy handler doesn't try to call |
| 79 | // finalizeResponse again after we've already done it here. |
| 80 | hrc.isFinalizedscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/handleResponseContext#isFinalized. = true |
| 81 | |
| 82 | // write the response |
| 83 | return hrc.handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/handleResponseContext#handler..finalizeResponsescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/Handler#finalizeResponse().(rw, req, hrc.responsescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/handleResponseContext#response., repl, hrc.startscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/handleResponseContext#start., hrc.loggerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/handleResponseContext#logger.) |
| 84 | } |
| 85 | |
| 86 | // CopyResponseHeadersHandler is a special HTTP handler which may |
| 87 | // only be used within reverse_proxy's handle_response routes, |
| 88 | // to copy headers from the proxy response. EXPERIMENTAL; |
| 89 | // subject to change. |
| 90 | type CopyResponseHeadersHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler# struct { |
| 91 | // A list of header fields to copy from the response. |
| 92 | // Cannot be defined at the same time as Exclude. |
| 93 | Includescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#Include. []string `json:"include,omitempty"` |
| 94 | |
| 95 | // A list of header fields to skip copying from the response. |
| 96 | // Cannot be defined at the same time as Include. |
| 97 | Excludescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#Exclude. []string `json:"exclude,omitempty"` |
| 98 | |
| 99 | includeMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#includeMap. map[string]struct{} |
| 100 | excludeMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#excludeMap. map[string]struct{} |
| 101 | ctxscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#ctx. caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context# |
| 102 | } |
| 103 | |
| 104 | // CaddyModule returns the Caddy module information. |
| 105 | func (CopyResponseHeadersHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#) CaddyModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#CaddyModule().() caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo# { |
| 106 | return caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#{ |
| 107 | IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#ID.: "http.handlers.copy_response_headers", |
| 108 | Newscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#New.: func() caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Modulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Module# { return new(CopyResponseHeadersHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#) }, |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Validate ensures the h's configuration is valid. |
| 113 | func (h *CopyResponseHeadersHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#) Validatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#Validate().() error { |
| 114 | if len(h.Excludescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#Exclude.) > 0 && len(h.Includescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#Include.) > 0 { |
| 115 | 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().("cannot define both 'exclude' and 'include' lists at the same time") |
| 116 | } |
| 117 | |
| 118 | return nil |
| 119 | } |
| 120 | |
| 121 | // Provision ensures that h is set up properly before use. |
| 122 | func (h *CopyResponseHeadersHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#) Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#Provision().(ctx caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#) error { |
| 123 | h.ctxscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#ctx. = ctx |
| 124 | |
| 125 | // Optimize the include list by converting it to a map |
| 126 | if len(h.Includescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#Include.) > 0 { |
| 127 | h.includeMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#includeMap. = map[string]struct{}{} |
| 128 | } |
| 129 | for _, field := range h.Includescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#Include. { |
| 130 | h.includeMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#includeMap.[httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.CanonicalHeaderKeyscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/CanonicalHeaderKey().(field)] = struct{}{} |
| 131 | } |
| 132 | |
| 133 | // Optimize the exclude list by converting it to a map |
| 134 | if len(h.Excludescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#Exclude.) > 0 { |
| 135 | h.excludeMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#excludeMap. = map[string]struct{}{} |
| 136 | } |
| 137 | for _, field := range h.Excludescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#Exclude. { |
| 138 | h.excludeMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#excludeMap.[httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.CanonicalHeaderKeyscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/CanonicalHeaderKey().(field)] = struct{}{} |
| 139 | } |
| 140 | |
| 141 | return nil |
| 142 | } |
| 143 | |
| 144 | // ServeHTTP implements the Handler interface. |
| 145 | func (h CopyResponseHeadersHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#) ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#ServeHTTP().(rw httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, req *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#, next caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#) error { |
| 146 | hrc, ok := req.Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().().Valuescip-go gomod github.com/golang/go/src go1.25.1 context/Context#Value().(proxyHandleResponseContextCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/proxyHandleResponseContextCtxKey.).(*handleResponseContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/handleResponseContext#) |
| 147 | |
| 148 | // don't allow this to be used outside of handle_response routes |
| 149 | if !ok { |
| 150 | return caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.Errorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Error().(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusInternalServerErrorscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusInternalServerError., |
| 151 | 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().("cannot use 'copy_response_headers' outside of reverse_proxy's handle_response routes")) |
| 152 | } |
| 153 | |
| 154 | for field, values := range hrc.responsescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/handleResponseContext#response..Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Response#Header. { |
| 155 | // Check the include list first, skip |
| 156 | // the header if it's _not_ in this list. |
| 157 | if len(h.includeMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#includeMap.) > 0 { |
| 158 | if _, ok := h.includeMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#includeMap.[field]; !ok { |
| 159 | continue |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // Then, check the exclude list, skip |
| 164 | // the header if it _is_ in this list. |
| 165 | if len(h.excludeMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#excludeMap.) > 0 { |
| 166 | if _, ok := h.excludeMapscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#excludeMap.[field]; ok { |
| 167 | continue |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Copy all the values for the header. |
| 172 | for _, value := range values { |
| 173 | rw.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#Header().().Addscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Add().(field, value) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return next.ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#ServeHTTP.(rw, req) |
| 178 | } |
| 179 | |
| 180 | // Interface guards |
| 181 | var ( |
| 182 | _ caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.MiddlewareHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MiddlewareHandler# = (*CopyResponseHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHandler#)(nil) |
| 183 | _ caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.Unmarshalerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Unmarshaler# = (*CopyResponseHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHandler#)(nil) |
| 184 | _ caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Provisionerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Provisioner# = (*CopyResponseHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHandler#)(nil) |
| 185 | |
| 186 | _ caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.MiddlewareHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MiddlewareHandler# = (*CopyResponseHeadersHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#)(nil) |
| 187 | _ caddyfilescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/.Unmarshalerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/caddyconfig/caddyfile`/Unmarshaler# = (*CopyResponseHeadersHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#)(nil) |
| 188 | _ caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Provisionerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Provisioner# = (*CopyResponseHeadersHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#)(nil) |
| 189 | _ caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Validatorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Validator# = (*CopyResponseHeadersHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy`/CopyResponseHeadersHandler#)(nil) |
| 190 | ) |