-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
90 lines (78 loc) · 1.78 KB
/
request.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
package support
import (
"github.com/echocat/slf4g/fields"
"net/http"
"strings"
)
var headerNewlineToSpace = strings.NewReplacer("\n", " ", "\r", " ")
func NormalizeHeaderContent(val string) string {
return headerNewlineToSpace.Replace(val)
}
func RequestIdOfRequest(req *http.Request) string {
if x := req.Header.Get("X-Request-ID"); len(x) > 0 && len(x) <= 256 {
if len(x) > 256 {
return x[:255]
}
return x
}
return ""
}
func CorrelationIdOfRequest(req *http.Request) any {
if x := req.Header.Get("X-Correlation-ID"); len(x) > 0 && len(x) <= 256 {
if len(x) > 256 {
return x[:255]
}
return x
}
return ""
}
func HostOfRequest(req *http.Request) any {
if x := req.Header.Get("X-Forwarded-Host"); len(x) > 0 {
return x
}
return req.Host
}
func RemoteIpOfRequest(req *http.Request) any {
if x := req.Header.Get("X-Forwarded-For"); len(x) > 0 {
return x
}
if x := req.Header.Get("X-Real-IP"); len(x) > 0 {
return x
}
remote := req.RemoteAddr
ld := strings.LastIndexByte(remote, ':')
if ld > 0 {
remote = remote[:ld]
}
return remote
}
func UriOfRequest(req *http.Request) any {
if x := req.Header.Get("X-Original-URI"); len(x) > 0 {
return x
}
result := req.RequestURI
if x := req.Header.Get("X-Forwarded-Prefix"); len(x) > 0 {
return x + result
}
return result
}
func UserAgentOfRequest(req *http.Request) string {
if x := req.Header.Get("User-Agent"); len(x) > 0 {
if len(x) > 256 {
return x[:255]
}
return x
}
return ""
}
func UserAgentOfRequestAny(req *http.Request) any {
return UserAgentOfRequest(req)
}
func PathOfRequest(req *http.Request) string {
return req.URL.Path
}
func RequestBasedLazyFor(req *http.Request, delegate func(*http.Request) any) fields.Lazy {
return fields.LazyFunc(func() interface{} {
return delegate(req)
})
}