From 182fa37e5fd1acea810ef091d3e50f29694f3baa Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Mon, 8 Oct 2018 05:07:02 +0300 Subject: [PATCH] querylog API -- when manually generating json, don't forget to escape strings --- coredns_plugin/querylog_top.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/coredns_plugin/querylog_top.go b/coredns_plugin/querylog_top.go index 82ee1130..bcc12b3a 100644 --- a/coredns_plugin/querylog_top.go +++ b/coredns_plugin/querylog_top.go @@ -270,18 +270,18 @@ func handleStatsTop(w http.ResponseWriter, r *http.Request) { json.WriteString("{\n") gen := func(json *bytes.Buffer, name string, top map[string]int, addComma bool) { - json.WriteString(" \"") - json.WriteString(name) - json.WriteString("\": {\n") + json.WriteString(" ") + json.WriteString(fmt.Sprintf("%q", name)) + json.WriteString(": {\n") sorted := sortByValue(top) + // no more than 50 entries + if len(sorted) > 50 { + sorted = sorted[:50] + } for i, key := range sorted { - // no more than 50 entries - if i >= 50 { - break - } - json.WriteString(" \"") - json.WriteString(key) - json.WriteString("\": ") + json.WriteString(" ") + json.WriteString(fmt.Sprintf("%q", key)) + json.WriteString(": ") json.WriteString(strconv.Itoa(top[key])) if i+1 != len(sorted) { json.WriteByte(',')