Skip to content

Commit e315d7d

Browse files
committed
Merge pull request revel#728 from dmitrykakunin/http_status
Add default http.Status code for render methods.
2 parents d5f968d + 5a3dc24 commit e315d7d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

controller.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,17 @@ func (c *Controller) SetCookie(cookie *http.Cookie) {
5959
}
6060

6161
func (c *Controller) RenderError(err error) Result {
62+
c.setStatusIfNil(http.StatusInternalServerError)
63+
6264
return ErrorResult{c.RenderArgs, err}
6365
}
6466

67+
func (c *Controller) setStatusIfNil(status int) {
68+
if c.Response.Status == 0 {
69+
c.Response.Status = status
70+
}
71+
}
72+
6573
// Render a template corresponding to the calling Controller method.
6674
// Arguments will be added to c.RenderArgs prior to rendering the template.
6775
// They are keyed on their local identifier.
@@ -76,6 +84,8 @@ func (c *Controller) RenderError(err error) Result {
7684
// This action will render views/Users/ShowUser.html, passing in an extra
7785
// key-value "user": (User).
7886
func (c *Controller) Render(extraRenderArgs ...interface{}) Result {
87+
c.setStatusIfNil(http.StatusOK)
88+
7989
// Get the calling function name.
8090
_, _, line, ok := runtime.Caller(1)
8191
if !ok {
@@ -103,6 +113,7 @@ func (c *Controller) Render(extraRenderArgs ...interface{}) Result {
103113
// A less magical way to render a template.
104114
// Renders the given template, using the current RenderArgs.
105115
func (c *Controller) RenderTemplate(templatePath string) Result {
116+
c.setStatusIfNil(http.StatusOK)
106117

107118
// Get the Template.
108119
template, err := MainTemplateLoader.Template(templatePath)
@@ -118,21 +129,29 @@ func (c *Controller) RenderTemplate(templatePath string) Result {
118129

119130
// Uses encoding/json.Marshal to return JSON to the client.
120131
func (c *Controller) RenderJson(o interface{}) Result {
132+
c.setStatusIfNil(http.StatusOK)
133+
121134
return RenderJsonResult{o, ""}
122135
}
123136

124137
// Renders a JSONP result using encoding/json.Marshal
125138
func (c *Controller) RenderJsonP(callback string, o interface{}) Result {
139+
c.setStatusIfNil(http.StatusOK)
140+
126141
return RenderJsonResult{o, callback}
127142
}
128143

129144
// Uses encoding/xml.Marshal to return XML to the client.
130145
func (c *Controller) RenderXml(o interface{}) Result {
146+
c.setStatusIfNil(http.StatusOK)
147+
131148
return RenderXmlResult{o}
132149
}
133150

134151
// Render plaintext in response, printf style.
135152
func (c *Controller) RenderText(text string, objs ...interface{}) Result {
153+
c.setStatusIfNil(http.StatusOK)
154+
136155
finalText := text
137156
if len(objs) > 0 {
138157
finalText = fmt.Sprintf(text, objs...)
@@ -142,6 +161,8 @@ func (c *Controller) RenderText(text string, objs ...interface{}) Result {
142161

143162
// Render html in response
144163
func (c *Controller) RenderHtml(html string) Result {
164+
c.setStatusIfNil(http.StatusOK)
165+
145166
return &RenderHtmlResult{html}
146167
}
147168

@@ -186,6 +207,8 @@ func (c *Controller) Forbidden(msg string, objs ...interface{}) Result {
186207
// RenderFile returns a file, either displayed inline or downloaded
187208
// as an attachment. The name and size are taken from the file info.
188209
func (c *Controller) RenderFile(file *os.File, delivery ContentDisposition) Result {
210+
c.setStatusIfNil(http.StatusOK)
211+
189212
var (
190213
modtime = time.Now()
191214
fileInfo, err = file.Stat()
@@ -205,6 +228,8 @@ func (c *Controller) RenderFile(file *os.File, delivery ContentDisposition) Resu
205228
// it implements io.Reader). When called directly on something generated or
206229
// streamed, modtime should mostly likely be time.Now().
207230
func (c *Controller) RenderBinary(memfile io.Reader, filename string, delivery ContentDisposition, modtime time.Time) Result {
231+
c.setStatusIfNil(http.StatusOK)
232+
208233
return &BinaryResult{
209234
Reader: memfile,
210235
Name: filename,
@@ -219,6 +244,8 @@ func (c *Controller) RenderBinary(memfile io.Reader, filename string, delivery C
219244
// c.Redirect("/controller/action")
220245
// c.Redirect("/controller/%d/action", id)
221246
func (c *Controller) Redirect(val interface{}, args ...interface{}) Result {
247+
c.setStatusIfNil(http.StatusMovedPermanently)
248+
222249
if url, ok := val.(string); ok {
223250
if len(args) == 0 {
224251
return &RedirectToUrlResult{url}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy