From bde4952614928964b02617a404df4f4c46bc9b62 Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 2 Jun 2024 12:49:36 -0700 Subject: [PATCH] Set correct content type and send once (#1965) Set correct Content-Type headers on each endpoint rather than on the router. The router would, at times, send two Content-Type headers and other times just send the wrong one. --- cmd/proxy/actions/app.go | 1 - cmd/proxy/actions/catalog.go | 1 + cmd/proxy/actions/health.go | 1 + cmd/proxy/actions/home.go | 2 +- cmd/proxy/actions/index.go | 2 ++ cmd/proxy/actions/readiness.go | 1 + cmd/proxy/actions/robots.go | 1 + cmd/proxy/actions/version.go | 2 ++ pkg/download/latest.go | 1 + pkg/download/list.go | 1 + pkg/download/version_info.go | 1 + pkg/download/version_module.go | 1 + 12 files changed, 13 insertions(+), 2 deletions(-) diff --git a/cmd/proxy/actions/app.go b/cmd/proxy/actions/app.go index 74df44abf..acc686710 100644 --- a/cmd/proxy/actions/app.go +++ b/cmd/proxy/actions/app.go @@ -52,7 +52,6 @@ func App(logger *log.Logger, conf *config.Config) (http.Handler, error) { SSLRedirect: conf.ForceSSL, SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, }).Handler, - mw.ContentType, ) var subRouter *mux.Router diff --git a/cmd/proxy/actions/catalog.go b/cmd/proxy/actions/catalog.go index 769208f1e..96847b7c6 100644 --- a/cmd/proxy/actions/catalog.go +++ b/cmd/proxy/actions/catalog.go @@ -23,6 +23,7 @@ func catalogHandler(s storage.Backend) http.HandlerFunc { const op errors.Op = "actions.CatalogHandler" cs, isCataloger := s.(storage.Cataloger) f := func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") if !isCataloger { w.WriteHeader(errors.KindNotImplemented) return diff --git a/cmd/proxy/actions/health.go b/cmd/proxy/actions/health.go index 8c34b33ca..53fc5f25e 100644 --- a/cmd/proxy/actions/health.go +++ b/cmd/proxy/actions/health.go @@ -5,5 +5,6 @@ import ( ) func healthHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) } diff --git a/cmd/proxy/actions/home.go b/cmd/proxy/actions/home.go index 8f41b979e..f73815444 100644 --- a/cmd/proxy/actions/home.go +++ b/cmd/proxy/actions/home.go @@ -125,7 +125,7 @@ func proxyHomeHandler(c *config.Config) http.HandlerFunc { w.WriteHeader(http.StatusInternalServerError) } - w.Header().Add("Content-Type", "text/html") + w.Header().Set("Content-Type", "text/html") w.WriteHeader(http.StatusOK) err = tmp.ExecuteTemplate(w, "home", templateData) diff --git a/cmd/proxy/actions/index.go b/cmd/proxy/actions/index.go index e747a7abb..1e4744e8d 100644 --- a/cmd/proxy/actions/index.go +++ b/cmd/proxy/actions/index.go @@ -23,6 +23,8 @@ func indexHandler(index index.Indexer) http.HandlerFunc { http.Error(w, err.Error(), errors.Kind(err)) return } + + w.Header().Set("Content-Type", "application/json; charset=utf-8") enc := json.NewEncoder(w) for _, meta := range list { if err = enc.Encode(meta); err != nil { diff --git a/cmd/proxy/actions/readiness.go b/cmd/proxy/actions/readiness.go index 48a900d53..515a603bf 100644 --- a/cmd/proxy/actions/readiness.go +++ b/cmd/proxy/actions/readiness.go @@ -9,6 +9,7 @@ import ( func getReadinessHandler(s storage.Backend) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if _, err := s.List(r.Context(), "github.com/gomods/athens"); err != nil { + w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(http.StatusInternalServerError) } } diff --git a/cmd/proxy/actions/robots.go b/cmd/proxy/actions/robots.go index b7edb1358..67ad8e3cf 100644 --- a/cmd/proxy/actions/robots.go +++ b/cmd/proxy/actions/robots.go @@ -9,6 +9,7 @@ import ( // robotsHandler implements GET baseURL/robots.txt. func robotsHandler(c *config.Config) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain; charset=utf-8") http.ServeFile(w, r, c.RobotsFile) } } diff --git a/cmd/proxy/actions/version.go b/cmd/proxy/actions/version.go index a0cba0120..7ca732203 100644 --- a/cmd/proxy/actions/version.go +++ b/cmd/proxy/actions/version.go @@ -9,4 +9,6 @@ import ( func versionHandler(w http.ResponseWriter, r *http.Request) { _ = json.NewEncoder(w).Encode(build.Data()) + w.Header().Set("Content-Type", "application/json; charset=utf-8") + w.WriteHeader(http.StatusOK) } diff --git a/pkg/download/latest.go b/pkg/download/latest.go index 6e5aad194..357f00054 100644 --- a/pkg/download/latest.go +++ b/pkg/download/latest.go @@ -17,6 +17,7 @@ const PathLatest = "/{module:.+}/@latest" func LatestHandler(dp Protocol, lggr log.Entry, df *mode.DownloadFile) http.Handler { const op errors.Op = "download.LatestHandler" f := func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain; charset=utf-8") mod, err := paths.GetModule(r) if err != nil { lggr.SystemErr(errors.E(op, err)) diff --git a/pkg/download/list.go b/pkg/download/list.go index 405fbca47..dd94e8c92 100644 --- a/pkg/download/list.go +++ b/pkg/download/list.go @@ -18,6 +18,7 @@ const PathList = "/{module:.+}/@v/list" func ListHandler(dp Protocol, lggr log.Entry, df *mode.DownloadFile) http.Handler { const op errors.Op = "download.ListHandler" f := func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") mod, err := paths.GetModule(r) if err != nil { lggr.SystemErr(errors.E(op, err)) diff --git a/pkg/download/version_info.go b/pkg/download/version_info.go index 3a6c96035..d68adb523 100644 --- a/pkg/download/version_info.go +++ b/pkg/download/version_info.go @@ -15,6 +15,7 @@ const PathVersionInfo = "/{module:.+}/@v/{version}.info" func InfoHandler(dp Protocol, lggr log.Entry, df *mode.DownloadFile) http.Handler { const op errors.Op = "download.InfoHandler" f := func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") mod, ver, err := getModuleParams(r, op) if err != nil { lggr.SystemErr(err) diff --git a/pkg/download/version_module.go b/pkg/download/version_module.go index 1a1002308..08b423da7 100644 --- a/pkg/download/version_module.go +++ b/pkg/download/version_module.go @@ -15,6 +15,7 @@ const PathVersionModule = "/{module:.+}/@v/{version}.mod" func ModuleHandler(dp Protocol, lggr log.Entry, df *mode.DownloadFile) http.Handler { const op errors.Op = "download.VersionModuleHandler" f := func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/plain; charset=utf-8") mod, ver, err := getModuleParams(r, op) if err != nil { err = errors.E(op, errors.M(mod), errors.V(ver), err)