add header-session

This commit is contained in:
aler9
2020-04-10 18:52:30 +02:00
parent 20e98c44de
commit e085d60e59
5 changed files with 102 additions and 13 deletions

View File

@@ -14,7 +14,7 @@ type HeaderAuth struct {
// ReadHeaderAuth parses an Authenticate or a WWW-Authenticate header. // ReadHeaderAuth parses an Authenticate or a WWW-Authenticate header.
func ReadHeaderAuth(in string) (*HeaderAuth, error) { func ReadHeaderAuth(in string) (*HeaderAuth, error) {
a := &HeaderAuth{ ha := &HeaderAuth{
Values: make(map[string]string), Values: make(map[string]string),
} }
@@ -22,7 +22,7 @@ func ReadHeaderAuth(in string) (*HeaderAuth, error) {
if i < 0 { if i < 0 {
return nil, fmt.Errorf("unable to find prefix (%s)", in) 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]+))(, )?") 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.TrimPrefix(m[2], "\"")
m[2] = strings.TrimSuffix(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
} }

View File

@@ -9,7 +9,7 @@ import (
var casesHeaderAuth = []struct { var casesHeaderAuth = []struct {
name string name string
byts string byts string
har *HeaderAuth ha *HeaderAuth
}{ }{
{ {
"basic", "basic",
@@ -66,7 +66,7 @@ func TestHeaderAuth(t *testing.T) {
t.Run(c.name, func(t *testing.T) { t.Run(c.name, func(t *testing.T) {
req, err := ReadHeaderAuth(c.byts) req, err := ReadHeaderAuth(c.byts)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, c.har, req) require.Equal(t, c.ha, req)
}) })
} }
} }

47
header-session.go Normal file
View File

@@ -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
}

42
header-session_test.go Normal file
View File

@@ -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)
})
}
}

View File

@@ -10,17 +10,17 @@ type HeaderTransport map[string]struct{}
// ReadHeaderTransport parses a Transport header. // ReadHeaderTransport parses a Transport header.
func ReadHeaderTransport(in string) HeaderTransport { func ReadHeaderTransport(in string) HeaderTransport {
th := make(map[string]struct{}) ht := make(map[string]struct{})
for _, t := range strings.Split(in, ";") { for _, t := range strings.Split(in, ";") {
th[t] = struct{}{} ht[t] = struct{}{}
} }
return th return ht
} }
// GetValue gets a value from the header. // GetValue gets a value from the header.
func (th HeaderTransport) GetValue(key string) string { func (ht HeaderTransport) GetValue(key string) string {
prefix := key + "=" prefix := key + "="
for t := range th { for t := range ht {
if strings.HasPrefix(t, prefix) { if strings.HasPrefix(t, prefix) {
return t[len(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. // GetPorts gets a given header value and parses its ports.
func (th HeaderTransport) GetPorts(key string) (int, int) { func (ht HeaderTransport) GetPorts(key string) (int, int) {
val := th.GetValue(key) val := ht.GetValue(key)
if val == "" { if val == "" {
return 0, 0 return 0, 0
} }