@@ -59,9 +59,17 @@ func (c *Controller) SetCookie(cookie *http.Cookie) {
59
59
}
60
60
61
61
func (c * Controller ) RenderError (err error ) Result {
62
+ c .setStatusIfNil (http .StatusInternalServerError )
63
+
62
64
return ErrorResult {c .RenderArgs , err }
63
65
}
64
66
67
+ func (c * Controller ) setStatusIfNil (status int ) {
68
+ if c .Response .Status == 0 {
69
+ c .Response .Status = status
70
+ }
71
+ }
72
+
65
73
// Render a template corresponding to the calling Controller method.
66
74
// Arguments will be added to c.RenderArgs prior to rendering the template.
67
75
// They are keyed on their local identifier.
@@ -76,6 +84,8 @@ func (c *Controller) RenderError(err error) Result {
76
84
// This action will render views/Users/ShowUser.html, passing in an extra
77
85
// key-value "user": (User).
78
86
func (c * Controller ) Render (extraRenderArgs ... interface {}) Result {
87
+ c .setStatusIfNil (http .StatusOK )
88
+
79
89
// Get the calling function name.
80
90
_ , _ , line , ok := runtime .Caller (1 )
81
91
if ! ok {
@@ -103,6 +113,7 @@ func (c *Controller) Render(extraRenderArgs ...interface{}) Result {
103
113
// A less magical way to render a template.
104
114
// Renders the given template, using the current RenderArgs.
105
115
func (c * Controller ) RenderTemplate (templatePath string ) Result {
116
+ c .setStatusIfNil (http .StatusOK )
106
117
107
118
// Get the Template.
108
119
template , err := MainTemplateLoader .Template (templatePath )
@@ -118,21 +129,29 @@ func (c *Controller) RenderTemplate(templatePath string) Result {
118
129
119
130
// Uses encoding/json.Marshal to return JSON to the client.
120
131
func (c * Controller ) RenderJson (o interface {}) Result {
132
+ c .setStatusIfNil (http .StatusOK )
133
+
121
134
return RenderJsonResult {o , "" }
122
135
}
123
136
124
137
// Renders a JSONP result using encoding/json.Marshal
125
138
func (c * Controller ) RenderJsonP (callback string , o interface {}) Result {
139
+ c .setStatusIfNil (http .StatusOK )
140
+
126
141
return RenderJsonResult {o , callback }
127
142
}
128
143
129
144
// Uses encoding/xml.Marshal to return XML to the client.
130
145
func (c * Controller ) RenderXml (o interface {}) Result {
146
+ c .setStatusIfNil (http .StatusOK )
147
+
131
148
return RenderXmlResult {o }
132
149
}
133
150
134
151
// Render plaintext in response, printf style.
135
152
func (c * Controller ) RenderText (text string , objs ... interface {}) Result {
153
+ c .setStatusIfNil (http .StatusOK )
154
+
136
155
finalText := text
137
156
if len (objs ) > 0 {
138
157
finalText = fmt .Sprintf (text , objs ... )
@@ -142,6 +161,8 @@ func (c *Controller) RenderText(text string, objs ...interface{}) Result {
142
161
143
162
// Render html in response
144
163
func (c * Controller ) RenderHtml (html string ) Result {
164
+ c .setStatusIfNil (http .StatusOK )
165
+
145
166
return & RenderHtmlResult {html }
146
167
}
147
168
@@ -186,6 +207,8 @@ func (c *Controller) Forbidden(msg string, objs ...interface{}) Result {
186
207
// RenderFile returns a file, either displayed inline or downloaded
187
208
// as an attachment. The name and size are taken from the file info.
188
209
func (c * Controller ) RenderFile (file * os.File , delivery ContentDisposition ) Result {
210
+ c .setStatusIfNil (http .StatusOK )
211
+
189
212
var (
190
213
modtime = time .Now ()
191
214
fileInfo , err = file .Stat ()
@@ -205,6 +228,8 @@ func (c *Controller) RenderFile(file *os.File, delivery ContentDisposition) Resu
205
228
// it implements io.Reader). When called directly on something generated or
206
229
// streamed, modtime should mostly likely be time.Now().
207
230
func (c * Controller ) RenderBinary (memfile io.Reader , filename string , delivery ContentDisposition , modtime time.Time ) Result {
231
+ c .setStatusIfNil (http .StatusOK )
232
+
208
233
return & BinaryResult {
209
234
Reader : memfile ,
210
235
Name : filename ,
@@ -219,6 +244,8 @@ func (c *Controller) RenderBinary(memfile io.Reader, filename string, delivery C
219
244
// c.Redirect("/controller/action")
220
245
// c.Redirect("/controller/%d/action", id)
221
246
func (c * Controller ) Redirect (val interface {}, args ... interface {}) Result {
247
+ c .setStatusIfNil (http .StatusMovedPermanently )
248
+
222
249
if url , ok := val .(string ); ok {
223
250
if len (args ) == 0 {
224
251
return & RedirectToUrlResult {url }
0 commit comments