Skip to content

Commit d59cb99

Browse files
committed
Support RTSP redirects #1909 by @eddielth
1 parent 007e8db commit d59cb99

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

pkg/rtsp/client.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,35 @@ func (c *Conn) Do(req *tcp.Request) (*tcp.Response, error) {
9393

9494
c.Fire(res)
9595

96-
if res.StatusCode == http.StatusUnauthorized {
96+
switch res.StatusCode {
97+
case http.StatusOK:
98+
return res, nil
99+
100+
case http.StatusMovedPermanently, http.StatusFound:
101+
rawURL := res.Header.Get("Location")
102+
103+
var u *url.URL
104+
if u, err = url.Parse(rawURL); err != nil {
105+
return nil, err
106+
}
107+
108+
if u.User == nil {
109+
u.User = c.auth.UserInfo() // restore auth if we don't have it in the new URL
110+
}
111+
112+
c.uri = u.String() // so auth will be saved on reconnect
113+
114+
_ = c.conn.Close()
115+
116+
if err = c.Dial(); err != nil {
117+
return nil, err
118+
}
119+
120+
req.URL = c.URL // because path was changed
121+
122+
return c.Do(req)
123+
124+
case http.StatusUnauthorized:
97125
switch c.auth.Method {
98126
case tcp.AuthNone:
99127
if c.auth.ReadNone(res) {
@@ -109,11 +137,7 @@ func (c *Conn) Do(req *tcp.Request) (*tcp.Response, error) {
109137
}
110138
}
111139

112-
if res.StatusCode != http.StatusOK {
113-
return res, fmt.Errorf("wrong response on %s", req.Method)
114-
}
115-
116-
return res, nil
140+
return res, fmt.Errorf("wrong response on %s", req.Method)
117141
}
118142

119143
func (c *Conn) Options() error {

pkg/tcp/auth.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ func (a *Auth) ReadNone(res *Response) bool {
112112
return false
113113
}
114114

115+
func (a *Auth) UserInfo() *url.Userinfo {
116+
return url.UserPassword(a.user, a.pass)
117+
}
118+
115119
func Between(s, sub1, sub2 string) string {
116120
i := strings.Index(s, sub1)
117121
if i < 0 {

0 commit comments

Comments
 (0)