Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
var errSerialization = errors.New("serialization error")

//nolint:gochecknoglobals
var byteSequenceWhitespace = regexp.MustCompile(`\s+`)
var byteSequenceObsFold = regexp.MustCompile("[ \t]*\r\n[ \t]+")

type serializer interface {
serialize(v []string) ([]string, error)
Expand Down Expand Up @@ -51,12 +51,11 @@ func (s *byteSequenceSerializer) serialize(v []string) ([]string, error) {
encoded := make([]string, len(v))

for i, sv := range v {
values := strings.Split(sv, ",")
for j, v := range values {
values[j] = byteSequenceWhitespace.ReplaceAllString(strings.TrimSpace(v), " ")
}

item := httpsfv.NewItem([]byte(strings.Join(values, ", ")))
// RFC 9421, Section 2.1.3: preserve field-value bytes except for
// surrounding HTTP whitespace and obsolete line folding.
value := strings.Trim(sv, " \t")
value = byteSequenceObsFold.ReplaceAllString(value, " ")
item := httpsfv.NewItem([]byte(value))

marshalled, err := httpsfv.Marshal(item)
if err != nil {
Expand Down
30 changes: 30 additions & 0 deletions serializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,36 @@ func TestByteSequenceSerialization(t *testing.T) {
input: []string{"value, with, lots, of, commas"},
result: []string{":dmFsdWUsIHdpdGgsIGxvdHMsIG9mLCBjb21tYXM=:"},
},
{
uc: "preserve internal spaces and commas",
input: []string{"foo , bar"},
result: []string{":Zm9vICwgIGJhcg==:"},
},
{
uc: "preserve internal tabs",
input: []string{"alpha\tbeta"},
result: []string{":YWxwaGEJYmV0YQ==:"},
},
{
uc: "strip surrounding HTTP whitespace",
input: []string{"\t alpha \t"},
result: []string{":YWxwaGE=:"},
},
{
uc: "unfold obsolete line folding",
input: []string{"hello\r\n \tworld"},
result: []string{":aGVsbG8gd29ybGQ=:"},
},
{
uc: "unfold obsolete line folding with preceding whitespace",
input: []string{"hello \r\n \tworld"},
result: []string{":aGVsbG8gd29ybGQ=:"},
},
{
uc: "preserve non-ASCII whitespace bytes",
input: []string{"\u00a0alpha"},
result: []string{":wqBhbHBoYQ==:"},
},
} {
t.Run(tc.uc, func(t *testing.T) {
params := httpsfv.NewParams()
Expand Down
Loading