-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
222 lines (192 loc) · 4.45 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package eventsocket
import (
"bufio"
"bytes"
"net/textproto"
"net/url"
"strconv"
"strings"
)
var (
singleLine = []byte("\n")
doubleLine = []byte("\n\n")
)
const BufferSize = 16 * 1024
type Command struct {
Lock bool
Uuid string
App string
Args string
}
type ESLkv struct {
body textproto.MIMEHeader
shouldEscape bool
}
// EventType indicates how/why this event is being raised
type EventType int
const (
//Parser and Socket errors
EventError EventType = iota
//Client Mode: Connection established / terminated
//Server Mode: New Connection / Lost Connection
EventState
//Library internal, used during auth phase
EventAuth
//Command Reply
EventReply
//API (reloadxml,reloadacl) commands
EventApi
//Received a disconnect notice (linger might still be on)
EventDisconnect
//Subscribed events
EventGeneric
)
type Event struct {
Type EventType
Headers ESLkv
Body []byte
EventBody ESLkv
Success bool
}
// Searches for next packet boundary
// Blocks until we can 'peek' inside the reader
func seeUpcomingHeader(r *bufio.Reader) (bool, error) {
for peekSize := 4; ; peekSize++ {
// This loop stops when Peek returns an error,
// which it does when r's buffer has been filled.
buf, err := r.Peek(peekSize)
if bytes.HasSuffix(buf, doubleLine) {
return true, nil
}
if err != nil {
return false, err
break
}
}
return false, nil
}
// Parses header and body
// Body can be easily converted to json using encoding/json
func parseMessage(r *bufio.Reader) *Event {
var err error
retMsg := new(Event)
retMsg.Headers.body, err = textproto.NewReader(r).ReadMIMEHeader()
if err != nil {
retMsg.Type = EventError
return retMsg
}
bodyLenStr := retMsg.Headers.Get("Content-Length")
if len(bodyLenStr) > 0 {
//has body, go parse it
bodyLen, err := strconv.Atoi(bodyLenStr)
//content length with invalid size
if err != nil {
retMsg.Success = false
return retMsg
}
retMsg.Body = make([]byte, bodyLen)
_, err = r.Peek(bodyLen)
if err != nil {
retMsg.Success = false
return retMsg
}
read, err := r.Read(retMsg.Body)
if err != nil || read != bodyLen {
retMsg.Success = false
return retMsg
}
}
ctype := retMsg.Headers.Get("Content-Type")
switch ctype {
case "auth/request":
retMsg.Type = EventAuth
case "command/reply":
retMsg.Type = EventReply
replyText := retMsg.Headers.Get("Reply-Text")
if strings.Contains(replyText, "+OK") {
retMsg.Success = true
}
if strings.Contains(replyText, "%") {
retMsg.Headers.shouldEscape = true
}
case "text/event-plain":
retMsg.Type = EventGeneric
retMsg.EventBody.shouldEscape = true
retMsg.parseBody()
case "text/event-json", "text/event-xml":
retMsg.Type = EventGeneric
case "text/disconnect-notice":
retMsg.Type = EventDisconnect
case "api/response":
retMsg.Type = EventApi
replyText := string(retMsg.Body)
if strings.Contains(replyText, "+OK") {
retMsg.Success = true
}
}
return retMsg
}
func (evt *Event) parseBody() {
var err error
evt.EventBody.body, err = textproto.NewReader(bufio.NewReader(bytes.NewBuffer(evt.Body))).ReadMIMEHeader()
if err != nil {
evt.Success = false
return
}
}
func localUnescape(s string) string {
x, _ := url.QueryUnescape(s)
return x
}
func (eB *ESLkv) Get(key string) string {
s := eB.body.Get(key)
if eB.shouldEscape {
return localUnescape(s)
}
return s
}
// readMessage blocks until a message is available
func readMessage(rw *bufio.ReadWriter) *Event {
for {
headerReady, err := seeUpcomingHeader(rw.Reader)
if err != nil {
break
}
if headerReady {
//good to parse
message := parseMessage(rw.Reader)
return message
}
}
//returns a zeroed event, which is an error
return new(Event)
}
func sendBytes(rw *bufio.ReadWriter, b []byte) (int, error) {
defer rw.Flush()
return rw.Write(b)
}
// GetExecute formats a dialplan command to be sent over TCP Connection
func (cmd *Command) GetExecute() []byte {
bbuf := bytes.NewBufferString("sendmsg")
if len(cmd.Uuid) > 0 {
bbuf.WriteString(" ")
bbuf.WriteString(cmd.Uuid)
}
bbuf.Write(singleLine)
bbuf.WriteString("call-command: execute")
bbuf.Write(singleLine)
bbuf.WriteString("execute-app-name: ")
bbuf.WriteString(cmd.App)
bbuf.Write(singleLine)
if len(cmd.Args) > 0 {
bbuf.WriteString("execute-app-arg: ")
bbuf.WriteString(cmd.Args)
bbuf.Write(singleLine)
}
if cmd.Lock {
bbuf.WriteString("event-lock: true")
bbuf.Write(singleLine)
}
bbuf.Write(doubleLine)
return bbuf.Bytes()
}