-
Notifications
You must be signed in to change notification settings - Fork 11
/
url.go
63 lines (50 loc) · 1.24 KB
/
url.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
package ejabberd
import (
"fmt"
"net/url"
"path"
)
// joinURL checks that Base URL is a valid URL and joins base URL with
// the method suffix string.
func joinURL(baseURL string, suffix string) (string, error) {
var u *url.URL
var err error
if u, err = url.Parse(baseURL); err != nil {
return "", fmt.Errorf("invalid url: %s", baseURL)
}
if u.Scheme != "http" && u.Scheme != "https" {
return "", fmt.Errorf("invalid url scheme: %s", u.Scheme)
}
u.Path = path.Join(u.Path, suffix)
return u.String(), nil
}
// tokenURL generates URL endpoint for retrieving a token using
// password grant type.
func tokenURL(baseURL, oauthPath string) (string, error) {
var path string
var err error
if oauthPath == "" {
path, err = joinURL(baseURL, "oauth")
} else {
path, err = joinURL(baseURL, oauthPath)
}
if err != nil {
return baseURL, err
}
return joinURL(path, "token")
}
// apiURL generates URL endpoint for calling a given ejabberd API
// command name.
func apiURL(baseURL, apiPath, name string) (string, error) {
var path string
var err error
if apiPath == "" {
path, err = joinURL(baseURL, "api")
} else {
path, err = joinURL(baseURL, apiPath)
}
if err != nil {
return baseURL, err
}
return joinURL(path, name+"/")
}