-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserializer.go
148 lines (116 loc) · 3.13 KB
/
serializer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package httpsig
import (
"errors"
"fmt"
"regexp"
"strings"
"github.com/dunglas/httpsfv"
)
var errSerialization = errors.New("serialization error")
type serializer interface {
serialize(v []string) ([]string, error)
}
//nolint:cyclop
func newSerializer(params *httpsfv.Params, isRequest bool) (serializer, error) {
var (
key string
ok bool
)
if _, hasReqParam := params.Get("req"); hasReqParam && isRequest {
return nil, fmt.Errorf("%w: 'req' parameter not valid for requests",
errSerialization)
}
_, isBs := params.Get("bs")
_, isSf := params.Get("sf")
_, isTr := params.Get("tr")
keyParam, isKey := params.Get("key")
if isTr {
return nil, fmt.Errorf("%w: message trailers are not supported", errSerialization)
}
if isBs && (isSf || isKey) {
return nil, fmt.Errorf("%w: cannot have both 'bs' and 'sf'/'key' parameters",
errSerialization)
}
if isKey {
if key, ok = keyParam.(string); !ok {
return nil, fmt.Errorf("%w: key parameter must be a string", errSerialization)
}
}
switch {
case isSf || isKey:
return &strictSerializer{key: key}, nil
case isBs:
return &byteSequenceSerializer{regex: regexp.MustCompile(`\s+`)}, nil
default:
return &rawSerializer{}, nil
}
}
type rawSerializer struct{}
func (s *rawSerializer) serialize(v []string) ([]string, error) {
return v, nil
}
type byteSequenceSerializer struct {
regex *regexp.Regexp
}
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] = s.regex.ReplaceAllString(strings.TrimSpace(v), " ")
}
item := httpsfv.NewItem([]byte(strings.Join(values, ", ")))
marshalled, err := httpsfv.Marshal(item)
if err != nil {
return nil, fmt.Errorf("%w: %w", errSerialization, err)
}
encoded[i] = marshalled
}
return encoded, nil
}
type strictSerializer struct {
key string
}
func (s *strictSerializer) serialize(v []string) ([]string, error) {
// strict encoding of field
parsed, err := s.parse(v)
if err != nil {
return nil, err
}
if len(s.key) != 0 {
dict, ok := parsed.(*httpsfv.Dictionary)
if !ok {
return nil, fmt.Errorf("%w: unable to parse value as dictionary",
errSerialization)
}
val, ok := dict.Get(s.key)
if !ok {
return nil, fmt.Errorf("%w: unable to find key '%s' in structured field", errSerialization, s.key)
}
marshalled, err := httpsfv.Marshal(val)
if err != nil {
return nil, fmt.Errorf("%w: %w", errSerialization, err)
}
return []string{marshalled}, nil
}
marshalled, err := httpsfv.Marshal(parsed)
if err != nil {
return nil, fmt.Errorf("%w: %w", errSerialization, err)
}
return []string{marshalled}, nil
}
func (s *strictSerializer) parse(values []string) (httpsfv.StructuredFieldValue, error) {
list, err := httpsfv.UnmarshalList(values)
if err == nil {
return list, nil
}
dict, err := httpsfv.UnmarshalDictionary(values)
if err == nil {
return dict, nil
}
item, err := httpsfv.UnmarshalItem(values)
if err == nil {
return item, nil
}
return nil, fmt.Errorf("%w: unable to parse structured header", errSerialization)
}