-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation_body.go
168 lines (141 loc) · 3.89 KB
/
operation_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package inreq
import (
"encoding"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
"mime"
"net/http"
"reflect"
)
var (
textUnmarshalerType = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()
)
// DecodeOperationBody is a DecodeOperation that reads values from the request body.
type DecodeOperationBody struct {
}
func (d *DecodeOperationBody) Decode(ctx DecodeContext, r *http.Request, isList bool, field reflect.Value,
tag *Tag) (bool, any, error) {
if r.Body == nil {
return false, nil, nil
}
if !ctx.AllowReadBody() {
return false, nil, errors.New("body operation not allowed")
}
if ctx.IsBodyDecoded() {
return false, nil, fmt.Errorf("body was already decoded")
}
return decodeBody(ctx, r, field, tag)
}
func decodeBodyReadData(ctx DecodeContext, r *http.Request) (bool, []byte, error) {
if ctx.IsBodyDecoded() {
return true, nil, fmt.Errorf("body was already decoded")
}
ctx.DecodedBody() // signal that the body was decoded
b, err := io.ReadAll(r.Body)
if err != nil {
return true, nil, err
}
defer r.Body.Close()
if len(b) == 0 {
return false, nil, nil
}
return true, b, nil
}
func decodeBodyRaw(ctx DecodeContext, r *http.Request, field reflect.Value) (bool, bool, any, error) {
// check for raw data
if field.Type().PkgPath() == "" { // only if predeclared type (ignores for example "type IP []byte".)
switch field.Type().Kind() {
case reflect.String:
rfound, rvalue, rerr := decodeBodyReadData(ctx, r)
return true, rfound, string(rvalue), rerr
case reflect.Slice:
if field.Type().Elem().Kind() == reflect.Uint8 {
rfound, rvalue, rerr := decodeBodyReadData(ctx, r)
return true, rfound, rvalue, rerr
}
}
}
return false, false, nil, nil
}
func decodeBody(ctx DecodeContext, r *http.Request, field reflect.Value, tag *Tag) (bool, any, error) {
// check for raw data
rfound, found, data, err := decodeBodyRaw(ctx, r, field)
if rfound {
return found, data, err
}
// decode into struct field
fv := field
if fv.CanAddr() {
fv = fv.Addr()
}
found, data, err = ctx.BodyDecoder().Unmarshal(ctx, tag.Options.Value("type", ""),
r, fv.Interface())
if found {
return found, data, err
}
// try known interfaces
if reflect.PointerTo(field.Type()).Implements(textUnmarshalerType) {
// encoding.TextUnmarshaler
rfound, rvalue, rerr := decodeBodyReadData(ctx, r)
if rfound {
if err != nil {
return rfound, rvalue, rerr
}
xtarget := reflect.New(field.Type())
um := xtarget.Interface().(encoding.TextUnmarshaler)
if err := um.UnmarshalText(rvalue); err != nil {
return true, nil, err
}
field.Set(xtarget.Elem())
return true, IgnoreDecodeValue, nil
}
}
return false, nil, nil
}
// defaultBodyDecoder decodes JSON and XML to structs.
type defaultBodyDecoder struct {
}
func NewDefaultBodyDecoder() BodyDecoder {
return &defaultBodyDecoder{}
}
func (d defaultBodyDecoder) Unmarshal(ctx DecodeContext, typeParam string, r *http.Request, data any) (bool, any, error) {
var mediaType string
if typeParam != "" {
switch typeParam {
case "json":
mediaType = "application/json"
case "xml":
mediaType = "application/xml"
}
}
if mediaType == "" {
var err error
contentType := r.Header.Get("Content-Type")
if contentType != "" {
mediaType, _, err = mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
return false, nil, fmt.Errorf("error detecting body content type: %w", err)
}
}
}
switch mediaType {
case "application/json":
ctx.DecodedBody()
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
return true, nil, fmt.Errorf("error parsing JSON body: %w", err)
}
return true, IgnoreDecodeValue, nil
case "text/xml", "application/xml":
ctx.DecodedBody()
err := xml.NewDecoder(r.Body).Decode(&data)
if err != nil {
return true, nil, fmt.Errorf("error parsing XML body: %w", err)
}
return true, IgnoreDecodeValue, nil
}
return false, nil, nil
}