forked from elastic/apm-agent-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forwarded.go
57 lines (54 loc) · 1.17 KB
/
forwarded.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
package apmhttputil
import (
"strconv"
"strings"
)
// ForwardedHeader holds information extracted from a "Forwarded" HTTP header.
type ForwardedHeader struct {
For string
Host string
Proto string
}
// ParseForwarded parses a "Forwarded" HTTP header.
func ParseForwarded(f string) ForwardedHeader {
// We only consider the first value in the sequence,
// if there are multiple. Disregard everything after
// the first comma.
if comma := strings.IndexRune(f, ','); comma != -1 {
f = f[:comma]
}
var result ForwardedHeader
for f != "" {
field := f
if semi := strings.IndexRune(f, ';'); semi != -1 {
field = f[:semi]
f = f[semi+1:]
} else {
f = ""
}
eq := strings.IndexRune(field, '=')
if eq == -1 {
// Malformed field, ignore.
continue
}
key := strings.TrimSpace(field[:eq])
value := strings.TrimSpace(field[eq+1:])
if len(value) > 0 && value[0] == '"' {
var err error
value, err = strconv.Unquote(value)
if err != nil {
// Malformed, ignore
continue
}
}
switch strings.ToLower(key) {
case "for":
result.For = value
case "host":
result.Host = value
case "proto":
result.Proto = value
}
}
return result
}