Repository navigation
Expand file tree
/
Copy pathresults.go
More file actions
456 lines (372 loc) · 11.1 KB
/
Copy pathresults.go
File metadata and controls
456 lines (372 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
2
8000
34
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package revel
import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
"code.google.com/p/go.net/websocket"
)
type Result interface {
Apply(req *Request, resp *Response)
}
// Action methods return this result to request a template be rendered.
type RenderTemplateResult struct {
Template Template // this could be layout if exists
TemplateName string // of original template name, useful with error processing
RenderArgs map[string]interface{}
}
func (r *RenderTemplateResult) Apply(req *Request, resp *Response) {
// Handle panics when rendering templates.
defer func() {
if err := recover(); err != nil {
ERROR.Println(err)
PlaintextErrorResult{fmt.Errorf("Template Execution Panic in %s:\n%s",
r.Template.Name(), err)}.Apply(req, resp)
}
}()
if captureErr := r.renderCapture(req, resp); captureErr != nil {
ErrorResult{r.RenderArgs, captureErr}.Apply(req, resp)
return
}
chunked := Config.BoolDefault("results.chunked", false)
// If it's a HEAD request, throw away the bytes.
out := io.Writer(resp.Out)
if req.Method == "HEAD" {
out = ioutil.Discard
}
// Render the template into a temporary buffer, to see if there was an error
// rendering the template. If not, then copy it into the response buffer.
// Otherwise, template render errors may result in unpredictable HTML (and
// would carry a 200 status code)
// (Always render to a temporary buffer first to avoid having error pages
// distorted by HTML already written)
var buf bytes.Buffer
r.renderTemplate(req, resp, &buf)
if buf.Len() == 0 {
// avoid http connection hanging up!
buf.WriteTo(out)
return
}
resp.WriteHeader(http.StatusOK, "text/html; charset=utf-8")
if !chunked {
resp.Out.Header().Set("Content-Length", strconv.Itoa(buf.Len()))
} else {
// It isn't needed for progressive rendering.
// However, it is needed when the total content length is unknown before the first bytes are sent.
resp.Out.Header().Set("Transfer-Encoding", "chunked")
}
buf.WriteTo(out)
}
func (r *RenderTemplateResult) renderTemplate(req *Request, resp *Response, buf *bytes.Buffer) {
err := r.Template.Render(buf, r.RenderArgs)
if err == nil {
return
}
// clean rendered result
buf.Reset()
var (
content []string
name, line, description = parseTemplateError(err)
)
if name == "" {
name = r.Template.Name()
content = r.Template.Content()
} else if goTemplate, err := MainTemplateLoader.Template(name); err == nil {
content = goTemplate.Content()
}
compileError := &Error{
Title: "Template Execution Error",
Line: line,
Path: name,
Description: description,
SourceLines: content,
}
ERROR.Printf("Template Execution Error (in %s): %s", name, description)
// respond as internal server error
resp.Status = 500
ErrorResult{r.RenderArgs, compileError}.Apply(req, resp)
}
func (r *RenderTemplateResult) renderCapture(req *Request, resp *Response) error {
yield2blocks, err := MainTemplateLoader.Yield2Blocks(r.TemplateName)
if err != nil {
return nil
}
// apply all yielded blocks to r.RenderArgs
for yieldName, blockName := range yield2blocks {
goTemplate, err := MainTemplateLoader.Template(blockName)
if err != nil {
ERROR.Println("Failed loading template block %s of %s : %s", blockName, r.TemplateName, err.Error())
continue
}
captureResult := CaptureTemplateResult{
Template: goTemplate,
RenderArgs: r.RenderArgs,
}
captureResult.Apply(req, resp)
if blockErr := captureResult.Error(); blockErr != nil {
if goTemplate, err := MainTemplateLoader.Template(r.TemplateName); err == nil {
_, line, description := parseTemplateError(blockErr)
return &Error{
Title: "Template Execution Error (Capture)",
Line: line,
Path: r.TemplateName,
Description: description,
SourceLines: goTemplate.Content(),
}
}
return blockErr
}
r.RenderArgs[yieldName] = captureResult.HTML()
}
return nil
}
type CaptureTemplateResult struct {
Template Template
RenderArgs map[string]interface{}
renderedHtml string
renderedError error
}
func (r *CaptureTemplateResult) Apply(req *Request, resp *Response) {
// Handle panics when rendering templates.
defer func() {
if panicErr := recover(); panicErr != nil {
r.renderedError = errors.New(fmt.Sprintf("Template Execution Panic (Capture) : %s\n", panicErr))
return
}
}()
// Internal help template variable for some purposes?
r.RenderArgs["RevelTemplateCaptureMode"] = true
buf := bytes.NewBuffer([]byte{})
if err := r.Template.Render(buf, r.RenderArgs); err != nil {
r.renderedError = errors.New(fmt.Sprintf("Template Excution Error (Capture) : %s\n", err.Error()))
return
}
r.renderedHtml = buf.String()
return
}
func (r *CaptureTemplateResult) HTML() template.HTML {
return template.HTML(r.renderedHtml)
}
func (r *CaptureTemplateResult) Error() error {
return r.renderedError
}
type RenderTextResult struct {
text string
}
func (r RenderTextResult) Apply(req *Request, resp *Response) {
resp.WriteHeader(http.StatusOK, "text/plain; charset=utf-8")
resp.Out.Write([]byte(r.text))
}
type RenderHtmlResult struct {
html string
}
func (r RenderHtmlResult) Apply(req *Request, resp *Response) {
resp.WriteHeader(http.StatusOK, "text/html; charset=utf-8")
resp.Out.Write([]byte(r.html))
}
type RenderJsonResult struct {
json interface{}
callback string
}
func (r RenderJsonResult) Apply(req *Request, resp *Response) {
var (
b []byte
err error
)
if Config.BoolDefault("results.pretty", false) {
b, err = json.MarshalIndent(r.json, "", " ")
} else {
b, err = json.Marshal(r.json)
}
if err != nil {
ERROR.Println("json marshal error : ", err.Error())
ErrorResult{Error: err}.Apply(req, resp)
return
}
if r.callback == "" {
resp.WriteHeader(http.StatusOK, "application/json; charset=utf-8")
resp.Out.Write(b)
return
}
resp.WriteHeader(http.StatusOK, "application/javascript; charset=utf-8")
resp.Out.Write([]byte(r.callback + "("))
resp.Out.Write(b)
resp.Out.Write([]byte(");"))
}
type RenderXmlResult struct {
xml interface{}
}
func (r RenderXmlResult) Apply(req *Request, resp *Response) {
var (
b []byte
err error
)
if Config.BoolDefault("results.pretty", false) {
b, err = xml.MarshalIndent(r.xml, "", " ")
} else {
b, err = xml.Marshal(r.xml)
}
if err != nil {
ERROR.Println("xml marshal error : ", err.Error())
ErrorResult{Error: err}.Apply(req, resp)
return
}
resp.WriteHeader(http.StatusOK, "application/xml; charset=utf-8")
resp.Out.Write(b)
}
// This result is used when the template loader or error template is not available.
type PlaintextErrorResult struct {
Error error
}
func (r PlaintextErrorResult) Apply(req *Request, resp *Response) {
resp.WriteHeader(http.StatusInternalServerError, "text/plain; charset=utf-8")
resp.Out.Write([]byte(r.Error.Error()))
}
// This result handles all kinds of error codes (500, 404, ..).
// It renders the relevant error page (errors/CODE.format, e.g. errors/500.json).
// If RunMode is "dev", this results in a friendly error page.
type ErrorResult struct {
RenderArgs map[string]interface{}
Error error
}
func (r ErrorResult) Apply(req *Request, resp *Response) {
// This func shows a plaintext error message, in case the template rendering
// doesn't work.
showPlaintext := func(err error) {
PlaintextErrorResult{fmt.Errorf("Server Error:\n%s\n\n"+
"Additionally, an error occurred when rendering the error page:\n%s",
r.Error, err)}.Apply(req, resp)
}
format := req.Format
status := resp.Status
if status == 0 {
status = http.StatusInternalServerError
}
contentType := ContentTypeByFilename("revel." + format)
if contentType == DefaultFileContentType {
contentType = "text/plain"
}
// Get the error template.
var err error
templateName := fmt.Sprintf("errors/%d.%s", status, format)
// first, search app/views/errors
templateSet, err := MainTemplateLoader.Template(templateName)
if err != nil || templateSet == nil {
// second, fallback to revel templates/errors
templateSet, err = RevelTemplateLoader.Template(templateName)
}
if templateSet == nil {
if err == nil {
err = fmt.Errorf("Couldn't find template %s", templateName)
}
showPlaintext(err)
return
}
// If it's not a revel error, wrap it in one.
var revelError *Error
switch err := r.Error.(type) {
case *Error:
revelError = err
case error:
revelError = &Error{
Title: "Server Error",
Description: err.Error(),
}
default:
revelError = &Error{
Title: "Unknown Server Error",
Description: "Unknown server error triggered",
}
}
if r.RenderArgs == nil {
r.RenderArgs = make(map[string]interface{})
}
r.RenderArgs["RunMode"] = RunMode
r.RenderArgs["Error"] = revelError
r.RenderArgs["Router"] = MainRouter
// Render it.
var buf bytes.Buffer
err = templateSet.Render(&buf, r.RenderArgs)
// If there was an error, print it in plain text.
if err != nil {
showPlaintext(err)
return
}
// need to check if we are on a websocket here
// net/http panics if we write to a hijacked connection
if req.Method != "WS" {
resp.WriteHeader(status, contentType)
buf.WriteTo(resp.Out)
} else {
websocket.Message.Send(req.Websocket, fmt.Sprint(revelError))
}
}
type ContentDisposition string
var (
Attachment ContentDisposition = "attachment"
Inline ContentDisposition = "inline"
)
type BinaryResult struct {
Reader io.Reader
Name string
Length int64
Delivery ContentDisposition
ModTime time.Time
}
func (r *BinaryResult) Apply(req *Request, resp *Response) {
disposition := string(r.Delivery)
if r.Name != "" {
disposition += fmt.Sprintf(`; filename="%s"`, strings.Replace(r.Name, `"`, `\"`, -1))
}
resp.Out.Header().Set("Content-Disposition", disposition)
// If we have a ReadSeeker, delegate to http.ServeContent
if rs, ok := r.Reader.(io.ReadSeeker); ok {
// http.ServeContent doesn't know about response.ContentType, so we set the respective header.
contentType := resp.ContentType
if contentType == "" {
contentType = ContentTypeByFilename(r.Name)
}
resp.Out.Header().Set("Content-Type", contentType)
http.ServeContent(resp.Out, req.Request, r.Name, r.ModTime, rs)
} else {
// Else, do a simple io.Copy.
if r.Length != -1 {
resp.Out.Header().Set("Content-Length", strconv.FormatInt(r.Length, 10))
}
resp.WriteHeader(http.StatusOK, ContentTypeByFilename(r.Name))
io.Copy(resp.Out, r.Reader)
}
// Close the Reader if we can
if v, ok := r.Reader.(io.Closer); ok {
v.Close()
}
}
type RedirectToUrlResult struct {
url string
}
func (r *RedirectToUrlResult) Apply(req *Request, resp *Response) {
resp.Out.Header().Set("Location", r.url)
resp.WriteHeader(http.StatusFound, "")
}
type RedirectToActionResult struct {
val interface{}
args map[string]string
}
func (r *RedirectToActionResult) Apply(req *Request, resp *Response) {
url, err := FindResourceUrl(r.val, r.args)
if err != nil {
ERROR.Println("Couldn't resolve redirect:", err.Error())
ErrorResult{Error: err}.Apply(req, resp)
return
}
rurl := &RedirectToUrlResult{url: url}
rurl.Apply(req, resp)
}