-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
92 lines (77 loc) · 1.79 KB
/
parser.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
// Copyright 2019, Shulhan <ms@kilabit.info>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dkim
import (
"fmt"
libbytes "github.com/shuLhan/share/lib/bytes"
)
type empty struct{}
// parser for DKIM tags.
//
// Rules,
//
// * Tags MUST NOT duplicate, otherwise entire value is invalid.
// * Unrecognized tags MUST be ignored
// * Tag with an empty value explicitly designates the empty string as the
// value.
//
// Simplified syntax,
//
// tags = tag *( ";" tag )
// tag = [FWS] key [FWS] "=" [FWS] value [FWS]
// key = ALPHA *(ALPHA / DIGIT / "_")
// value = tval *( tval / WSP / FWS )
//
// tval = %x21-3A / %x3C-7E
// FWS = *(*(WSP) "\r\n")
// WSP = " " / "\t"
type parser struct {
tags map[tagKey]empty // Map to check duplicate tags.
parser *libbytes.Parser
}
// newParser create and initialize new parser for DKIM Signature.
func newParser(value []byte) (p *parser) {
p = &parser{
parser: libbytes.NewParser(value, nil),
tags: make(map[tagKey]empty),
}
return p
}
// fetchTag parse and return single tag from reader.
func (p *parser) fetchTag() (t *tag, err error) {
var (
token []byte
d byte
)
p.parser.SetDelimiters([]byte{'='})
token, d = p.parser.ReadNoSpace()
if d == 0 {
return nil, nil
}
if d != '=' {
return nil, fmt.Errorf(`dkim: missing '='`)
}
t, err = newTag(token)
if err != nil {
return nil, err
}
if t == nil {
return nil, nil
}
if t.key != tagUnknown {
var ok bool
_, ok = p.tags[t.key]
if ok {
return nil, fmt.Errorf(`dkim: duplicate tag: '%s'`, token)
}
p.tags[t.key] = empty{}
}
p.parser.SetDelimiters([]byte{';'})
token, _ = p.parser.ReadNoSpace()
err = t.setValue(token)
if err != nil {
return nil, err
}
return t, nil
}