-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
request_processor.go
181 lines (148 loc) · 4.04 KB
/
request_processor.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
package muxie
import (
"bytes"
"encoding/json"
"encoding/xml"
"io/ioutil"
"net/http"
)
var (
// Charset is the default content type charset for Request Processors .
Charset = "utf-8"
// JSON implements the full `Processor` interface.
// It is responsible to dispatch JSON results to the client and to read JSON
// data from the request body.
//
// Usage:
// To read from a request:
// muxie.Bind(r, muxie.JSON, &myStructValue)
// To send a response:
// muxie.Dispatch(w, muxie.JSON, mySendDataValue)
JSON = &jsonProcessor{Prefix: nil, Indent: "", UnescapeHTML: false}
// XML implements the full `Processor` interface.
// It is responsible to dispatch XML results to the client and to read XML
// data from the request body.
//
// Usage:
// To read from a request:
// muxie.Bind(r, muxie.XML, &myStructValue)
// To send a response:
// muxie.Dispatch(w, muxie.XML, mySendDataValue)
XML = &xmlProcessor{Indent: ""}
)
func withCharset(cType string) string {
return cType + "; charset=" + Charset
}
// Binder is the interface which `muxie.Bind` expects.
// It is used to bind a request to a go struct value (ptr).
type Binder interface {
Bind(*http.Request, interface{}) error
}
// Bind accepts the current request and any `Binder` to bind
// the request data to the "ptrOut".
func Bind(r *http.Request, b Binder, ptrOut interface{}) error {
return b.Bind(r, ptrOut)
}
// Dispatcher is the interface which `muxie.Dispatch` expects.
// It is used to send a response based on a go struct value.
type Dispatcher interface {
// no io.Writer because we need to set the headers here,
// Binder and Processor are only for HTTP.
Dispatch(http.ResponseWriter, interface{}) error
}
// Dispatch accepts the current response writer and any `Dispatcher`
// to send the "v" to the client.
func Dispatch(w http.ResponseWriter, d Dispatcher, v interface{}) error {
return d.Dispatch(w, v)
}
// Processor implements both `Binder` and `Dispatcher` interfaces.
// It is used for implementations that can `Bind` and `Dispatch`
// the same data form.
//
// Look `JSON` and `XML` for more.
type Processor interface {
Binder
Dispatcher
}
var (
newLineB byte = '\n'
// the html codes for unescaping
ltHex = []byte("\\u003c")
lt = []byte("<")
gtHex = []byte("\\u003e")
gt = []byte(">")
andHex = []byte("\\u0026")
and = []byte("&")
)
type jsonProcessor struct {
Prefix []byte
Indent string
UnescapeHTML bool
}
var _ Processor = (*jsonProcessor)(nil)
func (p *jsonProcessor) Bind(r *http.Request, v interface{}) error {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
return json.Unmarshal(b, v)
}
func (p *jsonProcessor) Dispatch(w http.ResponseWriter, v interface{}) error {
var (
result []byte
err error
)
if indent := p.Indent; indent != "" {
marshalIndent := json.MarshalIndent
result, err = marshalIndent(v, "", indent)
result = append(result, newLineB)
} else {
marshal := json.Marshal
result, err = marshal(v)
}
if err != nil {
return err
}
if p.UnescapeHTML {
result = bytes.Replace(result, ltHex, lt, -1)
result = bytes.Replace(result, gtHex, gt, -1)
result = bytes.Replace(result, andHex, and, -1)
}
if len(p.Prefix) > 0 {
result = append([]byte(p.Prefix), result...)
}
w.Header().Set("Content-Type", withCharset("application/json"))
_, err = w.Write(result)
return err
}
type xmlProcessor struct {
Indent string
}
var _ Processor = (*xmlProcessor)(nil)
func (p *xmlProcessor) Bind(r *http.Request, v interface{}) error {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
return xml.Unmarshal(b, v)
}
func (p *xmlProcessor) Dispatch(w http.ResponseWriter, v interface{}) error {
var (
result []byte
err error
)
if indent := p.Indent; indent != "" {
marshalIndent := xml.MarshalIndent
result, err = marshalIndent(v, "", indent)
result = append(result, newLineB)
} else {
marshal := xml.Marshal
result, err = marshal(v)
}
if err != nil {
return err
}
w.Header().Set("Content-Type", withCharset("text/xml"))
_, err = w.Write(result)
return err
}