-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodecs.go
316 lines (278 loc) · 8.6 KB
/
codecs.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
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
234
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
package rapid
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"mime"
"net/http"
)
// Encoding and decoding requests on the client and server, respectively.
type RequestCodec interface {
// Encode request on client.
EncodeRequest() (headers http.Header, body io.ReadCloser, err error)
// Decode request.
DecodeRequest(r *http.Request) error
}
// Encoding and decoding responses on the server and client, respectively.
type ResponseCodec interface {
// Encode response into w.
// "http.Request" is included to support Accept-based responses.
EncodeResponse(r *http.Request, w http.ResponseWriter, status int, err error) error
// Decode response from r.
DecodeResponse(r *http.Response) error
}
type Codec interface {
RequestCodec
ResponseCodec
}
type panicResponseCodec struct {
RequestCodec
}
// Convert a RequestCodec into a Codec with ResponseCodec methods that panic.
func NopResponseCodec(codec RequestCodec) Codec {
return &panicResponseCodec{codec}
}
func (p *panicResponseCodec) EncodeResponse(r *http.Request, w http.ResponseWriter, status int, err error) error {
panic("EncodeResponse() is not supported")
}
func (p *panicResponseCodec) DecodeResponse(r *http.Response) error {
panic("DecodeResponse() is not supported")
}
type panicRequestCodec struct {
ResponseCodec
}
// Convert a ResponseCodec into a Codec with RequestCodec methods that panic.
func NopRequestCodec(codec ResponseCodec) Codec {
return &panicRequestCodec{codec}
}
func (p *panicRequestCodec) EncodeRequest() (headers http.Header, body io.ReadCloser, err error) {
panic("EncodeRequest() is not supported")
}
func (p *panicRequestCodec) DecodeRequest(r *http.Request) error {
panic("DecodeRequest() is not supported")
}
// ErrorResponse is the wire-format for a RAPID error response.
type ErrorResponse struct {
Error string `json:"e,omitempty"`
}
// A CodecFactory is a function that
type CodecFactory func(v interface{}) Codec
// Return v if it conforms to RequestCodec, otherwise use CodecFactory to
// encode/decode v.
func (c CodecFactory) Request(v interface{}) RequestCodec {
if c, ok := v.(RequestCodec); ok {
return c
}
return c(v)
}
// Return v if it conforms to ResponseCodec, otherwise use CodecFactory to
// encode/decode v.
func (c CodecFactory) Response(v interface{}) ResponseCodec {
if c, ok := v.(ResponseCodec); ok {
return c
}
return c(v)
}
// Return v if it conforms to Codec, otherwise use CodecFactory to encode/decode v.
func (c CodecFactory) Codec(v interface{}) Codec {
if c, ok := v.(Codec); ok {
return c
}
return c(v)
}
// Default, JSON codec.
type defaultCodec struct {
v interface{}
}
// Create a default JSON Codec.
func DefaultCodecFactory(v interface{}) Codec {
return &codecWrapper{&defaultCodec{v}}
}
func (d *defaultCodec) EncodeRequest() (http.Header, io.ReadCloser, error) {
body, err := json.Marshal(d.v)
if err != nil {
return nil, nil, err
}
contentTypeHeader := http.Header{
"Content-Type": {"application/json"},
"Accept": {"application/json"},
}
return contentTypeHeader, ioutil.NopCloser(bytes.NewReader(body)), nil
}
func (d *defaultCodec) DecodeRequest(r *http.Request) error {
return json.NewDecoder(r.Body).Decode(d.v)
}
func (d *defaultCodec) EncodeResponse(r *http.Request, w http.ResponseWriter, status int, err error) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
data := d.v
if err != nil && (status < 200 || status > 299) {
data = &ErrorResponse{Error: err.Error()}
}
return json.NewEncoder(w).Encode(data)
}
func (d *defaultCodec) DecodeResponse(r *http.Response) error {
if r.StatusCode < 200 || r.StatusCode >= 300 {
response := &ErrorResponse{}
if err := json.NewDecoder(r.Body).Decode(response); err != nil {
// Not a valid response structure, return error.
return Error(http.StatusInternalServerError, err.Error())
}
// Use error in response structure.
return Error(r.StatusCode, response.Error)
}
return json.NewDecoder(r.Body).Decode(d.v)
}
type FileDownload struct {
Filename string
MediaType string
Reader io.ReadCloser
}
func (f *FileDownload) EncodeResponse(r *http.Request, w http.ResponseWriter, status int, err error) error {
h := w.Header()
h.Set("Content-Type", f.MediaType)
h.Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{
"filename": f.Filename,
}))
w.WriteHeader(status)
_, err = io.Copy(w, f.Reader)
return err
}
func (f *FileDownload) DecodeResponse(r *http.Response) error {
_, params, err := mime.ParseMediaType(r.Header.Get("Content-Disposition"))
if err != nil {
return err
}
mt, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
return err
}
f.MediaType = mt
f.Filename = params["filename"]
f.Reader = r.Body
return nil
}
type FileUpload struct {
Filename string
MediaType string
Reader io.ReadCloser
}
func (f *FileUpload) EncodeRequest() (headers http.Header, body io.ReadCloser, err error) {
headers = http.Header{}
headers.Set("Content-Type", f.MediaType)
headers.Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{
"filename": f.Filename,
}))
return headers, f.Reader, nil
}
func (f *FileUpload) DecodeRequest(r *http.Request) error {
_, params, err := mime.ParseMediaType(r.Header.Get("Content-Disposition"))
if err != nil {
return err
}
mt, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
return err
}
f.MediaType = mt
f.Filename = params["filename"]
f.Reader = r.Body
return nil
}
type RawData []byte
func (d *RawData) EncodeRequest() (headers http.Header, body io.ReadCloser, err error) {
headers = http.Header{}
headers.Set("Content-Type", "application/octet-stream")
return headers, ioutil.NopCloser(bytes.NewReader(*d)), nil
}
func (d *RawData) DecodeRequest(r *http.Request) error {
defer r.Body.Close()
w := bytes.NewBuffer(nil)
if _, err := io.Copy(w, r.Body); err != nil {
return err
}
*d = w.Bytes()
return nil
}
func (d *RawData) EncodeResponse(r *http.Request, w http.ResponseWriter, status int, err error) error {
w.Header().Set("Content-Type", "application/octet-stream")
_, e := io.Copy(w, bytes.NewReader(*d))
return e
}
func (d *RawData) DecodeResponse(r *http.Response) error {
defer r.Body.Close()
w := bytes.NewBuffer(nil)
if _, err := io.Copy(w, r.Body); err != nil {
return err
}
*d = w.Bytes()
return nil
}
type codecWrapper struct {
Codec
}
// Wrapper factory providing some Codec convenience operations, such as
// automatic status+error inference, and injection of headers from HTTPStatus
// error values.
func NewResponseFixupCodecFactory(codec CodecFactory) CodecFactory {
return func(v interface{}) Codec { return &codecWrapper{codec(v)} }
}
func (e *codecWrapper) EncodeResponse(r *http.Request, w http.ResponseWriter, status int, err error) error {
status, err = FixupResonse(r, w, status, err)
return e.Codec.EncodeResponse(r, w, status, err)
}
// inferStatus attempts to infer the response status and error.
// If err and status are zero, status will be 201 for a POST response
// and 200 for any other method.
// For any other non-zero status when err is nil, err will be set to
// an HTTPError instance.
// If err is already a HTTPStatus, we extract the status code.
func inferStatus(r *http.Request, status int, err error) (int, error) {
// No error, just return status immediately.
if err == nil {
if status == 0 {
if r != nil && r.Method == "POST" {
status = http.StatusCreated
} else {
status = http.StatusOK
}
} else if status < 200 || status > 299 {
err = ErrorForStatus(status)
}
return status, err
}
// Check if it's a HTTPStatus error, in which case check the status code.
if st, ok := err.(*HTTPStatus); ok {
status = st.Status
} else if status == 0 {
// If it's any other error type, set 500 and continue.
status = http.StatusInternalServerError
if err == nil {
err = ErrorForStatus(status)
}
}
return status, err
}
// Convenience function that infers HTTP status and error, and injects headers
// from HTTPStatus error values (typically produced by Error(),
// ErrorForStatus() or ErrorWithHeaders()).
// Also see NewResponseFixupCodecFactory(), which creates a Codec that provides
// this functionality.
func FixupResonse(r *http.Request, w http.ResponseWriter, status int, err error) (int, error) {
status, err = inferStatus(r, status, err)
if e, ok := err.(*HTTPStatus); ok {
if e.Headers != nil {
for key, values := range e.Headers {
for _, value := range values {
w.Header().Add(key, value)
}
}
}
}
// If it's not an error, clear it so we don't return an unexpected error.
if status >= 200 && status <= 299 {
err = nil
}
return status, err
}