-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontext_finalize.go
More file actions
138 lines (126 loc) · 3.15 KB
/
context_finalize.go
File metadata and controls
138 lines (126 loc) · 3.15 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
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 finalBodyReader io.Reader
var redirect *Redirect
if !c.hasWrittenBody && c.Body != nil {
if bodyReader, ok := c.Body.(io.Reader); ok {
finalBodyReader = bodyReader
} else {
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 {
finalBodyReader = marshalledReader
} else {
c.Status = 500
if PrintHandlerErrors {
fmt.Printf("Error occurred when marshalling response body: %s", err)
}
}
}
}
}
if c.Status == 0 {
if redirect != nil {
c.Status = 302
} else if finalBodyReader == nil {
c.Status = 404
} else {
c.Status = 200
}
}
if redirect != nil {
to := resolveRedirectLocation(redirect.To, c.Request().URL.Path)
c.Headers.Set("Location", to)
}
writer := c.bodyWriter
if writer == nil {
writer = c.responseWriter
if !c.inhibitResponse {
for key, values := range c.Headers {
for _, value := range values {
writer.Header().Add(key, value)
}
}
for _, cookie := range c.Cookies {
http.SetCookie(writer, cookie)
}
}
}
if !c.inhibitResponse {
writer.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(writer, finalBodyReader)
if finalBodyReaderCloser, ok := finalBodyReader.(io.Closer); ok {
if err := finalBodyReaderCloser.Close(); err != nil && PrintHandlerErrors {
fmt.Printf("Failed to close body read closer: %s", err)
}
}
if err != nil {
c.Status = 500
fmt.Printf("error occurred when writing response body: %s", err)
}
}
}
if closer, ok := writer.(io.Closer); ok {
if err := closer.Close(); err != nil && PrintHandlerErrors {
fmt.Printf("Failed to close body writer: %s", err)
}
}
c.FinalError = c.Error
c.FinalErrorStack = c.ErrorStack
if c.doneChannel != nil {
close(c.doneChannel)
}
}
func resolveRedirectLocation(to string, currentPath string) string {
toUrl, err := url.Parse(to)
if err != nil {
return to
}
if toUrl.Scheme != "" || toUrl.Host != "" {
return to
}
if currentPath == "" {
currentPath = "/"
}
if to == "" || to[0] != '/' {
dir, _ := path.Split(currentPath)
to = dir + to
}
return to
}