@@ -18,7 +18,6 @@ import (
18
18
"encoding/json"
19
19
"fmt"
20
20
"io"
21
- "log"
22
21
"net/http"
23
22
"net/http/httputil"
24
23
"net/url"
@@ -50,10 +49,12 @@ type routes struct {
50
49
}
51
50
52
51
type 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
57
58
}
58
59
59
60
type Option interface {
@@ -97,6 +98,24 @@ func WithErrorOnReplace() Option {
97
98
})
98
99
}
99
100
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
+
100
119
// mux abstracts away the behavior we expect from the http.ServeMux type in this package.
101
120
type mux interface {
102
121
http.Handler
@@ -262,7 +281,8 @@ func (sle StaticLabelEnforcer) ExtractLabel(next http.HandlerFunc) http.Handler
262
281
})
263
282
}
264
283
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
266
286
opt := options {}
267
287
for _ , o := range opts {
268
288
o .apply (& opt )
@@ -275,17 +295,12 @@ func NewRoutes(upstream *url.URL, label string, extractLabeler ExtractLabeler, e
275
295
proxy := & httputil.ReverseProxy {
276
296
Rewrite : func (r * httputil.ProxyRequest ) {
277
297
r .SetURL (upstream )
278
- if len (strings . TrimSpace ( rewriteHostHeader ) ) == 0 {
298
+ if len (opt . rewriteHostHeader ) == 0 {
279
299
r .Out .Host = r .In .Host
280
300
} else {
281
- r .Out .Host = strings . TrimSpace ( rewriteHostHeader )
301
+ r .Out .Host = opt . rewriteHostHeader
282
302
}
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 {
289
304
r .Out .Header [strings .TrimSpace (header )] = []string {strings .TrimSpace (val )}
290
305
}
291
306
},
0 commit comments