-
Notifications
You must be signed in to change notification settings - Fork 180
/
url.go
106 lines (90 loc) · 2.41 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
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
// Package url contains the URL structure.
package url
import (
"fmt"
"net/url"
"regexp"
"strings"
)
// URL is a RTSP URL.
// This is basically an HTTP URL with some additional functions to handle
// control attributes.
type URL url.URL
var escapeRegexp = regexp.MustCompile(`^(.+?)://(.*?)@(.*?)/(.*?)$`)
// Parse parses a RTSP URL.
func Parse(s string) (*URL, error) {
// https://github.com/golang/go/issues/30611
m := escapeRegexp.FindStringSubmatch(s)
if m != nil {
m[3] = strings.ReplaceAll(m[3], "%25", "%")
m[3] = strings.ReplaceAll(m[3], "%", "%25")
s = m[1] + "://" + m[2] + "@" + m[3] + "/" + m[4]
}
u, err := url.Parse(s)
if err != nil {
return nil, err
}
if u.Scheme != "rtsp" && u.Scheme != "rtsps" {
return nil, fmt.Errorf("unsupported scheme '%s'", u.Scheme)
}
if u.Opaque != "" {
return nil, fmt.Errorf("URLs with opaque data are not supported")
}
if u.Fragment != "" {
return nil, fmt.Errorf("URLs with fragments are not supported")
}
return (*URL)(u), nil
}
// String implements fmt.Stringer.
func (u *URL) String() string {
return (*url.URL)(u).String()
}
// Clone clones a URL.
func (u *URL) Clone() *URL {
return (*URL)(&url.URL{
Scheme: u.Scheme,
User: u.User,
Host: u.Host,
Path: u.Path,
RawPath: u.RawPath,
ForceQuery: u.ForceQuery,
RawQuery: u.RawQuery,
})
}
// CloneWithoutCredentials clones a URL without its credentials.
func (u *URL) CloneWithoutCredentials() *URL {
return (*URL)(&url.URL{
Scheme: u.Scheme,
Host: u.Host,
Path: u.Path,
RawPath: u.RawPath,
ForceQuery: u.ForceQuery,
RawQuery: u.RawQuery,
})
}
// RTSPPathAndQuery returns the path and query of a RTSP URL.
func (u *URL) RTSPPathAndQuery() (string, bool) {
var pathAndQuery string
if u.RawPath != "" {
pathAndQuery = u.RawPath
} else {
pathAndQuery = u.Path
}
if u.RawQuery != "" {
pathAndQuery += "?" + u.RawQuery
}
return pathAndQuery, true
}
// Hostname returns u.Host, stripping any valid port number if present.
//
// If the result is enclosed in square brackets, as literal IPv6 addresses are,
// the square brackets are removed from the result.
func (u *URL) Hostname() string {
return (*url.URL)(u).Hostname()
}
// Port returns the port part of u.Host, without the leading colon.
//
// If u.Host doesn't contain a valid numeric port, Port returns an empty string.
func (u *URL) Port() string {
return (*url.URL)(u).Port()
}