Debian Codegraph @

url.go

raw
1 // Copyright (c) 2022 Tulir Asokan
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 package mautrix
8
9 import (
10 "fmt"
11 "net/url"
12 "strconv"
13 "strings"
14 )
15
16 func parseAndNormalizeBaseURL(homeserverURL string) (*url.URL, error) {
17 hsURL, err := url.Parse(homeserverURL)
18 if err != nil {
19 return nil, err
20 }
21 if hsURL.Scheme == "" {
22 hsURL.Scheme = "https"
23 fixedURL := hsURL.String()
24 hsURL, err = url.Parse(fixedURL)
25 if err != nil {
26 return nil, fmt.Errorf("failed to parse fixed URL '%s': %v", fixedURL, err)
27 }
28 }
29 hsURL.RawPath = hsURL.EscapedPath()
30 return hsURL, nil
31 }
32
33 // BuildURL builds a URL with the given path parts
34 func BuildURL(baseURL *url.URL, path ...interface{}) *url.URL {
35 createdURL := *baseURL
36 rawParts := make([]string, len(path)+1)
37 rawParts[0] = strings.TrimSuffix(createdURL.RawPath, "/")
38 parts := make([]string, len(path)+1)
39 parts[0] = strings.TrimSuffix(createdURL.Path, "/")
40 for i, part := range path {
41 switch casted := part.(type) {
42 case string:
43 parts[i+1] = casted
44 case int:
45 parts[i+1] = strconv.Itoa(casted)
46 case Stringifiable:
47 parts[i+1] = casted.String()
48 default:
49 parts[i+1] = fmt.Sprint(casted)
50 }
51 rawParts[i+1] = url.PathEscape(parts[i+1])
52 }
53 createdURL.Path = strings.Join(parts, "/")
54 createdURL.RawPath = strings.Join(rawParts, "/")
55 return &createdURL
56 }
57
58 // BuildURL builds a URL with the Client's homeserver and appservice user ID set already.
59 func (cli *Client) BuildURL(urlPath PrefixableURLPath) string {
60 return cli.BuildURLWithQuery(urlPath, nil)
61 }
62
63 // BuildClientURL builds a URL with the Client's homeserver and appservice user ID set already.
64 // This method also automatically prepends the client API prefix (/_matrix/client).
65 func (cli *Client) BuildClientURL(urlPath ...interface{}) string {
66 return cli.BuildURLWithQuery(ClientURLPath(urlPath), nil)
67 }
68
69 type PrefixableURLPath interface {
70 FullPath() []interface{}
71 }
72
73 type BaseURLPath []interface{}
74
75 func (bup BaseURLPath) FullPath() []interface{} {
76 return bup
77 }
78
79 type ClientURLPath []interface{}
80
81 func (cup ClientURLPath) FullPath() []interface{} {
82 return append([]interface{}{"_matrix", "client"}, []interface{}(cup)...)
83 }
84
85 type MediaURLPath []interface{}
86
87 func (mup MediaURLPath) FullPath() []interface{} {
88 return append([]interface{}{"_matrix", "media"}, []interface{}(mup)...)
89 }
90
91 // BuildURLWithQuery builds a URL with query parameters in addition to the Client's homeserver
92 // and appservice user ID set already.
93 func (cli *Client) BuildURLWithQuery(urlPath PrefixableURLPath, urlQuery map[string]string) string {
94 hsURL := *BuildURL(cli.HomeserverURL, urlPath.FullPath()...)
95 query := hsURL.Query()
96 if cli.AppServiceUserID != "" {
97 query.Set("user_id", string(cli.AppServiceUserID))
98 }
99 if urlQuery != nil {
100 for k, v := range urlQuery {
101 query.Set(k, v)
102 }
103 }
104 hsURL.RawQuery = query.Encode()
105 return hsURL.String()
106 }