@@ -18,7 +18,6 @@ import (
1818 "encoding/json"
1919 "fmt"
2020 "io"
21- "log"
2221 "net/http"
2322 "net/http/httputil"
2423 "net/url"
@@ -50,10 +49,12 @@ type routes struct {
5049}
5150
5251type options struct {
53- enableLabelAPIs bool
54- passthroughPaths []string
55- errorOnReplace bool
56- registerer prometheus.Registerer
52+ enableLabelAPIs bool
53+ passthroughPaths []string
54+ errorOnReplace bool
55+ registerer prometheus.Registerer
56+ extraHttpHeaders map [string ]string
57+ rewriteHostHeader string
5758}
5859
5960type Option interface {
@@ -97,6 +98,24 @@ func WithErrorOnReplace() Option {
9798 })
9899}
99100
101+ func WithExtraHttpHeaders (headers []string ) Option {
102+ return optionFunc (func (o * options ) {
103+ o .extraHttpHeaders = make (map [string ]string )
104+ for _ , headerArg := range headers {
105+ header , val , found := strings .Cut (headerArg , ":" )
106+ if found {
107+ o .extraHttpHeaders [strings .TrimSpace (header )] = strings .TrimSpace (val )
108+ }
109+ }
110+ })
111+ }
112+
113+ func WithRewriteHostHeader (host string ) Option {
114+ return optionFunc (func (o * options ) {
115+ o .rewriteHostHeader = host
116+ })
117+ }
118+
100119// mux abstracts away the behavior we expect from the http.ServeMux type in this package.
101120type mux interface {
102121 http.Handler
@@ -262,7 +281,8 @@ func (sle StaticLabelEnforcer) ExtractLabel(next http.HandlerFunc) http.Handler
262281 })
263282}
264283
265- func NewRoutes (upstream * url.URL , label string , extractLabeler ExtractLabeler , extraHttpHeaders []string , rewriteHostHeader string , opts ... Option ) (* routes , error ) {
284+ func NewRoutes (upstream * url.URL , label string , extractLabeler ExtractLabeler , opts ... Option ) (* routes , error ) {
285+ //extraHttpHeaders []string, rewriteHostHeader string
266286 opt := options {}
267287 for _ , o := range opts {
268288 o .apply (& opt )
@@ -275,17 +295,12 @@ func NewRoutes(upstream *url.URL, label string, extractLabeler ExtractLabeler, e
275295 proxy := & httputil.ReverseProxy {
276296 Rewrite : func (r * httputil.ProxyRequest ) {
277297 r .SetURL (upstream )
278- if len (strings . TrimSpace ( rewriteHostHeader ) ) == 0 {
298+ if len (opt . rewriteHostHeader ) == 0 {
279299 r .Out .Host = r .In .Host
280300 } else {
281- r .Out .Host = strings . TrimSpace ( rewriteHostHeader )
301+ r .Out .Host = opt . rewriteHostHeader
282302 }
283- for _ , headerArg := range extraHttpHeaders {
284- header , val , found := strings .Cut (headerArg , ":" )
285- if ! found {
286- log .Printf ("Header %s specified but ':' delimited not found" , headerArg )
287- continue
288- }
303+ for header , val := range opt .extraHttpHeaders {
289304 r .Out .Header [strings .TrimSpace (header )] = []string {strings .TrimSpace (val )}
290305 }
291306 },
0 commit comments