Skip to content

Commit

Permalink
Fast path for setting headers.
Browse files Browse the repository at this point in the history
- No heap allocation
- No indirection
- CanonicalMIMEHeaderKey() is not called
  • Loading branch information
manucorporat committed May 23, 2015
1 parent 35fd7fb commit a2105ce
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions render/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type (
}
)

const htmlContentType = "text/html; charset=utf-8"
var htmlContentType = []string{"text/html; charset=utf-8"}

func (r HTMLProduction) Instance(name string, data interface{}) Render {
return HTML{
Expand Down Expand Up @@ -58,6 +58,6 @@ func (r HTMLDebug) loadTemplate() *template.Template {
}

func (r HTML) Write(w http.ResponseWriter) error {
w.Header().Set("Content-Type", htmlContentType)
w.Header()["Content-Type"] = htmlContentType
return r.Template.ExecuteTemplate(w, r.Name, r.Data)
}
6 changes: 3 additions & 3 deletions render/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ type (
}
)

const jsonContentType = "application/json; charset=utf-8"
var jsonContentType = []string{"application/json; charset=utf-8"}

func (r JSON) Write(w http.ResponseWriter) error {
w.Header().Set("Content-Type", jsonContentType)
w.Header()["Content-Type"] = jsonContentType
return json.NewEncoder(w).Encode(r.Data)
}

func (r IndentedJSON) Write(w http.ResponseWriter) error {
w.Header().Set("Content-Type", jsonContentType)
w.Header()["Content-Type"] = jsonContentType
jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions render/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ type String struct {
Data []interface{}
}

const plainContentType = "text/plain; charset=utf-8"
var plainContentType = []string{"text/plain; charset=utf-8"}

func (r String) Write(w http.ResponseWriter) error {
header := w.Header()
if _, exist := header["Content-Type"]; !exist {
header.Set("Content-Type", plainContentType)
header["Content-Type"] = plainContentType
}
if len(r.Data) > 0 {
fmt.Fprintf(w, r.Format, r.Data...)
Expand Down
4 changes: 2 additions & 2 deletions render/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ type XML struct {
Data interface{}
}

const xmlContentType = "application/xml; charset=utf-8"
var xmlContentType = []string{"application/xml; charset=utf-8"}

func (r XML) Write(w http.ResponseWriter) error {
w.Header().Set("Content-Type", xmlContentType)
w.Header()["Content-Type"] = xmlContentType
return xml.NewEncoder(w).Encode(r.Data)
}

0 comments on commit a2105ce

Please sign in to comment.