-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuri.go
74 lines (56 loc) · 1.19 KB
/
uri.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
package room
import (
"strings"
)
type URI struct {
scheme string
authority string
path string
query string
}
func (u URI) String() string {
url := u.scheme + "://" + u.authority + u.path
if u.query != "" {
url += "?" + u.query
}
return url
}
func (u URI) Query() string {
return u.query
}
func (u URI) Path() string {
return u.path
}
func (u URI) Authority() string {
return u.authority
}
func (u URI) Scheme() string {
return u.scheme
}
func NewURI(fullUrl string) URI {
uri := URI{}
if strings.Contains(fullUrl, "?") {
splittedURL := strings.SplitN(fullUrl, "?", 2)
fullUrl = splittedURL[0]
// TODO use IStore instead of string
uri.query = splittedURL[1]
}
var urn string
if strings.HasPrefix(fullUrl, "https://") {
uri.scheme = "https"
urn = strings.TrimPrefix(fullUrl, "https://")
} else {
uri.scheme = "http"
urn = strings.TrimPrefix(fullUrl, "http://")
}
if len(urn) > 0 && urn[len(urn)-1] != '/' {
urn = urn + "/"
}
splittedURN := strings.SplitN(urn, "/", 2)
uri.authority = splittedURN[0]
uri.path = "/" + splittedURN[1]
if len(uri.path) > 0 && uri.path[len(uri.path)-1] == '/' {
uri.path = uri.path[:len(uri.path)-1]
}
return uri
}