84
84
. option (
85
85
"--custom-header <header>" ,
86
86
"Custom header to add to proxied requests. Use same option for multiple headers (--custom-header k1:v1 --custom-header k2:v2)" ,
87
- collect , [ ]
87
+ collectHeadersIntoObject , { }
88
88
)
89
89
. option ( "--insecure" , "Disable SSL cert verification" )
90
90
. option ( "--host-routing" , "Use host routing (host as first level of path)" )
@@ -107,8 +107,15 @@ args
107
107
"Define an external storage class. Defaults to in-MemoryStore."
108
108
) ;
109
109
110
- function collect ( value , previous ) {
111
- return previous . concat ( [ value ] ) ;
110
+ // collects multiple flags to an object
111
+ // --custom-header "k1:v1" --custom-header " k2 : v2 " --> {"k1":"v1","k2":"v2"}
112
+ function collectHeadersIntoObject ( value , previous ) {
113
+ var headerParts = value . split ( ":" ) . map ( p => p . trim ( ) )
114
+ if ( headerParts . length != 2 ) {
115
+ log . error ( "A single colon was expected in custom header: " + value ) ;
116
+ process . exit ( 1 ) ;
117
+ }
118
+ previous [ headerParts [ 0 ] ] = headerParts [ 1 ]
112
119
}
113
120
114
121
args . parse ( process . argv ) ;
@@ -263,6 +270,7 @@ options.hostRouting = args.hostRouting;
263
270
options . authToken = process . env . CONFIGPROXY_AUTH_TOKEN ;
264
271
options . redirectPort = args . redirectPort ;
265
272
options . redirectTo = args . redirectTo ;
273
+ options . headers = args . customHeader ;
266
274
options . timeout = args . timeout ;
267
275
options . proxyTimeout = args . proxyTimeout ;
268
276
@@ -309,24 +317,6 @@ if (!options.authToken) {
309
317
log . warn ( "REST API is not authenticated." ) ;
310
318
}
311
319
312
- // custom headers option
313
- options . customHeader = [ ] ;
314
- if ( args . customHeader ) {
315
- options . customHeader = args . customHeader . map ( s => s . trim ( ) ) . map ( header => {
316
- var i = header . indexOf ( ':' ) ;
317
- var key , value ;
318
- if ( i < 0 ) {
319
- log . error ( "Custom header is invalid: " + header ) ;
320
- process . exit ( 1 ) ;
321
- }
322
- else {
323
- var key = header . substr ( 0 , i ) . trim ( ) ;
324
- var value = header . substr ( i + 1 ) . trim ( ) ;
325
- }
326
- return { 'key' : key , 'value' : value } ;
327
- } ) . filter ( header => header . key ) ;
328
- }
329
-
330
320
// external backend class
331
321
options . storageBackend = args . storageBackend ;
332
322
0 commit comments