Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ var opts struct {
Listen string `short:"l" long:"listen" env:"LISTEN" description:"listen on host:port (default: 0.0.0.0:8080/8443 under docker, 127.0.0.1:80/443 without)"`
MaxSize string `short:"m" long:"max" env:"MAX_SIZE" default:"64K" description:"max request size"`
GzipEnabled bool `short:"g" long:"gzip" env:"GZIP" description:"enable gz compression"`
ProxyHeaders []string `short:"x" long:"header" env:"HEADER" description:"proxy headers" env-delim:","`
LBType string `long:"lb-type" env:"LB_TYPE" description:"load balancer type" choice:"random" choice:"failover" default:"random"` //nolint
ProxyHeaders []string `short:"x" long:"header" description:"proxy headers"` // env HEADER split in code to allow , inside ""

LBType string `long:"lb-type" env:"LB_TYPE" description:"load balancer type" choice:"random" choice:"failover" default:"random"` //nolint

SSL struct {
Type string `long:"type" env:"TYPE" description:"ssl (auto) support" choice:"none" choice:"static" choice:"auto" default:"none"` //nolint
Expand Down Expand Up @@ -221,6 +222,11 @@ func run() error {
return fmt.Errorf("failed to convert MaxSize: %w", err)
}

proxyHeaders := opts.ProxyHeaders
if len(proxyHeaders) == 0 {
proxyHeaders = splitAtCommas(os.Getenv("HEADER")) // env value may have comma inside "", parsed separately
}

px := &proxy.Http{
Version: revision,
Matcher: svc,
Expand All @@ -232,7 +238,7 @@ func run() error {
CacheControl: cacheControl,
GzEnabled: opts.GzipEnabled,
SSLConfig: sslConfig,
ProxyHeaders: opts.ProxyHeaders,
ProxyHeaders: proxyHeaders,
AccessLog: accessLog,
StdOutEnabled: opts.Logger.StdOut,
Signature: opts.Signature,
Expand Down Expand Up @@ -479,6 +485,35 @@ func sizeParse(inp string) (uint64, error) {
return strconv.ParseUint(inp, 10, 64)
}

// splitAtCommas split s at commas, ignoring commas in strings.
// based on https://stackoverflow.com/a/59318708
func splitAtCommas(s string) []string {
var res []string
var beg int
var inString bool

for i := 0; i < len(s); i++ {
if s[i] == ',' && !inString {
res = append(res, strings.TrimSpace(s[beg:i]))
beg = i + 1
continue
}

if s[i] == '"' {
if !inString {
inString = true
} else if i > 0 && s[i-1] != '\\' {
inString = false
}
}
}
res = append(res, strings.TrimSpace(s[beg:]))
if len(res) == 1 && res[0] == "" {
return []string{}
}
return res
}

type nopWriteCloser struct{ io.Writer }

func (n nopWriteCloser) Close() error { return nil }
Expand Down
24 changes: 24 additions & 0 deletions app/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,27 @@ func (h *TestPlugin) ErrorThing(req lib.Request, res *lib.Response) (err error)
res.StatusCode = 200
return nil
}

func Test_splitAtCommas(t *testing.T) {

tbl := []struct {
inp string
res []string
}{
{"a string", []string{"a string"}},
{"vv1, vv2, vv3", []string{"vv1", "vv2", "vv3"}},
{`"vv1, blah", vv2, vv3`, []string{"\"vv1, blah\"", "vv2", "vv3"}},
{
`Access-Control-Allow-Headers:"DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type",header123:val, foo:"bar1,bar2"`,
[]string{"Access-Control-Allow-Headers:\"DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type\"", "header123:val", "foo:\"bar1,bar2\""},
},
{"", []string{}},
}

for i, tt := range tbl {
t.Run(strconv.Itoa(i), func(t *testing.T) {
assert.Equal(t, tt.res, splitAtCommas(tt.inp))
})
}

}