restore support for cameras with percent sign in password, broken after 1e612f2

This commit is contained in:
aler9
2022-05-10 17:32:11 +02:00
parent 5d66170bd7
commit 6fe7a316ba
2 changed files with 14 additions and 4 deletions

View File

@@ -3,6 +3,7 @@ package base
import ( import (
"fmt" "fmt"
"net/url" "net/url"
"regexp"
"strings" "strings"
) )
@@ -11,10 +12,17 @@ import (
// control attributes. // control attributes.
type URL url.URL type URL url.URL
var escapeRegexp = regexp.MustCompile(`^(.+?)://(.*?)@(.*?)/(.*?)$`)
// ParseURL parses a RTSP URL. // ParseURL parses a RTSP URL.
func ParseURL(s string) (*URL, error) { func ParseURL(s string) (*URL, error) {
s = strings.ReplaceAll(s, "%25", "%") // https://github.com/golang/go/issues/30611
s = strings.ReplaceAll(s, "%", "%25") 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) u, err := url.Parse(s)
if err != nil { if err != nil {

View File

@@ -1,6 +1,7 @@
package base package base
import ( import (
"net/url"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@@ -22,11 +23,12 @@ func TestURLParse(t *testing.T) {
}{ }{
{ {
"ipv6 stateless", "ipv6 stateless",
`rtsp://[fe80::a8f4:3219:f33e:a072%wl0]:8554/proxied`, `rtsp://user:pa%23ss@[fe80::a8f4:3219:f33e:a072%wl0]:8554/prox%23ied`,
&URL{ &URL{
Scheme: "rtsp", Scheme: "rtsp",
Host: "[fe80::a8f4:3219:f33e:a072%wl0]:8554", Host: "[fe80::a8f4:3219:f33e:a072%wl0]:8554",
Path: "/proxied", Path: "/prox#ied",
User: url.UserPassword("user", "pa#ss"),
}, },
}, },
} { } {