Repository navigation
Expand file tree
/
Copy pathcomponent_identifier.go
More file actions
114 lines (87 loc) · 2.65 KB
/
Copy pathcomponent_identifier.go
File metadata and controls
114 lines (87 loc) · 2.65 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
103
104
105
106
107
108
109
110
111
112
113
114
package httpsig
import (
"bytes"
"fmt"
"strings"
"github.com/dunglas/httpsfv"
)
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{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) {
name, ok := item.Value.(string)
if !ok {
return nil, fmt.Errorf("%w: component name must be a string", ErrInvalidComponentIdentifier)
}
if name != strings.ToLower(name) {
return nil, fmt.Errorf("%w: component name %q must be lowercase", ErrInvalidComponentIdentifier, name)
}
if !strings.HasPrefix(name, "@") && !isValidHTTPFieldName(name) {
return nil, fmt.Errorf("%w: invalid HTTP field name %q", ErrInvalidComponentIdentifier, name)
}
canonicalizer, err := canonicalizerFor(name, item.Params)
if err != nil {
return nil, err
}
return &componentIdentifier{Item: item, c: canonicalizer}, nil
}
// equal reports whether two component identifiers have the same name and
// parameters, regardless of parameter order.
func (c *componentIdentifier) equal(other *componentIdentifier) bool {
if c.Value != other.Value || len(c.Params.Names()) != len(other.Params.Names()) {
return false
}
for _, name := range c.Params.Names() {
actual, present := other.Params.Get(name)
expected, _ := c.Params.Get(name)
if !present || !equalComponentParameterValues(actual, expected) {
return false
}
}
return true
}
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
}
func equalComponentParameterValues(actual, expected any) bool {
if expectedBytes, ok := expected.([]byte); ok {
actualBytes, ok := actual.([]byte)
return ok && bytes.Equal(actualBytes, expectedBytes)
}
return actual == expected
}
// HTTP field names follow the token syntax of RFC 9110, Section 5.1.
func isValidHTTPFieldName(name string) bool {
if name == "" {
return false
}
for i := range len(name) {
ch := name[i]
if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') || strings.IndexByte("!#$%&'*+-.^_`|~", ch) >= 0 {
continue
}
return false
}
return true
}