From 071637fde042c238cdb1d1bc3bf195fd8647e823 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sat, 31 Oct 2020 14:57:14 +0100 Subject: [PATCH] fix error when authenticating with vlc and slashes in paths --- auth/package_test.go | 6 +- auth/server.go | 20 ++--- base/url.go | 2 +- base/url_test.go | 190 +++++++++++++++++++++---------------------- 4 files changed, 106 insertions(+), 112 deletions(-) diff --git a/auth/package_test.go b/auth/package_test.go index 6de1d0db..59aa0c42 100644 --- a/auth/package_test.go +++ b/auth/package_test.go @@ -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) } diff --git a/auth/server.go b/auth/server.go index 779b6440..aea99e30 100644 --- a/auth/server.go +++ b/auth/server.go @@ -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,18 +126,12 @@ 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] - } - - return ret - }() - uri = newUrl.String() + // 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() + } if *auth.URI != uri { return fmt.Errorf("wrong url") diff --git a/base/url.go b/base/url.go index 85ad181a..b5124a54 100644 --- a/base/url.go +++ b/base/url.go @@ -1,8 +1,8 @@ package base import ( - "strings" "net/url" + "strings" ) func stringsReverseIndexByte(s string, c byte) int { diff --git a/base/url_test.go b/base/url_test.go index 9e6cc919..af05dbde 100644 --- a/base/url_test.go +++ b/base/url_test.go @@ -1,109 +1,109 @@ package base import ( - "net/url" - "testing" + "net/url" + "testing" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/require" ) func TestURLGetBasePath(t *testing.T) { - for _, ca := range []struct{ - u *url.URL - b string - } { - { - urlMustParse("rtsp://localhost:8554/teststream"), - "teststream", - }, - { - urlMustParse("rtsp://localhost:8554/test/stream"), - "test/stream", - }, - { - urlMustParse("rtsp://192.168.1.99:554/test?user=tmp&password=BagRep1&channel=1&stream=0.sdp"), - "test", - }, - { - urlMustParse("rtsp://192.168.1.99:554/te!st?user=tmp&password=BagRep1!&channel=1&stream=0.sdp"), - "te!st", - }, - { - urlMustParse("rtsp://192.168.1.99:554/user=tmp&password=BagRep1!&channel=1&stream=0.sdp"), - "user=tmp&password=BagRep1!&channel=1&stream=0.sdp", - }, - } { - b := URLGetBasePath(ca.u) - require.Equal(t, ca.b, b) - } + for _, ca := range []struct { + u *url.URL + b string + }{ + { + urlMustParse("rtsp://localhost:8554/teststream"), + "teststream", + }, + { + urlMustParse("rtsp://localhost:8554/test/stream"), + "test/stream", + }, + { + urlMustParse("rtsp://192.168.1.99:554/test?user=tmp&password=BagRep1&channel=1&stream=0.sdp"), + "test", + }, + { + urlMustParse("rtsp://192.168.1.99:554/te!st?user=tmp&password=BagRep1!&channel=1&stream=0.sdp"), + "te!st", + }, + { + urlMustParse("rtsp://192.168.1.99:554/user=tmp&password=BagRep1!&channel=1&stream=0.sdp"), + "user=tmp&password=BagRep1!&channel=1&stream=0.sdp", + }, + } { + b := URLGetBasePath(ca.u) + require.Equal(t, ca.b, b) + } } func TestURLGetBaseControlPath(t *testing.T) { - for _, ca := range []struct{ - u *url.URL - b string - c string - } { - { - urlMustParse("rtsp://localhost:8554/teststream/trackID=1"), - "teststream", - "trackID=1", - }, - { - urlMustParse("rtsp://localhost:8554/test/stream/trackID=1"), - "test/stream", - "trackID=1", - }, - { - urlMustParse("rtsp://192.168.1.99:554/test?user=tmp&password=BagRep1&channel=1&stream=0.sdp/trackID=1"), - "test", - "trackID=1", - }, - { - urlMustParse("rtsp://192.168.1.99:554/te!st?user=tmp&password=BagRep1!&channel=1&stream=0.sdp/trackID=1"), - "te!st", - "trackID=1", - }, - { - urlMustParse("rtsp://192.168.1.99:554/user=tmp&password=BagRep1!&channel=1&stream=0.sdp/trackID=1"), - "user=tmp&password=BagRep1!&channel=1&stream=0.sdp", - "trackID=1", - }, - } { - b, c, ok := URLGetBaseControlPath(ca.u) - require.Equal(t, true, ok) - require.Equal(t, ca.b, b) - require.Equal(t, ca.c, c) - } + for _, ca := range []struct { + u *url.URL + b string + c string + }{ + { + urlMustParse("rtsp://localhost:8554/teststream/trackID=1"), + "teststream", + "trackID=1", + }, + { + urlMustParse("rtsp://localhost:8554/test/stream/trackID=1"), + "test/stream", + "trackID=1", + }, + { + urlMustParse("rtsp://192.168.1.99:554/test?user=tmp&password=BagRep1&channel=1&stream=0.sdp/trackID=1"), + "test", + "trackID=1", + }, + { + urlMustParse("rtsp://192.168.1.99:554/te!st?user=tmp&password=BagRep1!&channel=1&stream=0.sdp/trackID=1"), + "te!st", + "trackID=1", + }, + { + urlMustParse("rtsp://192.168.1.99:554/user=tmp&password=BagRep1!&channel=1&stream=0.sdp/trackID=1"), + "user=tmp&password=BagRep1!&channel=1&stream=0.sdp", + "trackID=1", + }, + } { + b, c, ok := URLGetBaseControlPath(ca.u) + require.Equal(t, true, ok) + require.Equal(t, ca.b, b) + require.Equal(t, ca.c, c) + } } func TestURLAddControlPath(t *testing.T) { - for _, ca := range []struct{ - u *url.URL - ou *url.URL - } { - { - urlMustParse("rtsp://localhost:8554/teststream"), - urlMustParse("rtsp://localhost:8554/teststream/trackID=1"), - }, - { - urlMustParse("rtsp://localhost:8554/test/stream"), - urlMustParse("rtsp://localhost:8554/test/stream/trackID=1"), - }, - { - urlMustParse("rtsp://192.168.1.99:554/test?user=tmp&password=BagRep1&channel=1&stream=0.sdp"), - urlMustParse("rtsp://192.168.1.99:554/test?user=tmp&password=BagRep1&channel=1&stream=0.sdp/trackID=1"), - }, - { - urlMustParse("rtsp://192.168.1.99:554/te!st?user=tmp&password=BagRep1!&channel=1&stream=0.sdp"), - urlMustParse("rtsp://192.168.1.99:554/te!st?user=tmp&password=BagRep1!&channel=1&stream=0.sdp/trackID=1"), - }, - { - urlMustParse("rtsp://192.168.1.99:554/user=tmp&password=BagRep1!&channel=1&stream=0.sdp"), - urlMustParse("rtsp://192.168.1.99:554/user=tmp&password=BagRep1!&channel=1&stream=0.sdp/trackID=1"), - }, - } { - URLAddControlPath(ca.u, "trackID=1") - require.Equal(t, ca.ou, ca.u) - } + for _, ca := range []struct { + u *url.URL + ou *url.URL + }{ + { + urlMustParse("rtsp://localhost:8554/teststream"), + urlMustParse("rtsp://localhost:8554/teststream/trackID=1"), + }, + { + urlMustParse("rtsp://localhost:8554/test/stream"), + urlMustParse("rtsp://localhost:8554/test/stream/trackID=1"), + }, + { + urlMustParse("rtsp://192.168.1.99:554/test?user=tmp&password=BagRep1&channel=1&stream=0.sdp"), + urlMustParse("rtsp://192.168.1.99:554/test?user=tmp&password=BagRep1&channel=1&stream=0.sdp/trackID=1"), + }, + { + urlMustParse("rtsp://192.168.1.99:554/te!st?user=tmp&password=BagRep1!&channel=1&stream=0.sdp"), + urlMustParse("rtsp://192.168.1.99:554/te!st?user=tmp&password=BagRep1!&channel=1&stream=0.sdp/trackID=1"), + }, + { + urlMustParse("rtsp://192.168.1.99:554/user=tmp&password=BagRep1!&channel=1&stream=0.sdp"), + urlMustParse("rtsp://192.168.1.99:554/user=tmp&password=BagRep1!&channel=1&stream=0.sdp/trackID=1"), + }, + } { + URLAddControlPath(ca.u, "trackID=1") + require.Equal(t, ca.ou, ca.u) + } }