mirror of
https://github.com/aler9/gortsplib
synced 2025-10-05 23:26:54 +08:00
Persist credentials on source redirections (#124)
* add client opt to persist credentials on source redirections * Remove PersistCredentialsOnRedirect flag, persist creds (if any) by default on redirects * remove useless check Co-authored-by: aler9 <46489434+aler9@users.noreply.github.com>
This commit is contained in:

committed by
GitHub

parent
277e89f3ac
commit
09304e1c2e
12
client.go
12
client.go
@@ -1211,15 +1211,19 @@ func (c *Client) doDescribe(u *base.URL) (Tracks, *base.URL, *base.Response, err
|
|||||||
len(res.Header["Location"]) == 1 {
|
len(res.Header["Location"]) == 1 {
|
||||||
c.reset()
|
c.reset()
|
||||||
|
|
||||||
u, err := base.ParseURL(res.Header["Location"][0])
|
ru, err := base.ParseURL(res.Header["Location"][0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.scheme = u.Scheme
|
if u.User != nil {
|
||||||
c.host = u.Host
|
ru.User = u.User
|
||||||
|
}
|
||||||
|
|
||||||
return c.doDescribe(u)
|
c.scheme = ru.Scheme
|
||||||
|
c.host = ru.Host
|
||||||
|
|
||||||
|
return c.doDescribe(ru)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil, res, liberrors.ErrClientBadStatusCode{Code: res.StatusCode, Message: res.StatusMessage}
|
return nil, nil, res, liberrors.ErrClientBadStatusCode{Code: res.StatusCode, Message: res.StatusMessage}
|
||||||
|
@@ -1573,6 +1573,20 @@ func TestClientReadDifferentInterleavedIDs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestClientReadRedirect(t *testing.T) {
|
func TestClientReadRedirect(t *testing.T) {
|
||||||
|
for _, withCredentials := range []bool{false, true} {
|
||||||
|
runName := "WithoutCredentials"
|
||||||
|
if withCredentials {
|
||||||
|
runName = "WithCredentials"
|
||||||
|
}
|
||||||
|
t.Run(runName, func(t *testing.T) {
|
||||||
|
packetRecv := make(chan struct{})
|
||||||
|
|
||||||
|
c := Client{
|
||||||
|
OnPacketRTP: func(ctx *ClientOnPacketRTPCtx) {
|
||||||
|
close(packetRecv)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
l, err := net.Listen("tcp", "localhost:8554")
|
l, err := net.Listen("tcp", "localhost:8554")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer l.Close()
|
defer l.Close()
|
||||||
@@ -1637,6 +1651,7 @@ func TestClientReadRedirect(t *testing.T) {
|
|||||||
}, ", ")},
|
}, ", ")},
|
||||||
},
|
},
|
||||||
}.Write()
|
}.Write()
|
||||||
|
|
||||||
_, err = conn.Write(byts)
|
_, err = conn.Write(byts)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
@@ -1644,6 +1659,39 @@ func TestClientReadRedirect(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, base.Describe, req.Method)
|
require.Equal(t, base.Describe, req.Method)
|
||||||
|
|
||||||
|
if withCredentials {
|
||||||
|
if _, exists := req.Header["Authorization"]; !exists {
|
||||||
|
authRealm := "example@localhost"
|
||||||
|
authNonce := "exampleNonce"
|
||||||
|
authOpaque := "exampleOpaque"
|
||||||
|
authStale := "FALSE"
|
||||||
|
authAlg := "MD5"
|
||||||
|
byts, _ = base.Response{
|
||||||
|
Header: base.Header{
|
||||||
|
"WWW-Authenticate": headers.Authenticate{
|
||||||
|
Method: headers.AuthDigest,
|
||||||
|
Realm: &authRealm,
|
||||||
|
Nonce: &authNonce,
|
||||||
|
Opaque: &authOpaque,
|
||||||
|
Stale: &authStale,
|
||||||
|
Algorithm: &authAlg,
|
||||||
|
}.Write(),
|
||||||
|
},
|
||||||
|
StatusCode: base.StatusUnauthorized,
|
||||||
|
}.Write()
|
||||||
|
_, err = conn.Write(byts)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
req, err = readRequest(br)
|
||||||
|
require.NoError(t, err)
|
||||||
|
authHeaderVal, exists := req.Header["Authorization"]
|
||||||
|
require.True(t, exists)
|
||||||
|
var authHeader headers.Authenticate
|
||||||
|
require.NoError(t, authHeader.Read(authHeaderVal))
|
||||||
|
require.Equal(t, *authHeader.Username, "testusr")
|
||||||
|
require.Equal(t, base.Describe, req.Method)
|
||||||
|
}
|
||||||
|
|
||||||
track, err := NewTrackH264(96, []byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}, nil)
|
track, err := NewTrackH264(96, []byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
@@ -1708,19 +1756,17 @@ func TestClientReadRedirect(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}()
|
}()
|
||||||
|
|
||||||
packetRecv := make(chan struct{})
|
ru := "rtsp://localhost:8554/path1"
|
||||||
|
if withCredentials {
|
||||||
c := Client{
|
ru = "rtsp://testusr:testpwd@localhost:8554/path1"
|
||||||
OnPacketRTP: func(ctx *ClientOnPacketRTPCtx) {
|
|
||||||
close(packetRecv)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
err = c.StartReading(ru)
|
||||||
err = c.StartReading("rtsp://localhost:8554/path1")
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
|
|
||||||
<-packetRecv
|
<-packetRecv
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestClientReadPause(t *testing.T) {
|
func TestClientReadPause(t *testing.T) {
|
||||||
|
Reference in New Issue
Block a user