From 672c770dbe9ebc22b6a7ab886f9a42ea48f6484d Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Fri, 30 Mar 2018 21:42:36 +0000 Subject: [PATCH] Rework /report handler to pull format from URL Otherwise test 150 would fail - Gorilla mux was not handling a newline in the format parameter. This way you also get a better error if you have an error in the parameter - instead of a 404. --- prog/weaver/http.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/prog/weaver/http.go b/prog/weaver/http.go index 14be735b..7803eae8 100644 --- a/prog/weaver/http.go +++ b/prog/weaver/http.go @@ -329,7 +329,7 @@ func HandleHTTP(muxRouter *mux.Router, version string, router *weave.NetworkRout w.Write(json) }) - muxRouter.Methods("GET").Path("/report").Queries("format", "{format}").HandlerFunc( + muxRouter.Methods("GET").Path("/report").HandlerFunc( func(w http.ResponseWriter, r *http.Request) { funcs := template.FuncMap{ "json": func(v interface{}) string { @@ -337,7 +337,12 @@ func HandleHTTP(muxRouter *mux.Router, version string, router *weave.NetworkRout return string(a) }, } - formatTemplate, err := template.New("format").Funcs(funcs).Parse(mux.Vars(r)["format"]) + format, ok := r.URL.Query()["format"] + if !ok || len(format) != 1 { + http.Error(w, "error: must supply one format parameter", http.StatusBadRequest) + return + } + formatTemplate, err := template.New("format").Funcs(funcs).Parse(format[0]) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return