-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests.go
More file actions
310 lines (254 loc) · 7.18 KB
/
requests.go
File metadata and controls
310 lines (254 loc) · 7.18 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package ipgeolocation
import (
"net/textproto"
"strings"
)
type LookupRequest struct {
IP string
Lang string
Include []string
Fields []string
Excludes []string
UserAgent string
Headers map[string]string
Output ResponseFormat
}
type BulkLookupRequest struct {
IPs []string
Lang string
Include []string
Fields []string
Excludes []string
UserAgent string
Headers map[string]string
Output ResponseFormat
}
type normalizedLookupRequest struct {
IP string
Lang string
Include []string
Fields []string
Excludes []string
UserAgent string
Headers map[string]string
Output ResponseFormat
}
type normalizedBulkLookupRequest struct {
IPs []string
Lang string
Include []string
Fields []string
Excludes []string
UserAgent string
Headers map[string]string
Output ResponseFormat
}
func (r *LookupRequest) Validate() error {
_, err := normalizeLookupRequest(r)
return err
}
func (r *BulkLookupRequest) Validate() error {
_, err := normalizeBulkLookupRequest(r)
return err
}
func normalizeLookupRequest(request *LookupRequest) (normalizedLookupRequest, error) {
if request == nil {
return normalizedLookupRequest{
Headers: map[string]string{},
Output: ResponseFormatJSON,
}, nil
}
output, err := normalizeResponseFormat(request.Output)
if err != nil {
return normalizedLookupRequest{}, err
}
ip, err := normalizeLookupIP(request.IP)
if err != nil {
return normalizedLookupRequest{}, err
}
lang, err := normalizeLanguage(request.Lang)
if err != nil {
return normalizedLookupRequest{}, err
}
include, err := normalizeStringList(request.Include, "include")
if err != nil {
return normalizedLookupRequest{}, err
}
fields, err := normalizeStringList(request.Fields, "fields")
if err != nil {
return normalizedLookupRequest{}, err
}
excludes, err := normalizeStringList(request.Excludes, "excludes")
if err != nil {
return normalizedLookupRequest{}, err
}
userAgent, err := normalizeOptionalHeaderValue(request.UserAgent, "userAgent")
if err != nil {
return normalizedLookupRequest{}, err
}
headers, err := normalizeHeaders(request.Headers)
if err != nil {
return normalizedLookupRequest{}, err
}
return normalizedLookupRequest{
IP: ip,
Lang: lang,
Include: include,
Fields: fields,
Excludes: excludes,
UserAgent: userAgent,
Headers: headers,
Output: output,
}, nil
}
func normalizeBulkLookupRequest(request *BulkLookupRequest) (normalizedBulkLookupRequest, error) {
if request == nil {
return normalizedBulkLookupRequest{}, &ValidationError{Message: "bulk lookup request is required"}
}
output, err := normalizeResponseFormat(request.Output)
if err != nil {
return normalizedBulkLookupRequest{}, err
}
ips, err := normalizeIPList(request.IPs)
if err != nil {
return normalizedBulkLookupRequest{}, err
}
lang, err := normalizeLanguage(request.Lang)
if err != nil {
return normalizedBulkLookupRequest{}, err
}
include, err := normalizeStringList(request.Include, "include")
if err != nil {
return normalizedBulkLookupRequest{}, err
}
fields, err := normalizeStringList(request.Fields, "fields")
if err != nil {
return normalizedBulkLookupRequest{}, err
}
excludes, err := normalizeStringList(request.Excludes, "excludes")
if err != nil {
return normalizedBulkLookupRequest{}, err
}
userAgent, err := normalizeOptionalHeaderValue(request.UserAgent, "userAgent")
if err != nil {
return normalizedBulkLookupRequest{}, err
}
headers, err := normalizeHeaders(request.Headers)
if err != nil {
return normalizedBulkLookupRequest{}, err
}
return normalizedBulkLookupRequest{
IPs: ips,
Lang: lang,
Include: include,
Fields: fields,
Excludes: excludes,
UserAgent: userAgent,
Headers: headers,
Output: output,
}, nil
}
func normalizeLookupIP(value string) (string, error) {
normalized := strings.TrimSpace(value)
if value != "" && normalized == "" {
return "", &ValidationError{Message: "ip must not be blank"}
}
if normalized == "" {
return "", nil
}
if strings.ContainsAny(normalized, "\r\n") {
return "", &ValidationError{Message: "ip must not contain CR or LF"}
}
return normalized, nil
}
func normalizeIPList(values []string) ([]string, error) {
if values == nil {
return nil, &ValidationError{Message: "ips must contain at least one IP address or domain"}
}
if len(values) == 0 {
return nil, &ValidationError{Message: "ips must contain at least one IP address or domain"}
}
if len(values) > 50000 {
return nil, &ValidationError{Message: "ips must not contain more than 50000 entries"}
}
result := make([]string, 0, len(values))
for _, value := range values {
normalized, err := normalizeLookupIP(value)
if err != nil {
return nil, err
}
if normalized == "" {
return nil, &ValidationError{Message: "ips must not contain blank values"}
}
result = append(result, normalized)
}
return result, nil
}
func normalizeLanguage(value string) (string, error) {
normalized := strings.TrimSpace(value)
if value != "" && normalized == "" {
return "", &ValidationError{Message: "lang must not be blank"}
}
if normalized == "" {
return "", nil
}
switch strings.ToLower(normalized) {
case "en", "de", "ru", "ja", "fr", "cn", "es", "cs", "it", "ko", "fa", "pt":
return strings.ToLower(normalized), nil
default:
return "", &ValidationError{Message: "lang must be one of: en, de, ru, ja, fr, cn, es, cs, it, ko, fa, pt"}
}
}
func normalizeStringList(values []string, fieldName string) ([]string, error) {
if len(values) == 0 {
return nil, nil
}
seen := map[string]struct{}{}
result := make([]string, 0, len(values))
for _, value := range values {
normalized := strings.TrimSpace(value)
if normalized == "" {
return nil, &ValidationError{Message: fieldName + " must not contain blank values"}
}
if _, ok := seen[normalized]; ok {
continue
}
seen[normalized] = struct{}{}
result = append(result, normalized)
}
return result, nil
}
func normalizeOptionalHeaderValue(value string, fieldName string) (string, error) {
normalized := strings.TrimSpace(value)
if value != "" && normalized == "" {
return "", &ValidationError{Message: fieldName + " must not be blank"}
}
if normalized == "" {
return "", nil
}
if strings.ContainsAny(normalized, "\r\n") {
return "", &ValidationError{Message: fieldName + " must not contain CR or LF"}
}
return normalized, nil
}
func normalizeHeaders(headers map[string]string) (map[string]string, error) {
if len(headers) == 0 {
return map[string]string{}, nil
}
result := make(map[string]string, len(headers))
for key, value := range headers {
name := textproto.CanonicalMIMEHeaderKey(strings.TrimSpace(key))
if name == "" {
return nil, &ValidationError{Message: "headers must not contain blank names"}
}
normalizedValue := strings.TrimSpace(value)
if normalizedValue == "" {
return nil, &ValidationError{Message: "headers must not contain blank values"}
}
if strings.ContainsAny(name, "\r\n") || strings.ContainsAny(normalizedValue, "\r\n") {
return nil, &ValidationError{Message: "headers must not contain CR or LF"}
}
result[name] = normalizedValue
}
return result, nil
}