Skip to content

Commit

Permalink
Enhanced write content type
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Mar 15, 2018
1 parent 9cee664 commit 003b0ad
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ var (
jsoni = jsoniter.ConfigCompatibleWithStandardLibrary
)

func (c *context) writeContentType(value string) {
header := c.Response().Header()
if header.Get(HeaderContentType) != "" {
header.Set(HeaderContentType, value)
}
}

func (c *context) Request() *http.Request {
return c.request
}
Expand Down Expand Up @@ -435,7 +442,7 @@ func (c *context) JSONP(code int, callback string, i interface{}) (err error) {
}

func (c *context) JSONPBlob(code int, callback string, b []byte) (err error) {
c.response.Header().Set(HeaderContentType, MIMEApplicationJavaScriptCharsetUTF8)
c.writeContentType(MIMEApplicationJavaScriptCharsetUTF8)
c.response.WriteHeader(code)
if _, err = c.response.Write([]byte(callback + "(")); err != nil {
return
Expand Down Expand Up @@ -468,7 +475,7 @@ func (c *context) XMLPretty(code int, i interface{}, indent string) (err error)
}

func (c *context) XMLBlob(code int, b []byte) (err error) {
c.response.Header().Set(HeaderContentType, MIMEApplicationXMLCharsetUTF8)
c.writeContentType(MIMEApplicationXMLCharsetUTF8)
c.response.WriteHeader(code)
if _, err = c.response.Write([]byte(xml.Header)); err != nil {
return
Expand All @@ -478,14 +485,14 @@ func (c *context) XMLBlob(code int, b []byte) (err error) {
}

func (c *context) Blob(code int, contentType string, b []byte) (err error) {
c.response.Header().Set(HeaderContentType, contentType)
c.writeContentType(contentType)
c.response.WriteHeader(code)
_, err = c.response.Write(b)
return
}

func (c *context) Stream(code int, contentType string, r io.Reader) (err error) {
c.response.Header().Set(HeaderContentType, contentType)
c.writeContentType(contentType)
c.response.WriteHeader(code)
_, err = io.Copy(c.response, r)
return
Expand Down

0 comments on commit 003b0ad

Please sign in to comment.