mirror of
https://github.com/aler9/gortsplib
synced 2025-10-07 16:10:59 +08:00
65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
package gortsplib
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// HeaderTransport is a Transport header.
|
|
type HeaderTransport map[string]struct{}
|
|
|
|
// ReadHeaderTransport parses a Transport header.
|
|
func ReadHeaderTransport(v HeaderValue) (HeaderTransport, error) {
|
|
if len(v) == 0 {
|
|
return nil, fmt.Errorf("value not provided")
|
|
}
|
|
|
|
if len(v) > 1 {
|
|
return nil, fmt.Errorf("value provided multiple times (%v)", v)
|
|
}
|
|
|
|
ht := make(map[string]struct{})
|
|
for _, t := range strings.Split(v[0], ";") {
|
|
ht[t] = struct{}{}
|
|
}
|
|
|
|
return ht, nil
|
|
}
|
|
|
|
// Value gets a value from the header.
|
|
func (ht HeaderTransport) Value(key string) string {
|
|
prefix := key + "="
|
|
for t := range ht {
|
|
if strings.HasPrefix(t, prefix) {
|
|
return t[len(prefix):]
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// Ports gets a given header value and parses its ports.
|
|
func (ht HeaderTransport) Ports(key string) (int, int) {
|
|
val := ht.Value(key)
|
|
if val == "" {
|
|
return 0, 0
|
|
}
|
|
|
|
ports := strings.Split(val, "-")
|
|
if len(ports) != 2 {
|
|
return 0, 0
|
|
}
|
|
|
|
port1, err := strconv.ParseInt(ports[0], 10, 64)
|
|
if err != nil {
|
|
return 0, 0
|
|
}
|
|
|
|
port2, err := strconv.ParseInt(ports[1], 10, 64)
|
|
if err != nil {
|
|
return 0, 0
|
|
}
|
|
|
|
return int(port1), int(port2)
|
|
}
|