diff --git a/app/vmagent/main.go b/app/vmagent/main.go index 1f2e28249e..04c6cd8b26 100644 --- a/app/vmagent/main.go +++ b/app/vmagent/main.go @@ -228,7 +228,7 @@ func requestHandler(w http.ResponseWriter, r *http.Request) bool { errMsg := fmt.Sprintf("waiting for scrapes to init, left: %d", rdy) http.Error(w, errMsg, http.StatusTooEarly) } else { - w.Header().Set("Content-Type", "text/plain") + w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.WriteHeader(http.StatusOK) w.Write([]byte("OK")) } diff --git a/app/vmalert/datasource/vm.go b/app/vmalert/datasource/vm.go index bf57a04be7..7e0bc4ce4b 100644 --- a/app/vmalert/datasource/vm.go +++ b/app/vmalert/datasource/vm.go @@ -82,7 +82,7 @@ func (s *VMStorage) Query(ctx context.Context, query string) ([]Metric, error) { if err != nil { return nil, err } - req.Header.Set("Content-Type", "application/json") + req.Header.Set("Content-Type", "application/json; charset=utf-8") if s.basicAuthPass != "" { req.SetBasicAuth(s.basicAuthUser, s.basicAuthPass) } diff --git a/app/vmalert/notifier/alertmanager.go b/app/vmalert/notifier/alertmanager.go index f7dd036151..a24f5f723a 100644 --- a/app/vmalert/notifier/alertmanager.go +++ b/app/vmalert/notifier/alertmanager.go @@ -28,7 +28,7 @@ func (am *AlertManager) Send(ctx context.Context, alerts []Alert) error { if err != nil { return err } - req.Header.Set("Content-Type", "application/json") + req.Header.Set("Content-Type", "application/json; charset=utf-8") req = req.WithContext(ctx) if am.basicAuthPass != "" { req.SetBasicAuth(am.basicAuthUser, am.basicAuthPass) diff --git a/app/vmalert/web.go b/app/vmalert/web.go index dbd90fa9aa..a095356d97 100644 --- a/app/vmalert/web.go +++ b/app/vmalert/web.go @@ -40,7 +40,7 @@ func (rh *requestHandler) handler(w http.ResponseWriter, r *http.Request) bool { httpserver.Errorf(w, r, "error in %q: %s", r.URL.Path, err) return true } - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Write(data) return true case "/api/v1/alerts": @@ -49,7 +49,7 @@ func (rh *requestHandler) handler(w http.ResponseWriter, r *http.Request) bool { httpserver.Errorf(w, r, "error in %q: %s", r.URL.Path, err) return true } - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Write(data) return true case "/-/reload": @@ -67,7 +67,7 @@ func (rh *requestHandler) handler(w http.ResponseWriter, r *http.Request) bool { httpserver.Errorf(w, r, "error in %q: %s", r.URL.Path, err) return true } - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") w.Write(data) return true } diff --git a/app/vmselect/graphite/graphite.go b/app/vmselect/graphite/graphite.go index e28012703c..fe683a0d66 100644 --- a/app/vmselect/graphite/graphite.go +++ b/app/vmselect/graphite/graphite.go @@ -88,9 +88,9 @@ func MetricsFindHandler(startTime time.Time, at *auth.Token, w http.ResponseWrit } paths = deduplicatePaths(paths, delimiter) sortPaths(paths, delimiter) - contentType := "application/json" + contentType := "application/json; charset=utf-8" if jsonp != "" { - contentType = "text/javascript" + contentType = "text/javascript; charset=utf-8" } w.Header().Set("Content-Type", contentType) bw := bufferedwriter.Get(w) @@ -173,9 +173,9 @@ func MetricsExpandHandler(startTime time.Time, at *auth.Token, w http.ResponseWr } m[query] = paths } - contentType := "application/json" + contentType := "application/json; charset=utf-8" if jsonp != "" { - contentType = "text/javascript" + contentType = "text/javascript; charset=utf-8" } w.Header().Set("Content-Type", contentType) if groupByExpr { @@ -225,9 +225,9 @@ func MetricsIndexHandler(startTime time.Time, at *auth.Token, w http.ResponseWri if isPartial && searchutils.GetDenyPartialResponse(r) { return fmt.Errorf("cannot return full response, since some of vmstorage nodes are unavailable") } - contentType := "application/json" + contentType := "application/json; charset=utf-8" if jsonp != "" { - contentType = "text/javascript" + contentType = "text/javascript; charset=utf-8" } w.Header().Set("Content-Type", contentType) bw := bufferedwriter.Get(w) diff --git a/app/vmselect/main.go b/app/vmselect/main.go index d01e2f555e..c2a41ac9c6 100644 --- a/app/vmselect/main.go +++ b/app/vmselect/main.go @@ -339,19 +339,19 @@ func selectHandler(startTime time.Time, w http.ResponseWriter, r *http.Request, case "prometheus/api/v1/rules": // Return dumb placeholder rulesRequests.Inc() - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") fmt.Fprintf(w, "%s", `{"status":"success","data":{"groups":[]}}`) return true case "prometheus/api/v1/alerts": // Return dumb placehloder alertsRequests.Inc() - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") fmt.Fprintf(w, "%s", `{"status":"success","data":{"alerts":[]}}`) return true case "prometheus/api/v1/metadata": // Return dumb placeholder metadataRequests.Inc() - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") fmt.Fprintf(w, "%s", `{"status":"success","data":{}}`) return true default: @@ -378,7 +378,7 @@ func deleteHandler(startTime time.Time, w http.ResponseWriter, r *http.Request, func sendPrometheusError(w http.ResponseWriter, r *http.Request, err error) { logger.Warnf("error in %q: %s", r.RequestURI, err) - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") statusCode := http.StatusUnprocessableEntity var esc *httpserver.ErrorWithStatusCode if errors.As(err, &esc) { diff --git a/app/vmselect/prometheus/prometheus.go b/app/vmselect/prometheus/prometheus.go index 663ae67429..c4edb8f0e5 100644 --- a/app/vmselect/prometheus/prometheus.go +++ b/app/vmselect/prometheus/prometheus.go @@ -96,7 +96,7 @@ func FederateHandler(startTime time.Time, at *auth.Token, w http.ResponseWriter, return fmt.Errorf("cannot return full response, since some of vmstorage nodes are unavailable") } - w.Header().Set("Content-Type", "text/plain") + w.Header().Set("Content-Type", "text/plain; charset=utf-8") bw := bufferedwriter.Get(w) defer bufferedwriter.Put(bw) err = rss.RunParallel(func(rs *netstorage.Result, workerID uint) error { @@ -161,7 +161,7 @@ func ExportCSVHandler(startTime time.Time, at *auth.Token, w http.ResponseWriter MaxTimestamp: end, TagFilterss: tagFilterss, } - w.Header().Set("Content-Type", "text/csv") + w.Header().Set("Content-Type", "text/csv; charset=utf-8") bw := bufferedwriter.Get(w) defer bufferedwriter.Put(bw) @@ -350,9 +350,9 @@ func exportHandler(at *auth.Token, w http.ResponseWriter, r *http.Request, match WriteExportJSONLine(bb, xb) resultsCh <- bb } - contentType := "application/stream+json" + contentType := "application/stream+json; charset=utf-8" if format == "prometheus" { - contentType = "text/plain" + contentType = "text/plain; charset=utf-8" writeLineFunc = func(xb *exportBlock, resultsCh chan<- *quicktemplate.ByteBuffer) { bb := quicktemplate.AcquireByteBuffer() WriteExportPrometheusLine(bb, xb) @@ -631,7 +631,7 @@ func LabelValuesHandler(startTime time.Time, at *auth.Token, labelName string, w return fmt.Errorf("cannot return full response, since some of vmstorage nodes are unavailable") } - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") bw := bufferedwriter.Get(w) defer bufferedwriter.Put(bw) WriteLabelValuesResponse(bw, labelValues) @@ -714,7 +714,7 @@ func LabelsCountHandler(startTime time.Time, at *auth.Token, w http.ResponseWrit if isPartial && searchutils.GetDenyPartialResponse(r) { return fmt.Errorf("cannot return full response, since some of vmstorage nodes are unavailable") } - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") bw := bufferedwriter.Get(w) defer bufferedwriter.Put(bw) WriteLabelsCountResponse(bw, labelEntries) @@ -768,7 +768,7 @@ func TSDBStatusHandler(startTime time.Time, at *auth.Token, w http.ResponseWrite if isPartial && searchutils.GetDenyPartialResponse(r) { return fmt.Errorf("cannot return full response, since some of vmstorage nodes are unavailable") } - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") bw := bufferedwriter.Get(w) defer bufferedwriter.Put(bw) WriteTSDBStatusResponse(bw, status) @@ -842,7 +842,7 @@ func LabelsHandler(startTime time.Time, at *auth.Token, w http.ResponseWriter, r return fmt.Errorf("cannot return full response, since some of vmstorage nodes are unavailable") } - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") bw := bufferedwriter.Get(w) defer bufferedwriter.Put(bw) WriteLabelsResponse(bw, labels) @@ -913,8 +913,7 @@ func SeriesCountHandler(startTime time.Time, at *auth.Token, w http.ResponseWrit if isPartial && searchutils.GetDenyPartialResponse(r) { return fmt.Errorf("cannot return full response, since some of vmstorage nodes are unavailable") } - - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") bw := bufferedwriter.Get(w) defer bufferedwriter.Put(bw) WriteSeriesCountResponse(bw, n) @@ -977,7 +976,7 @@ func SeriesHandler(startTime time.Time, at *auth.Token, w http.ResponseWriter, r return fmt.Errorf("cannot return full response, since some of vmstorage nodes are unavailable") } - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") bw := bufferedwriter.Get(w) defer bufferedwriter.Put(bw) resultsCh := make(chan *quicktemplate.ByteBuffer) @@ -1117,7 +1116,7 @@ func QueryHandler(startTime time.Time, at *auth.Token, w http.ResponseWriter, r } } - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") bw := bufferedwriter.Get(w) defer bufferedwriter.Put(bw) WriteQueryResponse(bw, result) @@ -1219,7 +1218,7 @@ func queryRangeHandler(startTime time.Time, at *auth.Token, w http.ResponseWrite // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/153 result = removeEmptyValuesAndTimeseries(result) - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") bw := bufferedwriter.Get(w) defer bufferedwriter.Put(bw) WriteQueryRangeResponse(bw, result) diff --git a/app/vmstorage/main.go b/app/vmstorage/main.go index 7888d99bb4..a482740095 100644 --- a/app/vmstorage/main.go +++ b/app/vmstorage/main.go @@ -169,7 +169,7 @@ func requestHandler(w http.ResponseWriter, r *http.Request, strg *storage.Storag switch path { case "/create": - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") snapshotPath, err := strg.CreateSnapshot() if err != nil { err = fmt.Errorf("cannot create snapshot: %w", err) @@ -179,7 +179,7 @@ func requestHandler(w http.ResponseWriter, r *http.Request, strg *storage.Storag fmt.Fprintf(w, `{"status":"ok","snapshot":%q}`, snapshotPath) return true case "/list": - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") snapshots, err := strg.ListSnapshots() if err != nil { err = fmt.Errorf("cannot list snapshots: %w", err) @@ -196,7 +196,7 @@ func requestHandler(w http.ResponseWriter, r *http.Request, strg *storage.Storag fmt.Fprintf(w, `]}`) return true case "/delete": - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") snapshotName := r.FormValue("snapshot") if err := strg.DeleteSnapshot(snapshotName); err != nil { err = fmt.Errorf("cannot delete snapshot %q: %w", snapshotName, err) @@ -206,7 +206,7 @@ func requestHandler(w http.ResponseWriter, r *http.Request, strg *storage.Storag fmt.Fprintf(w, `{"status":"ok"}`) return true case "/delete_all": - w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Type", "application/json; charset=utf-8") snapshots, err := strg.ListSnapshots() if err != nil { err = fmt.Errorf("cannot list snapshots: %w", err) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 759deea641..e93b9661a6 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -10,6 +10,8 @@ * FEATURE: add `-search.treatDotsAsIsInRegexps` command-line flag, which can be used for automatic escaping of dots in regexp label filters used in queries. For example, if `-search.treatDotsAsIsInRegexps` is set, then the query `foo{bar=~"aaa.bb.cc|dd.eee"}` is automatically converted to `foo{bar=~"aaa\\.bb\\.cc|dd\\.eee"}`. This may be useful for querying Graphite data. +* FEATURE: consistently return text-based HTTP responses such as `plain/text` and `application/json` with `charset=utf-8`. + See https://github.com/VictoriaMetrics/VictoriaMetrics/pull/897 * BUGFIX: do not return data points in the end of the selected time range for time series ending in the middle of the selected time range. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/887 and https://github.com/VictoriaMetrics/VictoriaMetrics/issues/845 diff --git a/lib/httpserver/httpserver.go b/lib/httpserver/httpserver.go index 6a0efbbefb..0563f24e56 100644 --- a/lib/httpserver/httpserver.go +++ b/lib/httpserver/httpserver.go @@ -204,7 +204,7 @@ func handlerWrapper(s *server, w http.ResponseWriter, r *http.Request, rh Reques r.URL.Path = path switch r.URL.Path { case "/health": - w.Header().Set("Content-Type", "text/plain") + w.Header().Set("Content-Type", "text/plain; charset=utf-8") deadline := atomic.LoadInt64(&s.shutdownDelayDeadline) if deadline <= 0 { w.Write([]byte("OK")) @@ -236,7 +236,7 @@ func handlerWrapper(s *server, w http.ResponseWriter, r *http.Request, rh Reques case "/metrics": metricsRequests.Inc() startTime := time.Now() - w.Header().Set("Content-Type", "text/plain") + w.Header().Set("Content-Type", "text/plain; charset=utf-8") WritePrometheusMetrics(w) metricsHandlerDuration.UpdateDuration(startTime) return @@ -365,7 +365,7 @@ func (zrw *gzipResponseWriter) Write(p []byte) (int, error) { if h.Get("Content-Type") == "" { // Disable auto-detection of content-type, since it // is incorrectly detected after the compression. - h.Set("Content-Type", "text/html") + h.Set("Content-Type", "text/html; charset=utf-8") } } zrw.writeHeader()