Skip to content

Commit

Permalink
Issue #433: Ensure proxy.noroutestatus has three digits
Browse files Browse the repository at this point in the history
go1.10 requires that http.WriteHeader() is called with
a valid status code.

Fixes #433
  • Loading branch information
magiconair committed Feb 2, 2018
1 parent fcc935c commit fe5018d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
7 changes: 6 additions & 1 deletion config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func load(cmdline, environ, envprefix []string, props *properties.Properties) (c
f.IntVar(&cfg.Proxy.MaxConn, "proxy.maxconn", defaultConfig.Proxy.MaxConn, "maximum number of cached connections")
f.StringVar(&cfg.Proxy.Strategy, "proxy.strategy", defaultConfig.Proxy.Strategy, "load balancing strategy")
f.StringVar(&cfg.Proxy.Matcher, "proxy.matcher", defaultConfig.Proxy.Matcher, "path matching algorithm")
f.IntVar(&cfg.Proxy.NoRouteStatus, "proxy.noroutestatus", defaultConfig.Proxy.NoRouteStatus, "status code for invalid route")
f.IntVar(&cfg.Proxy.NoRouteStatus, "proxy.noroutestatus", defaultConfig.Proxy.NoRouteStatus, "status code for invalid route. Must be three digits")
f.DurationVar(&cfg.Proxy.ShutdownWait, "proxy.shutdownwait", defaultConfig.Proxy.ShutdownWait, "time for graceful shutdown")
f.DurationVar(&cfg.Proxy.DialTimeout, "proxy.dialtimeout", defaultConfig.Proxy.DialTimeout, "connection timeout for backend connections")
f.DurationVar(&cfg.Proxy.ResponseHeaderTimeout, "proxy.responseheadertimeout", defaultConfig.Proxy.ResponseHeaderTimeout, "response header timeout")
Expand Down Expand Up @@ -252,6 +252,11 @@ func load(cmdline, environ, envprefix []string, props *properties.Properties) (c
return nil, fmt.Errorf("invalid ui.access: %s", cfg.UI.Access)
}

// go1.10 will not accept a non-three digit status code
if cfg.Proxy.NoRouteStatus < 100 || cfg.Proxy.NoRouteStatus > 999 {
return nil, fmt.Errorf("proxy.noroutestatus must be between 100 and 999")
}

// handle deprecations
deprecate := func(name, msg string) {
if f.IsSet(name) {
Expand Down
12 changes: 12 additions & 0 deletions config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,18 @@ func TestLoad(t *testing.T) {
cfg: func(cfg *Config) *Config { return nil },
err: errors.New("cert source requires proto 'https' or 'tcp'"),
},
{
desc: "-proxy.noroutestatus too small",
args: []string{"-proxy.noroutestatus", "10"},
cfg: func(cfg *Config) *Config { return nil },
err: errors.New("proxy.noroutestatus must be between 100 and 999"),
},
{
desc: "-proxy.noroutestatus too big",
args: []string{"-proxy.noroutestatus", "1000"},
cfg: func(cfg *Config) *Config { return nil },
err: errors.New("proxy.noroutestatus must be between 100 and 999"),
},
{
args: []string{"-cfg"},
cfg: func(cfg *Config) *Config { return nil },
Expand Down
6 changes: 5 additions & 1 deletion proxy/http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ func (p *HTTPProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {

t := p.Lookup(r)
if t == nil {
w.WriteHeader(p.Config.NoRouteStatus)
status := p.Config.NoRouteStatus
if status < 100 || status > 999 {
status = http.StatusNotFound
}
w.WriteHeader(status)
html := noroute.GetHTML()
if html != "" {
io.WriteString(w, html)
Expand Down

0 comments on commit fe5018d

Please sign in to comment.