From b69c5f1250dff8cd00b4778108ff84fc7af6083f Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Fri, 27 Sep 2024 13:37:44 +0200 Subject: [PATCH 1/2] fix error on missing high_drift --- CHANGELOG.md | 3 ++- main.go | 14 ++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05a2b4f..bb6b287 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ -## v2.8.0 - TBD +## v2.7.1 - TBD Changes: +- Fix a server error being returned if the optional `high_drift` parameter is not given. ## v2.7.0 - 2024-09-10 diff --git a/main.go b/main.go index 6b1ebfc..f733d11 100644 --- a/main.go +++ b/main.go @@ -123,15 +123,17 @@ func handlerMetrics(w http.ResponseWriter, r *http.Request) { if t, err := time.ParseDuration(r.URL.Query().Get("duration")); err == nil { d = t } else { - http.Error(w, err.Error(), http.StatusBadRequest) + http.Error(w, "while parsing duration: "+err.Error(), http.StatusBadRequest) return } - if u, err := time.ParseDuration(r.URL.Query().Get("high_drift")); err == nil { - hd = u - } else { - http.Error(w, err.Error(), http.StatusBadRequest) - return + if r.URL.Query().Get("high_drift") != "" { + if u, err := time.ParseDuration(r.URL.Query().Get("high_drift")); err == nil { + hd = u + } else { + http.Error(w, "while parsing high_drift: "+err.Error(), http.StatusBadRequest) + return + } } } From 450b1de86feda08c174e2bbc35962ca6e13eb7b3 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Fri, 27 Sep 2024 13:39:04 +0200 Subject: [PATCH 2/2] do not reparse URL query over and over again --- main.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index f733d11..49763ea 100644 --- a/main.go +++ b/main.go @@ -101,16 +101,17 @@ func handlerMetrics(w http.ResponseWriter, r *http.Request) { hd := ntpHighDrift if ntpSource == "http" { + query := r.URL.Query() for _, i := range []string{"target", "protocol", "duration"} { - if r.URL.Query().Get(i) == "" { + if query.Get(i) == "" { http.Error(w, "Get parameter is empty: "+i, http.StatusBadRequest) return } } - s = r.URL.Query().Get("target") + s = query.Get("target") - if v, err := strconv.ParseInt(r.URL.Query().Get("protocol"), 10, 32); err != nil { + if v, err := strconv.ParseInt(query.Get("protocol"), 10, 32); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } else if v < 2 || v > 4 { @@ -120,15 +121,15 @@ func handlerMetrics(w http.ResponseWriter, r *http.Request) { p = int(v) } - if t, err := time.ParseDuration(r.URL.Query().Get("duration")); err == nil { + if t, err := time.ParseDuration(query.Get("duration")); err == nil { d = t } else { http.Error(w, "while parsing duration: "+err.Error(), http.StatusBadRequest) return } - if r.URL.Query().Get("high_drift") != "" { - if u, err := time.ParseDuration(r.URL.Query().Get("high_drift")); err == nil { + if query.Get("high_drift") != "" { + if u, err := time.ParseDuration(query.Get("high_drift")); err == nil { hd = u } else { http.Error(w, "while parsing high_drift: "+err.Error(), http.StatusBadRequest)