mirror of
https://github.com/aler9/gortsplib
synced 2025-10-06 15:46:51 +08:00
add header-session
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
@@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
47
header-session.go
Normal file
47
header-session.go
Normal 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
42
header-session_test.go
Normal 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)
|
||||
})
|
||||
}
|
||||
}
|
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user