-
Notifications
You must be signed in to change notification settings - Fork 4
/
response.go
169 lines (135 loc) · 3.79 KB
/
response.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
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
package aspen
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"bitbucket.org/ww/goautoneg"
)
var (
http406 = newErrHttp406()
defaultAcceptHeader = "text/html,application/xhtml+xml," +
"application/xml;q=0.9,*/*;q=0.8"
)
type errorHttp406 struct {
msg string
}
type HTTPResponseWrapper struct {
website *Website
w http.ResponseWriter
req *http.Request
statusCode int
bodyBytes []byte
bodyObj interface{}
contentType string
contentTypeHandlers map[string]func(*HTTPResponseWrapper)
handledContentTypes []string
err error
}
func newErrHttp406() *errorHttp406 {
return &errorHttp406{
msg: "406: No acceptable media type available",
}
}
func (me *errorHttp406) Error() string {
return me.msg
}
func (me *HTTPResponseWrapper) SetContentType(contentType string) {
if len(contentType) == 0 {
debugf("Ignoring call to `SetContentType` because argument is empty!")
return
}
if strings.HasPrefix(contentType, "text/") && !strings.Contains(contentType, "charset=") {
contentType = fmt.Sprintf("%v; charset=%v", contentType, me.website.CharsetDynamic)
}
me.contentType = contentType
}
func (me *HTTPResponseWrapper) SetBodyBytes(body []byte) {
me.bodyBytes = body
}
func (me *HTTPResponseWrapper) SetBody(o interface{}) {
me.bodyObj = o
}
func (me *HTTPResponseWrapper) SetStatusCode(sc int) {
me.statusCode = sc
}
func (me *HTTPResponseWrapper) SetError(err error) {
me.err = err
}
func (me *HTTPResponseWrapper) respond500(err error) {
me.w.Header().Set("Content-Type",
fmt.Sprintf("text/html; charset=%v", me.website.CharsetDynamic))
if isDebug {
me.w.Header().Set("X-AspenGo-Error", fmt.Sprintf("%v", err))
}
me.w.WriteHeader(http.StatusInternalServerError)
me.w.Write(http500Response)
}
func (me *HTTPResponseWrapper) respond406(err error) {
me.w.Header().Set("Content-Type",
fmt.Sprintf("text/html; charset=%v", me.website.CharsetDynamic))
if isDebug {
me.w.Header().Set("X-AspenGo-Error", fmt.Sprintf("%v", err))
}
me.w.WriteHeader(http.StatusNotAcceptable)
me.w.Write(http406Response)
}
func (me *HTTPResponseWrapper) Respond() {
if me.err != nil {
if _, ok := me.err.(*errorHttp406); ok {
me.respond406(me.err)
return
}
me.respond500(me.err)
return
}
me.w.Header().Set("Content-Type", me.contentType)
me.w.WriteHeader(me.statusCode)
me.w.Write(me.bodyBytes)
}
func (me *HTTPResponseWrapper) RespondJSON() {
if me.bodyObj == nil {
me.respond500(errors.New("JSON response body not set!"))
return
}
jsonBody, err := json.Marshal(me.bodyObj)
if err != nil {
me.respond500(err)
return
}
me.w.Header().Set("Content-Type", "application/json")
me.w.WriteHeader(me.statusCode)
me.w.Write(jsonBody)
}
func (me *HTTPResponseWrapper) RegisterContentTypeHandler(contentType string,
handlerFunc func(*HTTPResponseWrapper)) {
me.contentTypeHandlers[contentType] = handlerFunc
me.handledContentTypes = append(me.handledContentTypes, contentType)
}
func (me *HTTPResponseWrapper) NegotiateAndCallHandler() {
accept := me.req.Header.Get(internalAcceptHeader)
if len(accept) == 0 {
accept = me.req.Header.Get(http.CanonicalHeaderKey("Accept"))
}
debugf("Looking up handler for Accept: %q", accept)
debugf("Available content type handlers: %v", me.handledContentTypes)
negotiated := goautoneg.Negotiate(accept, me.handledContentTypes)
if len(negotiated) == 0 {
me.err = http406
return
}
handlerFunc, ok := me.contentTypeHandlers[negotiated]
if ok {
debugf("Calling handler %v for negotiated content type %q", handlerFunc, negotiated)
handlerFunc(me)
}
}
func (me *HTTPResponseWrapper) DebugContext(filename string, ctx map[string]interface{}) {
if me.website.Debug {
debugf("%q final context: %+v", filename, ctx)
for key, value := range ctx {
debugf("%q final context (%q): %+v", filename, key, value)
}
}
}