-
Notifications
You must be signed in to change notification settings - Fork 1
/
request_modifier.go
114 lines (97 loc) · 2.31 KB
/
request_modifier.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
package rzluramartian
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"github.com/clbanning/mxj"
"github.com/google/martian/parse"
"golang.org/x/net/html/charset"
)
const (
_contentType_JSON = "json"
_contentType_XML = "xml"
)
type (
RequestBodyModifierConfig struct {
OriginalContentType string `json:"original_content_type"`
ExpectedContentType string `json:"expected_content_type"`
}
RequestBodyModifier struct {
originalContentType string
expectedContentType string
}
)
func (m *RequestBodyModifier) ModifyRequest(req *http.Request) error {
if req.Body == nil {
return nil
}
var v map[string]interface{}
switch m.expectedContentType {
case _contentType_XML:
mxj.XmlCharsetReader = charset.NewReaderLabel
mv, err := mxj.NewMapXmlReader(xmlReader{r: req.Body})
if err != nil {
return err
}
v = mv
default:
// Default is JSON
payloadBytes, err := ioutil.ReadAll(req.Body)
if err != nil {
return err
}
req.Body.Close()
err = json.Unmarshal(payloadBytes, &v)
if err != nil {
return err
}
}
switch m.originalContentType {
case _contentType_XML:
transformedDataBytes, err := mxj.AnyXml(v)
if err != nil {
return err
}
req.Header.Del("Content-Type")
req.Header.Set("Content-Type", "application/xml")
req.ContentLength = int64(len(transformedDataBytes))
req.Body = ioutil.NopCloser(bytes.NewReader(transformedDataBytes))
default:
transformedDataBytes, err := json.Marshal(v)
if err != nil {
return err
}
req.Header.Del("Content-Type")
req.Header.Set("Content-Type", "application/json")
req.ContentLength = int64(len(transformedDataBytes))
req.Body = ioutil.NopCloser(bytes.NewReader(transformedDataBytes))
}
return nil
}
type xmlReader struct {
r io.Reader
}
func (x xmlReader) Read(p []byte) (n int, err error) {
n, err = x.r.Read(p)
if err != io.EOF {
return n, err
}
if len(p) == n {
return n, nil
}
p[n] = ([]byte("\n"))[0]
return n + 1, err
}
func requestTransformerFromJSON(b []byte) (*parse.Result, error) {
cfg := &RequestBodyModifierConfig{}
if err := json.Unmarshal(b, cfg); err != nil {
return nil, err
}
mod := &RequestBodyModifier{
expectedContentType: cfg.ExpectedContentType,
originalContentType: cfg.OriginalContentType,
}
return parse.NewResult(mod, []parse.ModifierType{parse.Request})
}