-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.go
83 lines (63 loc) · 1.7 KB
/
match.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
package web_container_proxy
// 处理host 以及 URl
import (
"fmt"
"log"
"net/http"
"strings"
)
func matchingHandlerOf(url, hostname string, handlers Handlers) (result http.Handler, found bool) {
if handlers == nil {
return nil, false
}
for pattern, handler := range handlers {
if pattern == "*" {
continue
}
if len(url) >= len(pattern) && url[0:len(pattern)] == pattern {
found = true
result = http.StripPrefix(pattern, handler.server)
}
}
if handler, hasDefaultHandler := handlers["*"]; !found && hasDefaultHandler {
result = handler.server
found = true
}
return result, found
}
func matchingServerOf(host, url string) (result http.Handler, found bool) {
log.Println(" url:" + url + "host:" + host)
hostname := hostnameOf(host)
wildcard := wildcardOf(hostname)
result, found = matchingHandlerOf(url, hostname, sites[hostname])
if !found {
if _, hasWildcard := sites[wildcard]; hasWildcard {
result, found = matchingHandlerOf(url, hostname, sites[wildcard])
} else {
log.Println("[INFO] Handler is found")
}
}
if wildcardSite, hasWildcardSite := sites["*"]; !found && hasWildcardSite {
result, found = matchingHandlerOf(url, hostname, wildcardSite)
} else if !found {
log.Println("[INFO] Handler is found")
} else {
log.Println("[INFO] Handler is null")
}
return result, found
}
func hostnameOf(host string) string {
hostname := strings.Split(host, ":")[0]
if len(hostname) > 4 && hostname[0:4] == "www." {
hostname = hostname[4:]
}
return hostname
}
func wildcardOf(hostname string) string {
parts := strings.Split(hostname, ".")
if len(parts) < 3 {
return fmt.Sprintf("*.%s", hostname)
}
parts[0] = "*"
return strings.Join(parts, ".")
}