Actually end the connection when request checks fail

This commit is contained in:
Bendodroid 2024-01-15 23:18:34 +01:00
commit cf9678d712
Signed by: bendodroid
GPG key ID: 3EEE19A0F73D5FFC
3 changed files with 26 additions and 23 deletions

View file

@ -17,14 +17,17 @@ func EnvironmentSensor(
resp *types.EnvironmentSensor,
) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
body := string(updateEndpointValidator(authDB, validCredentials, w, r))
body, err := updateEndpointValidator(authDB, validCredentials, w, r)
if err != nil {
log.Println(err)
return
}
// Parse request body
newState, err := strconv.ParseFloat(body, 64)
newState, err := strconv.ParseFloat(string(body), 64)
if err != nil || math.IsInf(newState, 0) {
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")
log.Println("Failed to parse request body from", r.RemoteAddr, "with error:", err)
http.Error(w, "HTTP request body has to be a valid float64 value != +/-Inf", http.StatusBadRequest)
return
}