fix error when authenticating with vlc and slashes in paths

This commit is contained in:
aler9
2020-10-31 14:57:14 +01:00
parent d85cfae2ed
commit 071637fde0
4 changed files with 106 additions and 112 deletions

View File

@@ -46,7 +46,7 @@ func TestAuthMethods(t *testing.T) {
}
}
func TestAuthBasePath(t *testing.T) {
func TestAuthVLC(t *testing.T) {
authServer := NewServer("testuser", "testpass",
[]headers.AuthMethod{headers.AuthBasic, headers.AuthDigest})
wwwAuthenticate := authServer.GenerateHeader()
@@ -54,9 +54,9 @@ func TestAuthBasePath(t *testing.T) {
ac, err := NewClient(wwwAuthenticate, "testuser", "testpass")
require.NoError(t, err)
authorization := ac.GenerateHeader(base.ANNOUNCE,
&url.URL{Scheme: "rtsp", Host: "myhost", Path: "mypath/"})
&url.URL{Scheme: "rtsp", Host: "myhost", Path: "/mypath/"})
err = authServer.ValidateHeader(authorization, base.ANNOUNCE,
&url.URL{Scheme: "rtsp", Host: "myhost", Path: "mypath/trackId=0"})
&url.URL{Scheme: "rtsp", Host: "myhost", Path: "/mypath/trackId=0"})
require.NoError(t, err)
}

View File

@@ -12,7 +12,7 @@ import (
"github.com/aler9/gortsplib/headers"
)
// Server is an object that helps a server to authenticate a client.
// Server allows a server to authenticate a client.
type Server struct {
user string
pass string
@@ -126,19 +126,13 @@ func (as *Server) ValidateHeader(v base.HeaderValue, method base.Method, ur *url
uri := ur.String()
if *auth.URI != uri {
// VLC strips the subpath
newUrl := *ur
newUrl.Path = func() string {
ret := newUrl.Path
if n := strings.Index(ret[1:], "/"); n >= 0 {
ret = ret[:n+2]
// VLC strips the control path; do another try without the control path
base, _, ok := base.URLGetBaseControlPath(ur)
if ok {
ur.Path = "/" + base + "/"
uri = ur.String()
}
return ret
}()
uri = newUrl.String()
if *auth.URI != uri {
return fmt.Errorf("wrong url")
}

View File

@@ -1,8 +1,8 @@
package base
import (
"strings"
"net/url"
"strings"
)
func stringsReverseIndexByte(s string, c byte) int {

View File

@@ -8,10 +8,10 @@ import (
)
func TestURLGetBasePath(t *testing.T) {
for _, ca := range []struct{
for _, ca := range []struct {
u *url.URL
b string
} {
}{
{
urlMustParse("rtsp://localhost:8554/teststream"),
"teststream",
@@ -39,11 +39,11 @@ func TestURLGetBasePath(t *testing.T) {
}
func TestURLGetBaseControlPath(t *testing.T) {
for _, ca := range []struct{
for _, ca := range []struct {
u *url.URL
b string
c string
} {
}{
{
urlMustParse("rtsp://localhost:8554/teststream/trackID=1"),
"teststream",
@@ -78,10 +78,10 @@ func TestURLGetBaseControlPath(t *testing.T) {
}
func TestURLAddControlPath(t *testing.T) {
for _, ca := range []struct{
for _, ca := range []struct {
u *url.URL
ou *url.URL
} {
}{
{
urlMustParse("rtsp://localhost:8554/teststream"),
urlMustParse("rtsp://localhost:8554/teststream/trackID=1"),