-
Notifications
You must be signed in to change notification settings - Fork 352
/
server.go
53 lines (48 loc) · 1.03 KB
/
server.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 httputil
import (
"net"
"net/http"
"strings"
)
func HostOnly(hostname string) string {
if strings.Contains(hostname, ":") {
host, _, _ := net.SplitHostPort(hostname)
return host
}
return hostname
}
func HostsOnly(hostname []string) []string {
ret := make([]string, len(hostname))
for i := 0; i < len(hostname); i++ {
ret[i] = HostOnly(hostname[i])
}
return ret
}
func HostMatches(r *http.Request, hosts []string) bool {
host := HostOnly(r.Host)
vHost := HostsOnly(hosts)
for _, v := range vHost {
if v == host {
return true
}
}
return false
}
func HostSubdomainOf(r *http.Request, hosts []string) bool {
host := HostOnly(r.Host)
subVHost := HostsOnly(hosts)
for i := 0; i < len(subVHost); i++ {
subVHost[i] = "." + subVHost[i]
}
for _, subV := range subVHost {
if !strings.HasSuffix(host, subV) || len(host) < len(subV)+1 {
continue
}
dot := strings.IndexRune(host, '.')
if dot > -1 && dot < len(host)-len(subV) {
continue
}
return true // it is a direct sub-domain
}
return false
}