-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathresolver.go
71 lines (61 loc) · 1.82 KB
/
resolver.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
package deeplinks
import (
"fmt"
"net/url"
"strings"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
var decoder = schema.NewDecoder()
func Resolve(link string) (Deeplink, error) {
u, err := url.Parse(link)
if err != nil {
return nil, errors.Wrap(err, "not a uri")
}
switch u.Scheme {
case "", "http", "https":
return resolveHttpLink(u)
case "tg":
return resolveTgLink(u)
default:
return nil, fmt.Errorf("'%v': invalid uri scheme (only tg://, http://, https:// are valid)", u.Scheme)
}
}
func resolveHttpLink(u *url.URL) (Deeplink, error) {
//? url host without schema parsing as path, see func description
fixURLHost(u)
//? Hostname(), cause MAYBE someone want add port for unknown reason. Logically it's better than u.Host
if !stringListContains(ReservedHosts(), u.Hostname()) {
return nil, fmt.Errorf("'%v' hostname is not owned by telegram", u.Hostname())
}
type pathVariablesConverter = func(path map[string]string, query url.Values) (Deeplink, error)
for tpl, f := range map[string]pathVariablesConverter{
"/joinchat/{token}": func(path map[string]string, query url.Values) (Deeplink, error) {
token, ok := path["token"]
if !ok || token == "" {
return nil, errors.New("invite token required")
}
return &JoinParameters{
Invite: token,
}, nil
},
"/{username}": func(path map[string]string, query url.Values) (Deeplink, error) {
username, ok := path["username"]
if !ok || username == "" {
return nil, errors.New("username required")
}
return &ResolveParameters{
Domain: strings.ToLower(username),
}, nil
},
} {
vars, ok := matchPath(tpl, u.Path)
if ok {
return f(vars, u.Query())
}
}
return nil, fmt.Errorf("'%v': this path does not look valid", u.Path)
}
func resolveTgLink(u *url.URL) (Deeplink, error) {
return nil, errors.New("not implemented")
}