From 2495ce5808f30550cf38782c5ab09d8c7a56994b Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Mon, 15 Jan 2024 22:20:53 +0100 Subject: [PATCH 1/5] Remove superfluous WriteHeader calls --- handlers/root.go | 2 -- handlers/sensors.go | 2 -- handlers/state.go | 2 -- 3 files changed, 6 deletions(-) diff --git a/handlers/root.go b/handlers/root.go index 1015f49..e6ee447 100644 --- a/handlers/root.go +++ b/handlers/root.go @@ -26,10 +26,8 @@ func Root(resp *types.SpaceAPIResponseV14) func(http.ResponseWriter, *http.Reque return } - // Respond with OK w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) _, _ = w.Write(response) } } diff --git a/handlers/sensors.go b/handlers/sensors.go index 7441699..ec0a6aa 100644 --- a/handlers/sensors.go +++ b/handlers/sensors.go @@ -32,8 +32,6 @@ func EnvironmentSensor( resp.Value = newState resp.LastChange = time.Now().Unix() - // Respond with OK - w.WriteHeader(http.StatusOK) _, _ = io.WriteString(w, "Update Successful") } } diff --git a/handlers/state.go b/handlers/state.go index 47bc6ab..b834072 100644 --- a/handlers/state.go +++ b/handlers/state.go @@ -31,8 +31,6 @@ func StateOpen( resp.Open = newState resp.LastChange = time.Now().Unix() - // Respond with OK - w.WriteHeader(http.StatusOK) _, _ = io.WriteString(w, "Update Successful") } } -- 2.44.1 From 1d30c16e468f5a9df58c3e6a7f76c589ce38622a Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Mon, 15 Jan 2024 22:22:04 +0100 Subject: [PATCH 2/5] More readable return statement --- handlers/util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handlers/util.go b/handlers/util.go index 625a9c0..319c726 100644 --- a/handlers/util.go +++ b/handlers/util.go @@ -41,5 +41,5 @@ func updateEndpointValidator( return } - return + return body } -- 2.44.1 From efbd55aac47f4dc3edaac10151e5d8107076a381 Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Mon, 15 Jan 2024 22:31:29 +0100 Subject: [PATCH 3/5] Improve logging in handlers/sensors.go --- handlers/sensors.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/handlers/sensors.go b/handlers/sensors.go index ec0a6aa..753574b 100644 --- a/handlers/sensors.go +++ b/handlers/sensors.go @@ -17,12 +17,12 @@ func EnvironmentSensor( resp *types.EnvironmentSensor, ) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - body := updateEndpointValidator(authDB, validCredentials, w, r) + body := string(updateEndpointValidator(authDB, validCredentials, w, r)) // Parse request body - newState, err := strconv.ParseFloat(string(body), 64) + newState, err := strconv.ParseFloat(body, 64) if err != nil || math.IsInf(newState, 0) { - log.Println("Failed to parse request body from", r.RemoteAddr) + log.Println("Failed to parse request body from", r.RemoteAddr, "body:", body) w.WriteHeader(http.StatusBadRequest) _, _ = io.WriteString(w, "HTTP request body has to be a valid float64 value != +/-Inf") return -- 2.44.1 From cc9cb8888c020708de6378440c8841fb7d8634c0 Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Mon, 15 Jan 2024 22:31:37 +0100 Subject: [PATCH 4/5] Improve logging in handlers/state.go --- handlers/state.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/handlers/state.go b/handlers/state.go index b834072..5db5d99 100644 --- a/handlers/state.go +++ b/handlers/state.go @@ -16,12 +16,12 @@ func StateOpen( resp *types.SpaceState, ) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - body := updateEndpointValidator(authDB, validCredentials, w, r) + body := string(updateEndpointValidator(authDB, validCredentials, w, r)) // Parse request body - newState, err := strconv.ParseBool(string(body)) + newState, err := strconv.ParseBool(body) if err != nil { - log.Println("Failed to parse request body from", r.RemoteAddr) + log.Println("Failed to parse request body from", r.RemoteAddr, "body:", body) w.WriteHeader(http.StatusBadRequest) _, _ = io.WriteString(w, "HTTP request body should either be true or false") return -- 2.44.1 From 7ac8e91cc2d4523fe286025258ea38e1d94360a3 Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Mon, 15 Jan 2024 22:32:07 +0100 Subject: [PATCH 5/5] Improve logging in handlers/util.go --- handlers/util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handlers/util.go b/handlers/util.go index 319c726..927b6c9 100644 --- a/handlers/util.go +++ b/handlers/util.go @@ -18,7 +18,7 @@ func updateEndpointValidator( // Check BasicAuth credentials username, password, ok := r.BasicAuth() if !ok || !util.CheckCredentials(authDB, validCredentials, username, password) { - log.Println("Unauthorized request from", r.RemoteAddr) + log.Println("Unauthorized request from", r.RemoteAddr, "Username:", username, "Password:", password) w.Header().Set("WWW-Authenticate", "Basic realm=\"space-api\"") w.WriteHeader(http.StatusUnauthorized) return -- 2.44.1