-
Notifications
You must be signed in to change notification settings - Fork 0
/
emissione.go
185 lines (149 loc) · 4.51 KB
/
emissione.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package emissione
import (
"bytes"
"fmt"
"net/http"
"regexp"
"sort"
"strings"
)
// Writer is a handler used by emissione, to deliver a specific
// output to the client.
type Writer interface {
Write(w http.ResponseWriter, i interface{}) error
}
// WriterMapping is a convenience alias for map[string]Writer
type WriterMapping map[string]Writer
// Handler is the core of emissione, defining the mapping of
// accept header values to Writers.
type Handler struct {
defaultHandler Writer
handlers WriterMapping
wildcardDetector *regexp.Regexp
}
// Default returns a opinionated configured emissione Handler.
func Default() *Handler {
json := NewJSONIndentWriter("", " ")
xml := NewXmlIndentWriter("", " ")
return New(
json,
WriterMapping{
"application/json": json,
"application/json;charset=utf-8": json,
"application/json; charset=utf-8": json,
"application/xml": xml,
"application/xml;charset=utf-8": xml,
"application/xml; charset=utf-8": xml,
},
)
}
// New returns a user-configured emissione Handler.
func New(defaultHandler Writer, handlerMapping WriterMapping) *Handler {
wildcardDetector, err := regexp.Compile(".*?/\\*(;q=[\\d.]+)?")
if err != nil {
panic(fmt.Sprintf("unexpected error building regex: %s", err))
}
lowerCaseMapping := make(WriterMapping, len(handlerMapping))
for k, v := range handlerMapping {
lowerCaseMapping[strings.ToLower(k)] = v
}
return &Handler{
wildcardDetector: wildcardDetector,
defaultHandler: defaultHandler,
handlers: lowerCaseMapping,
}
}
func (h Handler) findWriterByRequest(r *http.Request) Writer {
acceptHeader := r.Header.Get("Accept")
// If Accept headers was not set, use the default
if acceptHeader == "" {
return h.defaultHandler
}
// Split and lower-case
accepts := strings.Split(acceptHeader, ",")
for i := range accepts {
accepts[i] = strings.ToLower(accepts[i])
}
acceptsPrioritized := AcceptSlice(accepts)
sort.Sort(sort.Reverse(acceptsPrioritized))
// Iterate all send types
for _, accept := range acceptsPrioritized {
if possibleWriter := h.findWriterByType(accept); possibleWriter != nil {
return possibleWriter
}
}
return h.defaultHandler
}
func (h Handler) findWriterByType(acceptType string) Writer {
// 1: Wildcard?
if strings.HasPrefix(acceptType, "*/*") {
return h.defaultHandler
}
// 2: Exact match?
if writer, match := h.handlers[acceptType]; match {
return writer
}
// 3: Exact match without quality?
if strings.Contains(acceptType, "q=") {
accepts := strings.Split(acceptType, "q=")
acceptQualityLess := accepts[0]
if len(accepts) > 1 {
acceptQualityLess = strings.Join(accepts[:len(accepts)-1], "")
}
acceptQualityLess = strings.Trim(acceptQualityLess, "; ")
if writer, match := h.handlers[acceptQualityLess]; match {
return writer
}
}
// 4: Partial wildcard? (e.g. image/*)
if h.wildcardDetector.MatchString(acceptType) {
return h.findPartialWriterMatch(strings.Split(acceptType, "/")[0])
}
return nil
}
func (h Handler) findPartialWriterMatch(mimeType string) Writer {
applicableHandlers := WriterMapping{}
// Find all handlers which are applicable
for k, v := range h.handlers {
if strings.HasPrefix(k, mimeType) {
applicableHandlers[k] = v
}
}
handlerKeys := make([]string, len(applicableHandlers))
sort.Strings(handlerKeys)
return applicableHandlers[handlerKeys[0]]
}
// Write writes the given status code and object to the ResponseWriter.
// The Request object is used to resolve the desired output type.
func (h Handler) Write(w http.ResponseWriter, r *http.Request, code int, i interface{}) {
writer := h.findWriterByRequest(r)
if writer != nil {
// Buffer the result first, to catch encoding errors
sw := &statusWriter{ResponseWriter: w, Body: new(bytes.Buffer)}
if err := writer.Write(sw, i); err != nil {
panic(err)
}
w.WriteHeader(code)
if _, err := sw.Body.WriteTo(w); err != nil {
// welp, we did our best - something is terribly broken
panic(err)
}
} else {
w.WriteHeader(http.StatusUnsupportedMediaType)
// Don't write anything. If we don't have a writer, we can't
// reliably give the client back anything useful.
// e.g. if the client requested XML from a JSON-only API,
// it makes no sense to respond with a JSON style error.
}
}
// statusWriter is a helper to buffer
type statusWriter struct {
http.ResponseWriter
Body *bytes.Buffer
}
func (w *statusWriter) WriteHeader(status int) {
// NOP
}
func (w *statusWriter) Write(b []byte) (int, error) {
return w.Body.Write(b)
}