From bc299d4c700213855b8a8ad55c6982cda7c569e8 Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Tue, 16 Jan 2024 01:24:19 +0100 Subject: [PATCH 1/3] Set charset=utf-8 in Content-Type header --- handlers/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handlers/root.go b/handlers/root.go index e6ee447..f68f5fd 100644 --- a/handlers/root.go +++ b/handlers/root.go @@ -27,7 +27,7 @@ func Root(resp *types.SpaceAPIResponseV14) func(http.ResponseWriter, *http.Reque } w.Header().Set("Access-Control-Allow-Origin", "*") - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") _, _ = w.Write(response) } } -- 2.44.1 From fa6e6bd1422f24d64dcfd68b2269ab75f4581b4b Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Tue, 16 Jan 2024 01:37:54 +0100 Subject: [PATCH 2/3] Set WWW-Authenticate header directly and change realm to spaceapid for consistency --- handlers/util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handlers/util.go b/handlers/util.go index ffc4801..f29e8cc 100644 --- a/handlers/util.go +++ b/handlers/util.go @@ -19,7 +19,7 @@ func updateEndpointValidator( // Check BasicAuth credentials username, password, ok := r.BasicAuth() if !ok || !util.CheckCredentials(authDB, validCredentials, username, password) { - w.Header().Set("WWW-Authenticate", "Basic realm=\"space-api\"") + w.Header()["WWW-Authenticate"] = []string{"Basic realm=\"spaceapid\""} http.Error(w, "", http.StatusUnauthorized) return []byte{}, errors.New(fmt.Sprintf("Unauthorized request from %s Username: %s Password: %s", r.RemoteAddr, username, password)) } -- 2.44.1 From 2fb2e5f151e8bd4845885fdea099fef311faf4c2 Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Tue, 16 Jan 2024 01:48:48 +0100 Subject: [PATCH 3/3] We don't need to write anything on successful requests actually, 200 is the default --- handlers/sensors.go | 3 --- handlers/state.go | 3 --- 2 files changed, 6 deletions(-) diff --git a/handlers/sensors.go b/handlers/sensors.go index 0c30346..bd881fb 100644 --- a/handlers/sensors.go +++ b/handlers/sensors.go @@ -1,7 +1,6 @@ package handlers import ( - "io" "log" "math" "net/http" @@ -34,7 +33,5 @@ func EnvironmentSensor( // Set SpaceAPI response values resp.Value = newState resp.LastChange = time.Now().Unix() - - _, _ = io.WriteString(w, "Update Successful") } } diff --git a/handlers/state.go b/handlers/state.go index 72384e5..0ec9727 100644 --- a/handlers/state.go +++ b/handlers/state.go @@ -1,7 +1,6 @@ package handlers import ( - "io" "log" "net/http" "strconv" @@ -33,7 +32,5 @@ func StateOpen( // Set SpaceAPI response values resp.Open = newState resp.LastChange = time.Now().Unix() - - _, _ = io.WriteString(w, "Update Successful") } } -- 2.44.1