-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.go
More file actions
53 lines (45 loc) · 1.54 KB
/
input.go
File metadata and controls
53 lines (45 loc) · 1.54 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
package clientip
import "context"
// HeaderValues provides access to request header values by name.
//
// Implementations should return one slice entry per received header line.
// Single-IP sources rely on per-line values to detect duplicates, and chain
// sources preserve wire order across repeated lines. Do not merge duplicate
// header lines into one comma-joined value.
//
// Header names are requested in canonical MIME format (for example
// "X-Forwarded-For").
//
// net/http's http.Header satisfies this interface directly.
type HeaderValues interface {
Values(name string) []string
}
// HeaderValuesFunc adapts a function to the HeaderValues interface.
type HeaderValuesFunc func(name string) []string
// Values implements HeaderValues.
func (f HeaderValuesFunc) Values(name string) []string {
if f == nil {
return nil
}
return f(name)
}
// Input provides framework-agnostic request data for extraction.
//
// Context defaults to context.Background() when nil.
//
// For Headers, preserve repeated header lines as separate values for each
// header name (for example two X-Forwarded-For lines should yield a slice with
// length 2, and two X-Real-IP lines should also yield length 2). A nil Headers
// provider makes header-based sources unavailable; SourceRemoteAddr can still
// run if it is included with WithSources.
type Input struct {
Context context.Context
RemoteAddr string
Headers HeaderValues
}
func requestInputContext(input Input) context.Context {
if input.Context == nil {
return context.Background()
}
return input.Context
}