forked from EverythingMe/vertex
/
request.go
170 lines (133 loc) · 3.74 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
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
package vertex
import (
"fmt"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/dvirsky/go-pylog/logging"
"code.google.com/p/go-uuid/uuid"
"golang.org/x/text/language"
)
// Request wraps the standard http request object with higher level contextual data
type Request struct {
*http.Request
StartTime time.Time
Deadline time.Time
Locale string
UserAgent string
RemoteIP string
Location struct{ Lat, Long float64 }
RequestId string
Callback string
Secure bool
attributes map[string]interface{}
}
func (r *Request) String() string {
return fmt.Sprintf("Request/%s", r.RequestId)
}
func (r *Request) SetAttribute(key string, val interface{}) {
r.attributes[key] = val
}
func (r *Request) Attribute(key string) (interface{}, bool) {
v, found := r.attributes[key]
return v, found
}
// IsLocal returns true if a request is coming from localhost
func (r *Request) IsLocal() bool {
// TODO: a real check here
return r.RemoteIP == "127.0.0.1" || r.RemoteIP == "::1/128"
}
const DefaultLocale = "en-US"
const HeaderGeoPosition = "X-LatLong"
// parse the locale based on Accept-Language header. If no header found or the values are invalid,
// we fall back to en-US
func (r *Request) parseLocale() {
tags, _, err := language.ParseAcceptLanguage(r.Header.Get("Accept-Language"))
if err != nil {
logging.Warning("Could not parse accept lang header: %s", err)
return
}
if len(tags) > 0 {
logging.Debug("Locale for request: %s", tags[0])
r.Locale = tags[0].String()
}
}
// parse the client address, based on http headers or the actual ip
func (r *Request) parseAddr() {
// the default is the originating ip. but we try to find better options because this is almost
// never the right IP
if parts := strings.Split(r.Request.RemoteAddr, ":"); len(parts) == 2 {
r.RemoteIP = parts[0]
}
// If we have a forwarded-for header, take the address from there
if xff := strings.Trim(r.Header.Get("X-Forwarded-For"), ","); len(xff) > 0 {
addrs := strings.Split(xff, ",")
lastFwd := addrs[len(addrs)-1]
if ip := net.ParseIP(lastFwd); ip != nil {
logging.Debug("Setting IP based on XFF header to %s", ip)
r.RemoteIP = ip.String()
}
} else if xri := r.Header.Get("X-Real-Ip"); len(xri) > 0 {
if ip := net.ParseIP(xri); ip != nil {
logging.Debug("Setting IP based on XRI header to %s", ip)
r.RemoteIP = ip.String()
}
}
logging.Debug("Request ip: %s", r.RemoteIP)
}
// Parse our location header
func (r *Request) parseLocation() {
latlong := r.Header.Get(HeaderGeoPosition)
if latlong != "" {
parts := strings.Split(latlong, ",")
if len(parts) == 2 {
if lat, err := strconv.ParseFloat(parts[0], 64); err == nil {
if long, err := strconv.ParseFloat(parts[1], 64); err == nil {
r.Location.Lat = lat
r.Location.Long = long
}
}
}
}
}
// Detect if the request is secure or not, based on either TLS info or http headers/url
func (r *Request) parseSecure() {
logging.Info("Parsing secure. TLS: %v, URI: %s, Headers: %#v", r.TLS, r.RequestURI, r.Header)
if r.TLS != nil {
r.Secure = true
return
}
if u, err := url.ParseRequestURI(r.RequestURI); err == nil {
if u.Scheme == "https" {
r.Secure = true
return
}
}
xfp := r.Header.Get("X-Forwarded-Proto")
if xfp == "" {
xfp = r.Header.Get("X-Scheme")
}
if xfp == "https" {
r.Secure = true
}
}
// NewRequest wraps a new http request with a vertex request
func NewRequest(r *http.Request) *Request {
req := &Request{
Request: r,
StartTime: time.Now(),
Locale: DefaultLocale,
UserAgent: r.UserAgent(),
RequestId: uuid.New(),
Callback: r.FormValue(CallbackParam),
attributes: make(map[string]interface{}),
}
req.parseLocale()
req.parseAddr()
req.parseLocation()
req.parseSecure()
return req
}