-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathrequest_processor.go
148 lines (116 loc) · 2.71 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
package muxie
import (
"bytes"
"encoding/json"
"encoding/xml"
"io/ioutil"
"net/http"
)
var (
Charset = "utf-8"
JSON = &jsonProcessor{Prefix: nil, Indent: "", UnescapeHTML: false}
XML = &xmlProcessor{Indent: ""}
)
func withCharset(cType string) string {
return cType + "; charset=" + Charset
}
type Binder interface {
Bind(*http.Request, interface{}) error
}
func Bind(r *http.Request, b Binder, ptrOut interface{}) error {
return b.Bind(r, ptrOut)
}
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
}
func Dispatch(w http.ResponseWriter, d Dispatcher, v interface{}) error {
return d.Dispatch(w, v)
}
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
}