-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurls.go
112 lines (84 loc) · 1.55 KB
/
urls.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
package main
import (
log "github.com/sirupsen/logrus"
"net/url"
"strings"
)
// Redirect struct
type Redirect struct {
Schema string
Host string
Port string
}
// NewRedirect - creates new Redirect from redirect URL
func NewRedirect(raw string) *Redirect {
u := parseRedirectURL(raw)
u.Scheme = getScheme(u)
port := getPort(u)
host := getHost(u)
return &Redirect{
Schema: u.Scheme,
Host: host,
Port: port,
}
}
// GetHost - get full hostname to redirect URL to
func (r *Redirect) GetHost(u *url.URL) string {
host := r.Host
if host == "" {
host = u.Hostname()
}
s := r.Schema + "://" + host
if r.Port != "" {
s += ":" + r.Port
}
return s
}
func parseRedirectURL(raw string) *url.URL {
if strings.Index(raw, "://") < 0 {
redirectURL := &url.URL{}
redirectURL.Host = raw
if strings.Index(raw, "/") < 0 {
return redirectURL
}
redirectURL.Host = raw[0:strings.Index(raw, "/")]
return redirectURL
}
redirectURL, err := url.Parse(raw)
if err != nil {
log.Fatal(err)
}
return redirectURL
}
func getScheme(u *url.URL) string {
port := u.Port()
scheme := u.Scheme
if scheme != "" {
return scheme
}
if "443" == port {
return "https"
}
if port == "80" {
return "http"
}
return "http"
}
func getPort(u *url.URL) string {
port := u.Port()
scheme := u.Scheme
if scheme == "https" && port == "443" {
return ""
}
if scheme == "http" && port == "80" {
port = ""
}
return port
}
func getHost(u *url.URL) string {
host := u.Hostname()
if host == "REQUEST_HOST" {
host = ""
}
return host
}