/
request_info.go
90 lines (78 loc) · 2.31 KB
/
request_info.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 http_ns
import (
"net"
"strings"
"time"
"github.com/inoxlang/inox/internal/core"
"github.com/oklog/ulid/v2"
)
// should not contain super sensitive information (full cookie, email address, ...)
type IncomingRequestInfo struct {
ULID ulid.ULID
Path core.Path
Hostname string
Method string
//VMethod VMethod
//ResourceTypeName ModelName
ContentType string
RemoteAddrAndPort string
Referer string
URL core.URL
UserAgent string
ResponseStatusCode int
//TimeZone string
//UserLang string
CreationTime time.Time
EndTime time.Time //rename ?
HandlingDurationMillis int64
SessionCookieStart string
CookieNames []string
Errors []string
Info []string
SetCookieHeaderValueStart string
HeaderNames []string
SeeOtherRedirectURL string
ResourceWaitingDurationMicrosec int64
SentBodyBytes int
}
func (reqInfo IncomingRequestInfo) RemoteIpAddress() string {
ip, _, _ := net.SplitHostPort(reqInfo.RemoteAddrAndPort)
return ip
}
func NewIncomingRequestInfo(r *Request) *IncomingRequestInfo {
req := r.request
var cookieNames []string
cookies := req.Cookies()
for _, cookie := range cookies {
cookieNames = append(cookieNames, cookie.Name)
}
now := time.Now()
headerNames := make([]string, 0, len(req.Header))
for name, _ := range req.Header {
headerNames = append(headerNames, name)
}
hostname, _, err := net.SplitHostPort(req.Host)
if err != nil {
if strings.Contains(err.Error(), "missing port") {
hostname = req.Host
} else {
hostname = "failed-to-split-host-port"
}
}
return &IncomingRequestInfo{
ULID: ulid.Make(),
Path: r.Path,
Hostname: hostname,
Method: string(r.Method),
ContentType: req.Header.Get("Content-Type"),
RemoteAddrAndPort: req.RemoteAddr,
Referer: req.Header.Get("Referer"),
URL: r.URL,
UserAgent: req.Header.Get("User-Agent"),
CookieNames: cookieNames,
CreationTime: now,
//TimeZone: timezone,
//UserLang: lang,
HeaderNames: headerNames,
}
}