From dfcee1e21dddcc23beb5641dec19ba066b8476f8 Mon Sep 17 00:00:00 2001 From: Mark van der Velden Date: Thu, 27 Apr 2017 20:04:13 +0200 Subject: [PATCH 1/3] Removing the Favicon handler, in favor of a more generic handler --- handlers/favicon.go | 19 ------------------- main.go | 5 +++-- 2 files changed, 3 insertions(+), 21 deletions(-) delete mode 100644 handlers/favicon.go diff --git a/handlers/favicon.go b/handlers/favicon.go deleted file mode 100644 index 4024ee1..0000000 --- a/handlers/favicon.go +++ /dev/null @@ -1,19 +0,0 @@ -package handlers - -import ( - "net/http" - "strings" -) - -func NewIgnoreFaviconRequests() func(h http.Handler) http.Handler { - return func(h http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if strings.HasPrefix(r.RequestURI, "/favicon") { - http.NotFound(w, r) - return - } - - h.ServeHTTP(w, r) - }) - } -} diff --git a/main.go b/main.go index 9b2e2d5..3dfcb3e 100644 --- a/main.go +++ b/main.go @@ -135,10 +135,11 @@ func decorateHandler(l log.Logger, h http.Handler, b *ratelimit.Bucket) http.Han decorators = append( decorators, - // Checking on the route "health", doesn't support path-segment-stripping! handlers.NewHTTPStatusPaths(l, []string{"health", "health/"}, http.StatusOK), - handlers.NewIgnoreFaviconRequests(), + // Ignoring common foo requests + handlers.NewHTTPStatusPaths(l, []string{"/favicon"}, http.StatusNotFound), + handlers.NewRequestLogger(l), handlers.NewRateLimitHandler(l, b), ) From 643b8a5b9cb95ae41ff4f9eccb196bdb13e37cb4 Mon Sep 17 00:00:00 2001 From: Mark van der Velden Date: Thu, 27 Apr 2017 20:04:51 +0200 Subject: [PATCH 2/3] Making sure that requests without `url` parameter don't complain about invalid domains. --- handlers/urlparameters.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handlers/urlparameters.go b/handlers/urlparameters.go index b1c712f..dadcc38 100644 --- a/handlers/urlparameters.go +++ b/handlers/urlparameters.go @@ -16,7 +16,7 @@ func NewValidateURLParameter(l log.Logger, allowedHosts []string) func(h http.Ha return func(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { queryURL := r.URL.Query().Get("url") - if !isValidQueryURL(queryURL, hosts) { + if queryURL != "" && !isValidQueryURL(queryURL, hosts) { l.Log("error", "domain not registered", "QS", r.URL.RawQuery, "URL", queryURL) http.Error(w, "Unregisterd domain", http.StatusNotAcceptable) return From 05fc1f0085518c166ae53b641062b7badef6c4c0 Mon Sep 17 00:00:00 2001 From: Mark van der Velden Date: Thu, 27 Apr 2017 20:05:33 +0200 Subject: [PATCH 3/3] Defining two "health" endpoints, both `/` and `/health`, makes k8s happy --- handlers/ratelimiter.go | 2 +- handlers/statuspaths.go | 7 +++---- main.go | 3 ++- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/handlers/ratelimiter.go b/handlers/ratelimiter.go index 735c394..acfb0bb 100644 --- a/handlers/ratelimiter.go +++ b/handlers/ratelimiter.go @@ -13,7 +13,7 @@ func NewRateLimitHandler(l log.Logger, b *ratelimit.Bucket) func(h http.Handler) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { d := b.Take(1) if d > 0 { - l.Log("msg", "Rate limiting", "delay", d) + l.Log("msg", "Rate limiting", "delay", d, "remote_addr", r.RemoteAddr) time.Sleep(d) } diff --git a/handlers/statuspaths.go b/handlers/statuspaths.go index dc1f434..4c4d47f 100644 --- a/handlers/statuspaths.go +++ b/handlers/statuspaths.go @@ -7,19 +7,18 @@ import ( ) func NewHTTPStatusPaths(_ log.Logger, paths []string, httpStatus int) func(h http.Handler) http.Handler { - var actions = make(map[string]bool, len(paths)) + var pathMap = make(map[string]bool, len(paths)) for _, p := range paths { if p == "" { continue } - actions[p] = true + pathMap[p] = true } return func(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - action := r.URL.Path[1:] - if _, exists := actions[action]; action != "" && exists { + if _, exists := pathMap[r.URL.Path]; exists { w.WriteHeader(httpStatus) return } diff --git a/main.go b/main.go index 3dfcb3e..de96cdf 100644 --- a/main.go +++ b/main.go @@ -135,7 +135,8 @@ func decorateHandler(l log.Logger, h http.Handler, b *ratelimit.Bucket) http.Han decorators = append( decorators, - handlers.NewHTTPStatusPaths(l, []string{"health", "health/"}, http.StatusOK), + // Defining "health" end-points. + handlers.NewHTTPStatusPaths(l, []string{"/health", "/"}, http.StatusOK), // Ignoring common foo requests handlers.NewHTTPStatusPaths(l, []string{"/favicon"}, http.StatusNotFound),