This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathparser.go
173 lines (146 loc) · 4.14 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package octetcounting
import (
"fmt"
"io"
syslog "github.com/influxdata/go-syslog/v3"
"github.com/influxdata/go-syslog/v3/rfc5424"
)
// parser is capable to parse the input stream containing syslog messages with octetcounting framing.
//
// Use NewParser function to instantiate one.
type parser struct {
msglen int64
maxMessageLength int
s Scanner
internal syslog.Machine
last Token
stepback bool // Wheter to retrieve the last token or not
bestEffort bool // Best effort mode flag
emit syslog.ParserListener
}
// NewParser returns a syslog.Parser suitable to parse syslog messages sent with transparent - ie. octet counting (RFC 5425) - framing.
func NewParser(opts ...syslog.ParserOption) syslog.Parser {
p := &parser{
emit: func(*syslog.Result) { /* noop */ },
maxMessageLength: 8192, // size as per RFC5425#section-4.3.1
}
for _, opt := range opts {
p = opt(p).(*parser)
}
// Create internal parser depending on options
if p.bestEffort {
p.internal = rfc5424.NewMachine(rfc5424.WithBestEffort())
} else {
p.internal = rfc5424.NewMachine()
}
return p
}
func (p *parser) WithMaxMessageLength(length int) {
p.maxMessageLength = length
}
// HasBestEffort tells whether the receiving parser has best effort mode on or off.
func (p *parser) HasBestEffort() bool {
return p.bestEffort
}
// WithBestEffort implements the syslog.BestEfforter interface.
//
// The generic options uses it.
func (p *parser) WithBestEffort() {
p.bestEffort = true
}
// WithListener implements the syslog.Parser interface.
//
// The generic options uses it.
func (p *parser) WithListener(f syslog.ParserListener) {
p.emit = f
}
// Parse parses the io.Reader incoming bytes.
//
// It stops parsing when an error regarding RFC 5425 is found.
func (p *parser) Parse(r io.Reader) {
p.s = *NewScanner(r, p.maxMessageLength)
p.run()
}
func (p *parser) run() {
for {
var tok Token
// First token MUST be a MSGLEN
if tok = p.scan(); tok.typ != MSGLEN {
p.emit(&syslog.Result{
Error: fmt.Errorf("found %s, expecting a %s", tok, MSGLEN),
})
break
}
if int(p.s.msglen) > p.maxMessageLength {
p.emit(&syslog.Result{
Error: fmt.Errorf("message too long to parse. was size %d, max length %d", p.s.msglen, p.maxMessageLength),
})
break
}
// Next we MUST see a WS
if tok = p.scan(); tok.typ != WS {
p.emit(&syslog.Result{
Error: fmt.Errorf("found %s, expecting a %s", tok, WS),
})
break
}
// Next we MUST see a SYSLOGMSG with length equal to MSGLEN
if tok = p.scan(); tok.typ != SYSLOGMSG {
e := fmt.Errorf(`found %s after "%s", expecting a %s containing %d octets`, tok, tok.lit, SYSLOGMSG, p.s.msglen)
// Underflow case
if len(tok.lit) < int(p.s.msglen) && p.bestEffort {
// Though MSGLEN was not respected, we try to parse the existing SYSLOGMSG as a RFC5424 syslog message
result := p.parse(tok.lit)
if result.Error == nil {
result.Error = e
}
p.emit(result)
break
}
p.emit(&syslog.Result{
Error: e,
})
break
}
// Parse the SYSLOGMSG literal pretending it is a RFC5424 syslog message
result := p.parse(tok.lit)
if p.bestEffort || result.Error == nil {
p.emit(result)
}
if !p.bestEffort && result.Error != nil {
p.emit(&syslog.Result{Error: result.Error})
break
}
// Next we MUST see an EOF otherwise the parsing we'll start again
if tok = p.scan(); tok.typ == EOF {
break
} else {
p.unscan()
}
}
}
func (p *parser) parse(input []byte) *syslog.Result {
sys, err := p.internal.Parse(input)
return &syslog.Result{
Message: sys,
Error: err,
}
}
// scan returns the next token from the underlying scanner;
// if a token has been unscanned then read that instead.
func (p *parser) scan() Token {
// If we have a token on the buffer, then return it.
if p.stepback {
p.stepback = false
return p.last
}
// Otherwise read the next token from the scanner.
tok := p.s.Scan()
// Save it to the buffer in case we unscan later.
p.last = tok
return tok
}
// unscan pushes the previously read token back onto the buffer.
func (p *parser) unscan() {
p.stepback = true
}