diff --git a/render/html.go b/render/html.go index 139a8aea..d7a0b898 100644 --- a/render/html.go +++ b/render/html.go @@ -21,14 +21,14 @@ type ( ) func (html HTMLRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { - WriteHeader(w, code, "text/html") + writeHeader(w, code, "text/html; charset=utf-8") file := data[0].(string) args := data[1] return html.Template.ExecuteTemplate(w, file, args) } func (r *HTMLDebugRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { - WriteHeader(w, code, "text/html") + writeHeader(w, code, "text/html; charset=utf-8") file := data[0].(string) obj := data[1] @@ -57,7 +57,7 @@ func (_ htmlPlainRender) Render(w http.ResponseWriter, code int, data ...interfa } func WriteHTMLString(w http.ResponseWriter, code int, format string, values []interface{}) { - WriteHeader(w, code, "text/html") + writeHeader(w, code, "text/html; charset=utf-8") if len(values) > 0 { fmt.Fprintf(w, format, values...) } else { diff --git a/render/json.go b/render/json.go index a6bab248..5cd1fe74 100644 --- a/render/json.go +++ b/render/json.go @@ -16,16 +16,20 @@ func (_ jsonRender) Render(w http.ResponseWriter, code int, data ...interface{}) } func (_ indentedJSON) Render(w http.ResponseWriter, code int, data ...interface{}) error { - WriteHeader(w, code, "application/json") - jsonData, err := json.MarshalIndent(data[0], "", " ") + return WriteIndentedJSON(w, code, data[0]) +} + +func WriteJSON(w http.ResponseWriter, code int, data interface{}) error { + writeHeader(w, code, "application/json; charset=utf-8") + return json.NewEncoder(w).Encode(data) +} + +func WriteIndentedJSON(w http.ResponseWriter, code int, data interface{}) error { + writeHeader(w, code, "application/json; charset=utf-8") + jsonData, err := json.MarshalIndent(data, "", " ") if err != nil { return err } _, err = w.Write(jsonData) return err } - -func WriteJSON(w http.ResponseWriter, code int, data interface{}) error { - WriteHeader(w, code, "application/json") - return json.NewEncoder(w).Encode(data) -} diff --git a/render/render.go b/render/render.go index 694f4007..e80958b3 100644 --- a/render/render.go +++ b/render/render.go @@ -22,28 +22,7 @@ var ( _ Render = &HTMLDebugRender{} ) -func WriteHeader(w http.ResponseWriter, code int, contentType string) { - contentType = joinStrings(contentType, "; charset=utf-8") +func writeHeader(w http.ResponseWriter, code int, contentType string) { w.Header().Set("Content-Type", contentType) w.WriteHeader(code) } - -func joinStrings(a ...string) string { - if len(a) == 0 { - return "" - } - if len(a) == 1 { - return a[0] - } - n := 0 - for i := 0; i < len(a); i++ { - n += len(a[i]) - } - - b := make([]byte, n) - n = 0 - for _, s := range a { - n += copy(b[n:], s) - } - return string(b) -} diff --git a/render/render_test.go b/render/render_test.go index 0ffcf4d0..3ecca0e9 100644 --- a/render/render_test.go +++ b/render/render_test.go @@ -145,9 +145,3 @@ func TestRenderHTMLTemplate(t *testing.T) { assert.Equal(t, w.Body.String(), "Hello alexandernyquist") assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8") } - -func TestRenderJoinStrings(t *testing.T) { - assert.Equal(t, joinStrings("a", "BB", "c"), "aBBc") - assert.Equal(t, joinStrings("a", "", "c"), "ac") - assert.Equal(t, joinStrings("text/html", "; charset=utf-8"), "text/html; charset=utf-8") -} diff --git a/render/text.go b/render/text.go index bfcfc6ff..efd52015 100644 --- a/render/text.go +++ b/render/text.go @@ -15,7 +15,7 @@ func (_ plainTextRender) Render(w http.ResponseWriter, code int, data ...interfa } func WritePlainText(w http.ResponseWriter, code int, format string, values []interface{}) { - WriteHeader(w, code, "text/plain") + writeHeader(w, code, "text/plain; charset=utf-8") // we assume w.Write can not fail, is that right? if len(values) > 0 { fmt.Fprintf(w, format, values...) diff --git a/render/xml.go b/render/xml.go index 3792d5fa..8ebe302b 100644 --- a/render/xml.go +++ b/render/xml.go @@ -12,6 +12,6 @@ func (_ xmlRender) Render(w http.ResponseWriter, code int, data ...interface{}) } func WriteXML(w http.ResponseWriter, code int, data interface{}) error { - WriteHeader(w, code, "application/xml") + writeHeader(w, code, "application/xml; charset=utf-8") return xml.NewEncoder(w).Encode(data) }