-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context-finalize.go
122 lines (111 loc) · 2.77 KB
/
context-finalize.go
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
package navaros
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strings"
)
// finalize is called by the router after all matching handlers have processed
// the request. It is responsible for writing the response to the client. For
// libraries which to extend or encapsulate the functionality of Navaros,
// Finalize can be called with the CtxFinalize function.
func (c *Context) finalize() {
if c.Error != nil {
c.Status = 500
if PrintHandlerErrors {
fmt.Printf("Error occurred when handling request: %s\n%s", c.Error, c.ErrorStack)
}
}
var redirect *Redirect
var finalBodyReader io.Reader
if !c.hasWrittenBody && c.Body != nil {
switch body := c.Body.(type) {
case *Redirect:
redirect = body
case Redirect:
redirect = &body
case string:
finalBodyReader = strings.NewReader(body)
case []byte:
finalBodyReader = bytes.NewReader(body)
default:
marshalledReader, err := c.marshallResponseBody()
if err != nil {
c.Status = 500
fmt.Printf("Error occurred when marshalling response body: %s", err)
} else {
finalBodyReader = marshalledReader
}
}
}
if c.Status == 0 {
if redirect != nil {
c.Status = 302
} else if finalBodyReader == nil {
c.Status = 404
} else {
c.Status = 200
}
}
if !c.hasWrittenHeaders {
for key, values := range c.Headers {
for _, value := range values {
c.bodyWriter.Header().Add(key, value)
}
}
if redirect != nil {
to := redirect.To
toUrl, err := url.Parse(to)
if err != nil {
c.Status = 500
fmt.Printf("Error occurred when parsing redirect url: %s", err)
} else {
if toUrl.Scheme == "" && toUrl.Host == "" {
currentPath := c.Request().URL.Path
if currentPath == "" {
currentPath = "/"
}
if to == "" || to[0] != '/' {
currentChunks, _ := path.Split(currentPath)
to = currentChunks + to
}
query := ""
if i := strings.Index(to, "?"); i != -1 {
query = to[i:]
to = to[:i]
}
to += query
}
}
c.bodyWriter.Header().Add("Location", to)
}
for _, cookie := range c.Cookies {
http.SetCookie(c.bodyWriter, cookie)
}
if !c.inhibitResponse {
c.bodyWriter.WriteHeader(c.Status)
}
}
hasBody := finalBodyReader != nil
is100Range := c.Status >= 100 && c.Status < 200
is204Or304 := c.Status == 204 || c.Status == 304
if !c.inhibitResponse && hasBody {
if is100Range || is204Or304 {
fmt.Printf("Response with status %d has body but no content is expected", c.Status)
} else {
_, err := io.Copy(c.bodyWriter, finalBodyReader)
if err != nil {
c.Status = 500
fmt.Printf("Error occurred when writing response body: %s", err)
}
}
}
c.FinalError = c.Error
c.FinalErrorStack = c.ErrorStack
for _, doneHandler := range c.doneHandlers {
doneHandler()
}
}