normalize header keys

This commit is contained in:
aler9
2020-05-17 16:10:46 +02:00
parent b729d0192f
commit ffd1119b91
2 changed files with 54 additions and 0 deletions

View File

@@ -3,7 +3,9 @@ package gortsplib
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"net/http"
"sort" "sort"
"strings"
) )
const ( const (
@@ -12,6 +14,20 @@ const (
_MAX_HEADER_VALUE_LENGTH = 1024 _MAX_HEADER_VALUE_LENGTH = 1024
) )
func normalizeHeaderKey(in string) string {
switch strings.ToLower(in) {
case "rtp-info":
return "RTP-INFO"
case "www-authenticate":
return "WWW-Authenticate"
case "cseq":
return "CSeq"
}
return http.CanonicalHeaderKey(in)
}
// Header is a RTSP reader, present in both Requests and Responses. // Header is a RTSP reader, present in both Requests and Responses.
type Header map[string][]string type Header map[string][]string
@@ -43,6 +59,7 @@ func readHeader(rb *bufio.Reader) (Header, error) {
return nil, err return nil, err
} }
key += string(byts[:len(byts)-1]) key += string(byts[:len(byts)-1])
key = normalizeHeaderKey(key)
err = readByteEqual(rb, ' ') err = readByteEqual(rb, ' ')
if err != nil { if err != nil {

View File

@@ -59,3 +59,40 @@ func TestHeaderWrite(t *testing.T) {
}) })
} }
} }
var casesHeaderNormalization = []struct {
name string
byts []byte
header Header
}{
{
"standard",
[]byte("Content-type: testing\r\n" +
"Content-length: value\r\n" +
"\r\n"),
Header{
"Content-Type": []string{"testing"},
"Content-Length": []string{"value"},
},
},
{
"non-standard",
[]byte("Www-Authenticate: value\r\n" +
"Cseq: value\r\n" +
"\r\n"),
Header{
"WWW-Authenticate": []string{"value"},
"CSeq": []string{"value"},
},
},
}
func TestHeaderNormalization(t *testing.T) {
for _, c := range casesHeaderNormalization {
t.Run(c.name, func(t *testing.T) {
req, err := readHeader(bufio.NewReader(bytes.NewBuffer(c.byts)))
require.NoError(t, err)
require.Equal(t, c.header, req)
})
}
}