From de687edb2e641abcb8d0aa46249eaace491d9273 Mon Sep 17 00:00:00 2001 From: Drew Bailey <2614075+drewbailey@users.noreply.github.com> Date: Fri, 27 Mar 2020 09:01:54 -0400 Subject: [PATCH] wrap http.Handlers better comments --- command/agent/http.go | 16 ++++++++-------- command/agent/http_oss.go | 9 +++++++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/command/agent/http.go b/command/agent/http.go index cb31a76a8..bda062315 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -323,14 +323,14 @@ func (s *HTTPServer) registerHandlers(enableDebug bool) { s.mux.HandleFunc("/v1/operator/scheduler/configuration", s.wrap(s.OperatorSchedulerConfiguration)) if uiEnabled { - s.mux.Handle("/ui/", http.StripPrefix("/ui/", handleUI(http.FileServer(&UIAssetWrapper{FileSystem: assetFS()})))) + s.mux.Handle("/ui/", http.StripPrefix("/ui/", s.handleUI(http.FileServer(&UIAssetWrapper{FileSystem: assetFS()})))) } else { // Write the stubHTML s.mux.HandleFunc("/ui/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(stubHTML)) }) } - s.mux.Handle("/", handleRootFallthrough()) + s.mux.Handle("/", s.handleRootFallthrough()) if enableDebug { if !s.agent.config.DevMode { @@ -386,23 +386,23 @@ func (e *codedError) Code() int { return e.code } -func handleUI(h http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { +func (s *HTTPServer) handleUI(h http.Handler) http.Handler { + return s.auditHTTPHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { header := w.Header() header.Add("Content-Security-Policy", "default-src 'none'; connect-src *; img-src 'self' data:; script-src 'self'; style-src 'self' 'unsafe-inline'; form-action 'none'; frame-ancestors 'none'") h.ServeHTTP(w, req) return - }) + })) } -func handleRootFallthrough() http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { +func (s *HTTPServer) handleRootFallthrough() http.Handler { + return s.auditHTTPHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { if req.URL.Path == "/" { http.Redirect(w, req, "/ui/", 307) } else { w.WriteHeader(http.StatusNotFound) } - }) + })) } func errCodeFromHandler(err error) (int, string) { diff --git a/command/agent/http_oss.go b/command/agent/http_oss.go index b99fe79c6..a323bda1b 100644 --- a/command/agent/http_oss.go +++ b/command/agent/http_oss.go @@ -2,7 +2,9 @@ package agent -import "net/http" +import ( + "net/http" +) // registerEnterpriseHandlers is a no-op for the oss release func (s *HTTPServer) registerEnterpriseHandlers() { @@ -23,14 +25,17 @@ func (s *HTTPServer) entOnly(resp http.ResponseWriter, req *http.Request) (inter return nil, CodedError(501, ErrEntOnly) } -func (s HTTPServer) auditHandler(h handlerFn) handlerFn { +// auditHandler wraps the passed handlerFn +func (s *HTTPServer) auditHandler(h handlerFn) handlerFn { return h } +// auditHTTPHandler wraps the passed handlerByteFn func (s *HTTPServer) auditNonJSONHandler(h handlerByteFn) handlerByteFn { return h } +// auditHTTPHandler wraps the passed http.Handler func (s *HTTPServer) auditHTTPHandler(h http.Handler) http.Handler { return h }