-
Notifications
You must be signed in to change notification settings - Fork 9
/
body.go
154 lines (144 loc) · 3.74 KB
/
body.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
package luddite
import (
"bytes"
"encoding/json"
"encoding/xml"
"mime"
"net/http"
"reflect"
"time"
"github.com/gorilla/schema"
)
const (
ContentTypeCss = "text/css"
ContentTypeGif = "image/gif"
ContentTypeHtml = "text/html"
ContentTypeJson = "application/json"
ContentTypeMsgpack = "application/msgpack"
ContentTypeMultipartFormData = "multipart/form-data"
ContentTypeOctetStream = "application/octet-stream"
ContentTypePlain = "text/plain"
ContentTypePng = "image/png"
ContentTypeProtobuf = "application/protobuf"
ContentTypeWwwFormUrlencoded = "application/x-www-form-urlencoded"
ContentTypeXml = "application/xml"
maxFormDataMemoryUsage = 10 * 1024 * 1024
)
var formDecoder = schema.NewDecoder()
func init() {
t := time.Time{}
formDecoder.RegisterConverter(t, ConvertTime)
}
func ConvertTime(value string) reflect.Value {
if t, err := time.Parse(time.RFC3339, value); err == nil {
return reflect.ValueOf(t)
}
return reflect.Value{}
}
func ReadRequest(req *http.Request, v interface{}) error {
SetContextRequestProgress(req.Context(), "luddite", "ReadRequest", "begin")
ct := req.Header.Get(HeaderContentType)
switch mt, _, _ := mime.ParseMediaType(ct); mt {
case ContentTypeMultipartFormData:
if err := req.ParseMultipartForm(maxFormDataMemoryUsage); err != nil {
return NewError(nil, EcodeDeserializationFailed, err)
}
if err := formDecoder.Decode(v, req.PostForm); err != nil {
return NewError(nil, EcodeDeserializationFailed, err)
}
return nil
case ContentTypeWwwFormUrlencoded:
if err := req.ParseForm(); err != nil {
return NewError(nil, EcodeDeserializationFailed, err)
}
if err := formDecoder.Decode(v, req.PostForm); err != nil {
return NewError(nil, EcodeDeserializationFailed, err)
}
return nil
case ContentTypeJson:
decoder := json.NewDecoder(req.Body)
err := decoder.Decode(v)
if err != nil {
return NewError(nil, EcodeDeserializationFailed, err)
}
return nil
case ContentTypeXml:
decoder := xml.NewDecoder(req.Body)
err := decoder.Decode(v)
if err != nil {
return NewError(nil, EcodeDeserializationFailed, err)
}
return nil
case "":
return nil
default:
return NewError(nil, EcodeUnsupportedMediaType, ct)
}
}
func WriteResponse(rw http.ResponseWriter, status int, v interface{}) (err error) {
var b []byte
if v != nil {
switch v.(type) {
case *Error:
case error:
v = NewError(nil, EcodeInternal, v)
}
switch rw.Header().Get(HeaderContentType) {
case ContentTypeJson:
b, err = json.Marshal(v)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
b, err = json.Marshal(NewError(nil, EcodeSerializationFailed, err))
if err != nil {
rw.Write(b)
}
return
}
case ContentTypeXml:
b, err = xml.Marshal(v)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
b, err = xml.Marshal(NewError(nil, EcodeSerializationFailed, err))
if err != nil {
rw.Write(b)
}
return
}
case ContentTypeHtml:
switch v.(type) {
case []byte:
b = v.([]byte)
case string:
b = []byte(v.(string))
default:
b, err = json.Marshal(v)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
b, err = json.Marshal(NewError(nil, EcodeSerializationFailed, err))
if err != nil {
rw.Write(b)
}
return
}
esc := new(bytes.Buffer)
json.HTMLEscape(esc, b)
b = esc.Bytes()
}
default:
switch v.(type) {
case []byte:
b = v.([]byte)
case string:
b = []byte(v.(string))
default:
rw.WriteHeader(http.StatusNotAcceptable)
return
}
}
}
rw.WriteHeader(status)
if b != nil {
_, err = rw.Write(b)
}
return
}