-
Notifications
You must be signed in to change notification settings - Fork 1
/
subdomain.go
100 lines (86 loc) · 2.33 KB
/
subdomain.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
package subdomain
import (
"net/http"
"strings"
"github.com/go-chi/chi"
"github.com/go-chi/hostrouter"
)
//Routes embeds github.com/go-chi/hostrouter's Routes
type Routes struct{ hostrouter.Routes }
//New creates an instance
func New() Routes {
return Routes{hostrouter.New()}
}
//Map maps subdomains to their routers
func (sr Routes) Map(subdomain string, h chi.Router) {
subdomain = strings.TrimSuffix(subdomain, ".")
sr.Routes.Map(subdomain, h)
}
//ServeHTTP implements the http.Handler interface
func (sr Routes) ServeHTTP(w http.ResponseWriter, r *http.Request) {
host := requestHost(r)
for subdomain, router := range sr.Routes {
if strings.HasPrefix(host, subdomain) && isSingleSubLevel(subdomain, host) {
router.ServeHTTP(w, r)
return
}
}
if router, ok := sr.Routes["*"]; ok {
router.ServeHTTP(w, r)
return
}
http.NotFound(w, r)
}
//make sure the subdomain is directly after the main hostname.
//or as defined by the user (default is 2 levels for normal domains
//and 1 level for localhost)
//e.g. sub.example.com ✔ sub.examploo.example.com ❌
func isSingleSubLevel(subdomain string, host string) bool {
subDomainLevels := strings.Count(subdomain, ".")
hostLevels := strings.Count(host, ".")
if ((subDomainLevels+1) == hostLevels && host == "localhost") || (subDomainLevels+2) == hostLevels {
return true
}
return false
}
//Copyright (c) 2016-Present https://github.com/go-chi authors
// MIT License
func requestHost(r *http.Request) (host string) {
// not standard, but most popular
host = r.Header.Get("X-Forwarded-Host")
if host != "" {
return
}
// RFC 7239
host = r.Header.Get("Forwarded")
_, _, host = parseForwarded(host)
if host != "" {
return
}
// if all else fails fall back to request host
host = r.Host
return
}
//Copyright (c) 2016-Present https://github.com/go-chi authors
// MIT License
func parseForwarded(forwarded string) (addr, proto, host string) {
if forwarded == "" {
return
}
for _, forwardedPair := range strings.Split(forwarded, ";") {
if tv := strings.SplitN(forwardedPair, "=", 2); len(tv) == 2 {
token, value := tv[0], tv[1]
token = strings.TrimSpace(token)
value = strings.TrimSpace(strings.Trim(value, `"`))
switch strings.ToLower(token) {
case "for":
addr = value
case "proto":
proto = value
case "host":
host = value
}
}
}
return
}