-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
53 lines (42 loc) · 896 Bytes
/
util.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
package main
import (
"net"
"net/http"
"strings"
)
func getHost(r *http.Request) string {
host := r.Header.Get("X-Forwarded-Host")
if host == "" {
host = r.Host
}
return host
}
func getIpAddress(r *http.Request) string {
ip := strings.Split(r.Header.Get("X-Forwarded-For"), ",")[0]
if ip == "" {
directIp, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
//LATER: log? try to parse?
ip = directIp
} else {
ip = directIp
}
}
return ip
}
func getFlatHeaders(r *http.Request, prefix string) map[string]string {
result := make(map[string]string)
for name, values := range r.Header {
if strings.HasPrefix(name, prefix) {
result[name] = values[0]
}
}
return result
}
func getHeaders(r *http.Request) map[string][]string {
result := make(map[string][]string)
for name, values := range r.Header {
result[name] = values
}
return result
}