-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomponent_identifier.go
More file actions
102 lines (77 loc) · 2.1 KB
/
component_identifier.go
File metadata and controls
102 lines (77 loc) · 2.1 KB
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
package httpsig
import (
"encoding/base64"
"fmt"
"strings"
"github.com/dunglas/httpsfv"
)
func quoteIdentifierName(input string) string {
// nothing to do if already quoted
if strings.HasPrefix(input, `"`) {
return input
}
// try to split the structured field
parts := strings.Split(input, ";")
name, rest := parts[0], parts[1:]
// always quote the name
identifier := "\"" + name + "\""
if len(rest) == 0 {
return identifier
}
// just add the rest
return identifier + ";" + strings.Join(rest, ";")
}
func normaliseParams(params *httpsfv.Params) *httpsfv.Params {
if len(params.Names()) == 0 {
return params
}
ps := httpsfv.NewParams()
for _, name := range params.Names() {
value, _ := params.Get(name)
if v, ok := value.([]byte); ok {
encoded := base64.StdEncoding.EncodeToString(v)
ps.Add(name, encoded)
} else if v, ok := value.(httpsfv.Token); ok {
ps.Add(name, string(v))
} else {
ps.Add(name, value)
}
}
return ps
}
type componentIdentifier struct {
httpsfv.Item
c canonicalizer
}
func toComponentIdentifiers(identifiers []string) ([]*componentIdentifier, error) {
cis := make([]*componentIdentifier, 0, len(identifiers))
for _, identifier := range identifiers {
item, err := httpsfv.UnmarshalItem([]string{quoteIdentifierName(identifier)})
if err != nil {
return nil, fmt.Errorf("%w: %s: %w", ErrInvalidComponentIdentifier, identifiers, err)
}
id, err := newComponentIdentifier(item)
if err != nil {
return nil, err
}
cis = append(cis, id)
}
return cis, nil
}
func newComponentIdentifier(item httpsfv.Item) (*componentIdentifier, error) {
// ok if panics
name := strings.ToLower(item.Value.(string)) //nolint: forcetypeassert
canonicalizer, err := canonicalizerFor(name)
if err != nil {
return nil, err
}
item.Params = normaliseParams(item.Params)
return &componentIdentifier{Item: item, c: canonicalizer}, nil
}
func (c *componentIdentifier) createComponent(msg *Message) (component, error) {
values, err := c.c.canonicalize(msg, c.Params)
if err != nil {
return component{}, err
}
return component{key: c, value: values}, nil
}