Files
gortsplib/pkg/base/url.go
2021-09-23 08:53:10 +02:00

85 lines
1.7 KiB
Go

package base
import (
"fmt"
"net/url"
)
// URL is a RTSP URL.
// This is basically an HTTP URL with some additional functions to handle
// control attributes.
type URL url.URL
// ParseURL parses a RTSP URL.
func ParseURL(s string) (*URL, error) {
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
}
// remove leading slash
if len(pathAndQuery) == 0 || pathAndQuery[0] != '/' {
return "", false
}
pathAndQuery = pathAndQuery[1:]
return pathAndQuery, true
}