-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
token.go
71 lines (56 loc) · 1.64 KB
/
token.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
package httpsfv
import (
"errors"
"fmt"
"io"
)
// isExtendedTchar checks if c is a valid token character as defined in the spec.
func isExtendedTchar(c byte) bool {
if isAlpha(c) || isDigit(c) {
return true
}
switch c {
case '!', '#', '$', '%', '&', '\'', '*', '+', '-', '.', '^', '_', '`', '|', '~', ':', '/':
return true
}
return false
}
// ErrInvalidTokenFormat is returned when a token format is invalid.
var ErrInvalidTokenFormat = errors.New("invalid token format")
// Token represents a token as defined in
// https://httpwg.org/specs/rfc8941.html#token.
// A specific type is used to distinguish tokens from strings.
type Token string
// marshalSFV serializes as defined in
// https://httpwg.org/specs/rfc8941.html#ser-token.
func (t Token) marshalSFV(b io.StringWriter) error {
if len(t) == 0 {
return fmt.Errorf("a token cannot be empty: %w", ErrInvalidTokenFormat)
}
if !isAlpha(t[0]) && t[0] != '*' {
return fmt.Errorf("a token must start with an alpha character or *: %w", ErrInvalidTokenFormat)
}
for i := 1; i < len(t); i++ {
if !isExtendedTchar(t[i]) {
return fmt.Errorf("the character %c isn't allowed in a token: %w", t[i], ErrInvalidTokenFormat)
}
}
_, err := b.WriteString(string(t))
return err
}
// parseToken parses as defined in
// https://httpwg.org/specs/rfc8941.html#parse-token.
func parseToken(s *scanner) (Token, error) {
if s.eof() || (!isAlpha(s.data[s.off]) && s.data[s.off] != '*') {
return "", &UnmarshalError{s.off, ErrInvalidTokenFormat}
}
start := s.off
s.off++
for !s.eof() {
if !isExtendedTchar(s.data[s.off]) {
break
}
s.off++
}
return Token(s.data[start:s.off]), nil
}