Skip to content

Commit 336d04f

Browse files
committed
fix: Preserving HTTP field value bytes in bs serialization
1 parent 59f96b9 commit 336d04f

2 files changed

Lines changed: 36 additions & 7 deletions

File tree

‎serializer.go‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
var errSerialization = errors.New("serialization error")
1313

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

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

5353
for i, sv := range v {
54-
values := strings.Split(sv, ",")
55-
for j, v := range values {
56-
values[j] = byteSequenceWhitespace.ReplaceAllString(strings.TrimSpace(v), " ")
57-
}
58-
59-
item := httpsfv.NewItem([]byte(strings.Join(values, ", ")))
54+
// RFC 9421, Section 2.1.3: preserve field-value bytes except for
55+
// surrounding HTTP whitespace and obsolete line folding.
56+
value := strings.Trim(sv, " \t")
57+
value = byteSequenceObsFold.ReplaceAllString(value, " ")
58+
item := httpsfv.NewItem([]byte(value))
6059

6160
marshalled, err := httpsfv.Marshal(item)
6261
if err != nil {

‎serializer_test.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,36 @@ func TestByteSequenceSerialization(t *testing.T) {
152152
input: []string{"value, with, lots, of, commas"},
153153
result: []string{":dmFsdWUsIHdpdGgsIGxvdHMsIG9mLCBjb21tYXM=:"},
154154
},
155+
{
156+
uc: "preserve internal spaces and commas",
157+
input: []string{"foo , bar"},
158+
result: []string{":Zm9vICwgIGJhcg==:"},
159+
},
160+
{
161+
uc: "preserve internal tabs",
162+
input: []string{"alpha\tbeta"},
163+
result: []string{":YWxwaGEJYmV0YQ==:"},
164+
},
165+
{
166+
uc: "strip surrounding HTTP whitespace",
167+
input: []string{"\t alpha \t"},
168+
result: []string{":YWxwaGE=:"},
169+
},
170+
{
171+
uc: "unfold obsolete line folding",
172+
input: []string{"hello\r\n \tworld"},
173+
result: []string{":aGVsbG8gd29ybGQ=:"},
174+
},
175+
{
176+
uc: "unfold obsolete line folding with preceding whitespace",
177+
input: []string{"hello \r\n \tworld"},
178+
result: []string{":aGVsbG8gd29ybGQ=:"},
179+
},
180+
{
181+
uc: "preserve non-ASCII whitespace bytes",
182+
input: []string{"\u00a0alpha"},
183+
result: []string{":wqBhbHBoYQ==:"},
184+
},
155185
} {
156186
t.Run(tc.uc, func(t *testing.T) {
157187
params := httpsfv.NewParams()

0 commit comments

Comments
 (0)