Repository navigation
Expand file tree
/
Copy pathrequest.go
More file actions
470 lines (366 loc) · 11 KB
/
Copy pathrequest.go
File metadata and controls
470 lines (366 loc) · 11 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package ehttp
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"sync/atomic"
"go.uber.org/zap"
"github.com/EqualSoftware/ehttp/internal/redact"
)
var ErrRequestBodyTooLarge = errors.New("request body too large")
type Request struct {
Request *http.Request
proxies trustedProxies
peer atomic.Uint32 // peerUnknown, peerTrusted or peerUntrusted; the result is the same in every goroutine
flags atomic.Uint32 // flagForwardedWarned and flagPreflightHandled; one word keeps Request in its size class
res *ResponseWriter // for Vary; nil in a Request built by hand
logger *zap.Logger // the server logger; nil in a Request built by hand
log atomic.Pointer[zap.Logger] // logger with the request fields, built on first use
}
const (
// flagForwardedWarned is set after the warning for an invalid forwarded header.
flagForwardedWarned uint32 = 1 << iota
// flagPreflightHandled is set when the Cors middleware ran, or when the router
// refused the request itself. The Development warning for a preflight that
// was refused before Cors reads it.
flagPreflightHandled
)
var nopLogger = zap.NewNop()
// Logger returns the logger of the server with the fields request_id, method
// and route (the registered pattern). A field with an empty value is left
// out. The first call caches the logger, so a middleware that adds request
// data (for example RequestId) must run before the first call. It is safe
// to call from many goroutines.
func (req *Request) Logger() *zap.Logger {
if l := req.log.Load(); l != nil {
return l
}
req.log.CompareAndSwap(nil, req.newLogger())
return req.log.Load()
}
// baseLogger returns the server logger, or a no-op logger.
func (req *Request) baseLogger() *zap.Logger {
if req.logger == nil {
return nopLogger
}
return req.logger
}
// quoteField is quoteValue for the value of the header name, with the deny
// list of the server (the default one if the request has no response).
func (req *Request) quoteField(name, value string) string {
var m *redact.Matcher
if req.res != nil {
m = req.res.redact
}
if m.Sensitive(name) {
return redact.Mask
}
return quoteValue(value)
}
// newLogger builds the logger of the request and does not cache it. The
// library uses it, so its own log calls never freeze the fields before a
// middleware like RequestId has run.
func (req *Request) newLogger() *zap.Logger {
fields := make([]zap.Field, 0, 3)
if id := req.Header(HeaderXRequestID); id != "" {
fields = append(fields, zap.String("request_id", id))
}
fields = append(fields, zap.String("method", req.Method()))
if route := req.RoutePath(); route != "" {
fields = append(fields, zap.String("route", route))
}
return req.baseLogger().With(fields...)
}
func (req *Request) WithContext(ctx context.Context) *Request {
req.Request = req.Request.WithContext(ctx)
return req
}
func (req *Request) Context() context.Context {
return req.Request.Context()
}
func (req *Request) Method() string {
return req.Request.Method
}
func (req *Request) RoutePath() string {
return req.Request.Pattern
}
// Protocol returns "https" for a TLS connection and "http" otherwise.
// A trusted proxy can set it with X-Forwarded-Proto (see WithTrustedProxies).
func (req *Request) Protocol() string {
if v := req.forwardedProto(); v != "" {
return v
}
if req.Request.TLS != nil {
return "https"
}
return "http"
}
// Cookie returns the value of the cookie name, or "" if it is absent.
func (req *Request) Cookie(name string) string {
c, err := req.Request.Cookie(name)
if err != nil {
return ""
}
return c.Value
}
// Host returns the Host header. A trusted proxy can set it with
// X-Forwarded-Host (see WithTrustedProxies).
func (req *Request) Host() string {
if host := req.forwardedHost(); host != "" {
return host
}
return req.Request.Host
}
// Hostname returns Host without the port, in lowercase (RFC 3986 §3.2.2).
func (req *Request) Hostname() string {
return strings.ToLower(hostOnly(req.Host()))
}
func (req *Request) Path() string {
return req.Request.URL.EscapedPath()
}
func (req *Request) Query(name string) string {
return req.Request.URL.Query().Get(name)
}
func (req *Request) Header(name string) string {
return req.Request.Header.Get(name)
}
func (req *Request) Origin() string {
return req.Header(HeaderOrigin)
}
func (req *Request) UserAgent() string {
return req.Header(HeaderUserAgent)
}
// Param returns a route param. The value is percent-decoded once, per segment
// (RFC 3986 §2.1). When a value has a ".." part (split on "/", literal or
// decoded), the router sends 403 and does not call the handler. A decoded NUL
// or backslash gives 400. A decoded "/" stays as data, and so does a "." part.
// Still confine a wildcard ("...") value before file use, for example with
// os.Root or filepath.IsLocal. Static segments compare the raw, still-escaped
// path, so "/us%65rs" does not match "/users".
func (req *Request) Param(name string) string {
return req.Request.PathValue(name)
}
// Accepts reports if the Accept header allows the media type value. The answer
// depends on Accept, so it adds Accept to Vary (RFC 9110 §12.5.5).
//
// A missing Accept header allows every type. The most specific media range that
// matches value sets its q value, and q=0 means not acceptable (RFC 9110
// §12.4.2). Media type parameters in the header are ignored. An invalid header
// or an invalid value is never acceptable, so the caller answers with 406.
func (req *Request) Accepts(value string) bool {
if req.res != nil {
req.res.varyQuiet(HeaderAccept)
}
typ, sub, ok := parseMediaType(value)
if !ok {
req.newLogger().Error(fmt.Sprintf("invalid accepts value '%s'", value))
return false
}
best, seen, ok := req.acceptBest(typ, sub)
if !ok {
return false
}
return !seen || best.q > 0
}
// acceptBest finds the best match of the media type in all Accept lines. seen
// is false if the header has no member. ok is false if the header is invalid.
func (req *Request) acceptBest(typ, sub string) (best acceptMatch, seen, ok bool) {
for _, line := range req.Request.Header.Values(HeaderAccept) {
for rest, more := line, true; more; {
var member string
member, rest, more, ok = cutUnquoted(rest, ',')
member = strings.Trim(member, " \t")
if ok && member == "" {
continue
}
rng, valid := parseAcceptRange(member)
if !ok || !valid {
req.newLogger().Warn("ehttp: invalid Accept header", zap.String("value", req.quoteField(HeaderAccept, line)))
return best, seen, false
}
seen = true
best.add(rng, typ, sub)
}
}
return best, seen, true
}
// cutUnquoted cuts s at the first sep that is not inside a quoted string
// (RFC 9110 §5.6.4). found is true if it cut. ok is false if a quote does not
// end.
func cutUnquoted(s string, sep byte) (before, after string, found, ok bool) {
quoted := false
for i := 0; i < len(s); i++ {
switch c := s[i]; {
case quoted && c == '\\':
i++
case c == '"':
quoted = !quoted
case c == sep && !quoted:
return s[:i], s[i+1:], true, true
}
}
return s, "", false, !quoted
}
// validQuoted reports if s is a quoted-string (RFC 9110 §5.6.4).
func validQuoted(s string) bool {
if len(s) < 2 || s[0] != '"' {
return false
}
for i := 1; i < len(s); i++ {
switch s[i] {
case '"':
return i == len(s)-1
case '\\':
i++
}
}
return false
}
// acceptMatch holds the best match found so far: the specificity of the range
// (0 none, 1 "*/*", 2 "type/*", 3 "type/subtype") and its q value.
type acceptMatch struct {
specificity int
q int
}
func (m *acceptMatch) add(r acceptRange, typ, sub string) {
spec := r.specificity(typ, sub)
switch {
case spec == 0 || spec < m.specificity:
case spec > m.specificity:
m.specificity, m.q = spec, r.q
default:
m.q = max(m.q, r.q)
}
}
// acceptRange is one media range of an Accept header. q is in thousandths.
type acceptRange struct {
typ, sub string
q int
}
func (r acceptRange) specificity(typ, sub string) int {
switch {
case r.typ == "*":
return 1
case !strings.EqualFold(r.typ, typ):
return 0
case r.sub == "*":
return 2
case strings.EqualFold(r.sub, sub):
return 3
}
return 0
}
// parseAcceptRange parses "type/subtype", "type/*" or "*/*" with parameters
// (RFC 9110 §12.5.1). Parameters other than q are checked and ignored. They
// do not count for specificity.
func parseAcceptRange(member string) (acceptRange, bool) {
rng := acceptRange{q: 1000}
head, params, more, ok := cutUnquoted(member, ';')
if !ok {
return rng, false
}
typ, sub, ok := strings.Cut(strings.TrimRight(head, " \t"), "/")
if !ok || !isToken(typ) && typ != "*" || !isToken(sub) && sub != "*" || typ == "*" && sub != "*" {
return rng, false
}
rng.typ, rng.sub = typ, sub
for more {
var p string
p, params, more, ok = cutUnquoted(params, ';')
p = strings.Trim(p, " \t")
if !ok {
return rng, false
}
if p == "" {
continue
}
name, val, ok := strings.Cut(p, "=")
if !ok || !isToken(name) || !isToken(val) && !validQuoted(val) {
return rng, false
}
if name != "q" && name != "Q" {
continue
}
if rng.q, ok = parseQValue(val); !ok {
return rng, false
}
}
return rng, true
}
// parseQValue parses a weight (RFC 9110 §12.4.2) into thousandths.
func parseQValue(s string) (int, bool) {
whole, frac, _ := strings.Cut(s, ".")
if len(frac) > 3 || whole != "0" && whole != "1" {
return 0, false
}
q := int(whole[0]-'0') * 1000
scale := 100
for i := range len(frac) {
d := int(frac[i] - '0')
if d < 0 || d > 9 || whole == "1" && d != 0 {
return 0, false
}
q += d * scale
scale /= 10
}
return q, true
}
// parseMediaType parses "type/subtype" with optional parameters, which it drops.
func parseMediaType(value string) (typ, sub string, ok bool) {
head, _, _ := strings.Cut(value, ";")
typ, sub, ok = strings.Cut(strings.Trim(head, " \t"), "/")
return typ, sub, ok && isToken(typ) && isToken(sub)
}
// isToken reports if s is a non-empty token (RFC 9110 §5.6.2).
func isToken(s string) bool {
if s == "" {
return false
}
for i := range len(s) {
c := s[i]
if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' {
continue
}
if strings.IndexByte("!#$%&'*+-.^_`|~", c) < 0 {
return false
}
}
return true
}
func (req *Request) Body() ([]byte, error) {
b, err := io.ReadAll(req.Request.Body)
if err != nil {
var maxBytesErr *http.MaxBytesError
if errors.As(err, &maxBytesErr) {
return nil, ErrRequestBodyTooLarge
}
return nil, err
}
return b, nil
}
func (req *Request) BodyString() (string, error) {
b, err := io.ReadAll(req.Request.Body)
if err != nil {
var maxBytesErr *http.MaxBytesError
if errors.As(err, &maxBytesErr) {
return "", ErrRequestBodyTooLarge
}
return "", err
}
return string(b), nil
}
func (req *Request) BodyJSON(v interface{}) error {
b, err := io.ReadAll(req.Request.Body)
if err != nil {
var maxBytesErr *http.MaxBytesError
if errors.As(err, &maxBytesErr) {
return ErrRequestBodyTooLarge
}
return err
}
return json.Unmarshal(b, v)
}