-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreaders.go
206 lines (171 loc) · 5.55 KB
/
readers.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package shuttle
import (
"net/http"
"strings"
)
type acceptReader struct {
acceptable map[string][]string
result any
useDefaultIfAcceptNotFound bool
maxAcceptTypes int
monitor Monitor
}
func newAcceptReader(serializerFactories map[string]func() Serializer, result *TextResult, useDefaultIfAcceptNotFound bool, maxAcceptTypes int, monitor Monitor) Reader {
acceptable := make(map[string][]string)
for acceptType := range serializerFactories {
acceptable[acceptType] = []string{acceptType}
}
return &acceptReader{
acceptable: acceptable,
result: result,
useDefaultIfAcceptNotFound: useDefaultIfAcceptNotFound,
maxAcceptTypes: maxAcceptTypes,
monitor: monitor,
}
}
func (this *acceptReader) Read(_ InputModel, request *http.Request) any {
if normalized, found := this.findAcceptType(request.Header[headerAccept]); !found {
this.monitor.NotAcceptable()
return this.result
} else {
request.Header[headerAccept] = normalized
}
return nil
}
func (this *acceptReader) findAcceptType(acceptTypes []string) ([]string, bool) {
if len(acceptTypes) == 0 {
return nil, true
}
for _, value := range acceptTypes {
var item string
acceptTypesIndex := 0
for {
index := strings.Index(value, ",")
if index >= 0 {
item = value[0:index]
value = value[index+1:]
} else {
item = value
}
item = normalizeMediaType(item)
if item == headerAcceptAnyValue {
return nil, true // default
} else if types, contains := this.acceptable[item]; contains {
return types, true
} else if this.maxAcceptTypes > -1 && this.maxAcceptTypes <= acceptTypesIndex+1 {
return nil, true
} else if index == -1 {
break
}
acceptTypesIndex++
}
}
if this.useDefaultIfAcceptNotFound {
return nil, true
}
return nil, false
}
func normalizeMediaType(value string) string {
if index := strings.Index(value, ";"); index >= 0 {
value = value[0:index]
}
return strings.TrimSpace(value)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type deserializeReader struct {
available map[string]Deserializer
unsupportedMediaTypeResult any
result ResultContainer
monitor Monitor
}
func newDeserializeReader(deserializerFactories map[string]func() Deserializer, unsupportedMediaTypeResult any, result ResultContainer, monitor Monitor) Reader {
available := make(map[string]Deserializer, len(deserializerFactories))
for contentType, factory := range deserializerFactories {
available[contentType] = factory()
}
return &deserializeReader{
available: available,
unsupportedMediaTypeResult: unsupportedMediaTypeResult,
result: result,
monitor: monitor,
}
}
func (this *deserializeReader) Read(input InputModel, request *http.Request) any {
this.monitor.Deserialize()
var target any = input
if value, ok := input.(DeserializeBody); ok {
target = value.Body()
}
if deserializer := this.loadDeserializer(request.Header[headerContentType]); deserializer == nil {
this.monitor.UnsupportedMediaType()
return this.unsupportedMediaTypeResult
} else if err := deserializer.Deserialize(target, request.Body); err != nil {
this.monitor.DeserializeFailed()
this.result.SetContent(err) // implementations of this may override and no-op SetContent
return this.result.Result()
}
return nil
}
func (this *deserializeReader) loadDeserializer(contentTypes []string) Deserializer {
for _, contentType := range contentTypes {
if deserializer, contains := this.available[normalizeMediaType(contentType)]; contains {
return deserializer
}
}
return nil
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type parseFormReader struct {
result any
monitor Monitor
}
func newParseFormReader(result any, monitor Monitor) Reader {
return &parseFormReader{
result: result,
monitor: monitor,
}
}
func (this *parseFormReader) Read(_ InputModel, request *http.Request) any {
this.monitor.ParseForm()
if err := request.ParseForm(); err != nil {
this.monitor.ParseFormFailed(err)
return this.result
}
return nil
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type bindReader struct {
result ResultContainer
monitor Monitor
}
func newBindReader(result ResultContainer, monitor Monitor) Reader {
return &bindReader{result: result, monitor: monitor}
}
func (this *bindReader) Read(target InputModel, request *http.Request) any {
this.monitor.Bind()
if err := target.Bind(request); err != nil {
this.monitor.BindFailed(err)
this.result.SetContent(err)
return this.result.Result()
}
return nil
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type validateReader struct {
result ResultContainer
buffer []error
monitor Monitor
}
func newValidateReader(result ResultContainer, bufferSize int, monitor Monitor) Reader {
return &validateReader{result: result, buffer: make([]error, bufferSize), monitor: monitor}
}
func (this *validateReader) Read(target InputModel, _ *http.Request) any {
this.monitor.Validate()
if count := target.Validate(this.buffer); count > 0 {
errs := this.buffer[0:count]
this.monitor.ValidateFailed(errs)
this.result.SetContent(errs)
return this.result.Result()
}
return nil
}