diff --git a/header-auth.go b/header-auth.go index dd50e6de..85ca9af9 100644 --- a/header-auth.go +++ b/header-auth.go @@ -14,7 +14,7 @@ type HeaderAuth struct { // ReadHeaderAuth parses an Authenticate or a WWW-Authenticate header. func ReadHeaderAuth(in string) (*HeaderAuth, error) { - a := &HeaderAuth{ + ha := &HeaderAuth{ Values: make(map[string]string), } @@ -22,7 +22,7 @@ func ReadHeaderAuth(in string) (*HeaderAuth, error) { if i < 0 { return nil, fmt.Errorf("unable to find prefix (%s)", in) } - a.Prefix, in = in[:i], in[i+1:] + ha.Prefix, in = in[:i], in[i+1:] r := regexp.MustCompile("^([a-z]+)=(\"(.+?)\"|([a-zA-Z0-9]+))(, )?") @@ -35,8 +35,8 @@ func ReadHeaderAuth(in string) (*HeaderAuth, error) { m[2] = strings.TrimPrefix(m[2], "\"") m[2] = strings.TrimSuffix(m[2], "\"") - a.Values[m[1]] = m[2] + ha.Values[m[1]] = m[2] } - return a, nil + return ha, nil } diff --git a/header-auth_test.go b/header-auth_test.go index 46c83b59..59a21672 100644 --- a/header-auth_test.go +++ b/header-auth_test.go @@ -9,7 +9,7 @@ import ( var casesHeaderAuth = []struct { name string byts string - har *HeaderAuth + ha *HeaderAuth }{ { "basic", @@ -66,7 +66,7 @@ func TestHeaderAuth(t *testing.T) { t.Run(c.name, func(t *testing.T) { req, err := ReadHeaderAuth(c.byts) require.NoError(t, err) - require.Equal(t, c.har, req) + require.Equal(t, c.ha, req) }) } } diff --git a/header-session.go b/header-session.go new file mode 100644 index 00000000..c3b20aef --- /dev/null +++ b/header-session.go @@ -0,0 +1,47 @@ +package gortsplib + +import ( + "fmt" + "strconv" + "strings" +) + +// HeaderSession is a Session header. +type HeaderSession struct { + Session string + Timeout *uint +} + +// ReadHeaderSession parses a Session header. +func ReadHeaderSession(in string) (*HeaderSession, error) { + parts := strings.Split(in, ";") + if len(parts) == 0 { + return nil, fmt.Errorf("invalid value") + } + + hs := &HeaderSession{} + + hs.Session, parts = parts[0], parts[1:] + + for _, part := range parts { + keyval := strings.Split(part, "=") + if len(keyval) != 2 { + return nil, fmt.Errorf("invalid value") + } + + key, strValue := keyval[0], keyval[1] + if key != "timeout" { + return nil, fmt.Errorf("invalid key '%s'", key) + } + + iv, err := strconv.ParseUint(strValue, 10, 64) + if err != nil { + return nil, err + } + uiv := uint(iv) + + hs.Timeout = &uiv + } + + return hs, nil +} diff --git a/header-session_test.go b/header-session_test.go new file mode 100644 index 00000000..6527f791 --- /dev/null +++ b/header-session_test.go @@ -0,0 +1,42 @@ +package gortsplib + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +var casesHeaderSession = []struct { + name string + byts string + hs *HeaderSession +}{ + { + "base", + `A3eqwsafq3rFASqew`, + &HeaderSession{ + Session: "A3eqwsafq3rFASqew", + }, + }, + { + "with timeout", + `A3eqwsafq3rFASqew;timeout=47`, + &HeaderSession{ + Session: "A3eqwsafq3rFASqew", + Timeout: func() *uint { + v := uint(47) + return &v + }(), + }, + }, +} + +func TestHeaderSession(t *testing.T) { + for _, c := range casesHeaderSession { + t.Run(c.name, func(t *testing.T) { + req, err := ReadHeaderSession(c.byts) + require.NoError(t, err) + require.Equal(t, c.hs, req) + }) + } +} diff --git a/header-transport.go b/header-transport.go index 2be81f80..0b611bea 100644 --- a/header-transport.go +++ b/header-transport.go @@ -10,17 +10,17 @@ type HeaderTransport map[string]struct{} // ReadHeaderTransport parses a Transport header. func ReadHeaderTransport(in string) HeaderTransport { - th := make(map[string]struct{}) + ht := make(map[string]struct{}) for _, t := range strings.Split(in, ";") { - th[t] = struct{}{} + ht[t] = struct{}{} } - return th + return ht } // GetValue gets a value from the header. -func (th HeaderTransport) GetValue(key string) string { +func (ht HeaderTransport) GetValue(key string) string { prefix := key + "=" - for t := range th { + for t := range ht { if strings.HasPrefix(t, prefix) { return t[len(prefix):] } @@ -29,8 +29,8 @@ func (th HeaderTransport) GetValue(key string) string { } // GetPorts gets a given header value and parses its ports. -func (th HeaderTransport) GetPorts(key string) (int, int) { - val := th.GetValue(key) +func (ht HeaderTransport) GetPorts(key string) (int, int) { + val := ht.GetValue(key) if val == "" { return 0, 0 }