Skip to content

Commit bde4952

Browse files
authored
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.
1 parent 0ef761c commit bde4952

File tree

12 files changed

+13
-2
lines changed

12 files changed

+13
-2
lines changed

cmd/proxy/actions/app.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ func App(logger *log.Logger, conf *config.Config) (http.Handler, error) {
5252
SSLRedirect: conf.ForceSSL,
5353
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
5454
}).Handler,
55-
mw.ContentType,
5655
)
5756

5857
var subRouter *mux.Router

cmd/proxy/actions/catalog.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func catalogHandler(s storage.Backend) http.HandlerFunc {
2323
const op errors.Op = "actions.CatalogHandler"
2424
cs, isCataloger := s.(storage.Cataloger)
2525
f := func(w http.ResponseWriter, r *http.Request) {
26+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
2627
if !isCataloger {
2728
w.WriteHeader(errors.KindNotImplemented)
2829
return

cmd/proxy/actions/health.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ import (
55
)
66

77
func healthHandler(w http.ResponseWriter, r *http.Request) {
8+
w.Header().Set("Content-Type", "application/json")
89
w.WriteHeader(http.StatusOK)
910
}

cmd/proxy/actions/home.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func proxyHomeHandler(c *config.Config) http.HandlerFunc {
125125
w.WriteHeader(http.StatusInternalServerError)
126126
}
127127

128-
w.Header().Add("Content-Type", "text/html")
128+
w.Header().Set("Content-Type", "text/html")
129129
w.WriteHeader(http.StatusOK)
130130

131131
err = tmp.ExecuteTemplate(w, "home", templateData)

cmd/proxy/actions/index.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ func indexHandler(index index.Indexer) http.HandlerFunc {
2323
http.Error(w, err.Error(), errors.Kind(err))
2424
return
2525
}
26+
27+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
2628
enc := json.NewEncoder(w)
2729
for _, meta := range list {
2830
if err = enc.Encode(meta); err != nil {

cmd/proxy/actions/readiness.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
func getReadinessHandler(s storage.Backend) http.HandlerFunc {
1010
return func(w http.ResponseWriter, r *http.Request) {
1111
if _, err := s.List(r.Context(), "github.com/gomods/athens"); err != nil {
12+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
1213
w.WriteHeader(http.StatusInternalServerError)
1314
}
1415
}

cmd/proxy/actions/robots.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
// robotsHandler implements GET baseURL/robots.txt.
1010
func robotsHandler(c *config.Config) http.HandlerFunc {
1111
return func(w http.ResponseWriter, r *http.Request) {
12+
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
1213
http.ServeFile(w, r, c.RobotsFile)
1314
}
1415
}

cmd/proxy/actions/version.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ import (
99

1010
func versionHandler(w http.ResponseWriter, r *http.Request) {
1111
_ = json.NewEncoder(w).Encode(build.Data())
12+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
13+
w.WriteHeader(http.StatusOK)
1214
}

pkg/download/latest.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const PathLatest = "/{module:.+}/@latest"
1717
func LatestHandler(dp Protocol, lggr log.Entry, df *mode.DownloadFile) http.Handler {
1818
const op errors.Op = "download.LatestHandler"
1919
f := func(w http.ResponseWriter, r *http.Request) {
20+
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
2021
mod, err := paths.GetModule(r)
2122
if err != nil {
2223
lggr.SystemErr(errors.E(op, err))

pkg/download/list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const PathList = "/{module:.+}/@v/list"
1818
func ListHandler(dp Protocol, lggr log.Entry, df *mode.DownloadFile) http.Handler {
1919
const op errors.Op = "download.ListHandler"
2020
f := func(w http.ResponseWriter, r *http.Request) {
21+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
2122
mod, err := paths.GetModule(r)
2223
if err != nil {
2324
lggr.SystemErr(errors.E(op, err))

0 commit comments

Comments
 (0)