/
lama2parser.go
268 lines (234 loc) · 6.68 KB
/
lama2parser.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package parser
import (
"errors"
"strings"
"github.com/HexmosTech/gabs/v2"
"github.com/HexmosTech/lama2/utils"
"github.com/rs/zerolog/log"
)
type Lama2Parser struct {
*Parser
Context map[string]bool
MarkRange map[string]int
}
// NewLama2Parser creates a new Lama2Parser
// and initializes it properly
func NewLama2Parser() *Lama2Parser {
p := &Lama2Parser{&Parser{}, make(map[string]bool), make(map[string]int)}
p.Pm = p
p.Init()
return p
}
// Start primarily calls the Lama2File method
func (p *Lama2Parser) Start() (*gabs.Container, error) {
log.Trace().Msg("Within the Start function")
temp := gabs.New()
res, e := p.Match([]string{"Lama2File"})
if e == nil {
temp.Set(res, "value")
temp.Set("Lama2Parser", "type")
return temp, nil
}
return nil, e
}
func (p *Lama2Parser) Lama2File() (*gabs.Container, error) {
// Trying to get:
// PSBlock? Requestor [SPSBlock Requestor]*
log.Trace().Msg("Within Lama2File")
temp := gabs.New()
tempArr, e1 := temp.Array()
if e1 != nil {
return nil, errors.New("couldn't create Array for the parsed data")
}
// optionally match processor
res2, procE1 := p.Match([]string{"Processor"})
if procE1 == nil {
tempArr.ArrayAppend(res2)
}
_, sepE1 := p.Match([]string{"Separator"})
if procE1 != nil && sepE1 == nil {
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1, "Separator without preceding processor block found", []string{})
} else if procE1 == nil && sepE1 != nil {
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1, "Processor without subsequent requestor block found", []string{})
} else {
log.Debug().Str("Found separator", "---").Msg("")
}
// match requester
res3, reqE1 := p.Match([]string{"Requester"})
if reqE1 != nil {
return nil, reqE1
}
tempArr.ArrayAppend(res3)
log.Debug().Str("Parse structure so far", tempArr.String())
// until file is done:
var res4, res5 *gabs.Container
var procE2, e5 error
for p.Pos < p.TotalLen {
_, sepE2 := p.Match([]string{"Separator"})
if sepE2 != nil {
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1, "Separator expected (1)", []string{})
}
// match processor
res4, procE2 = p.Match([]string{"Processor"})
if procE2 != nil {
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1, "Processor expected (2)", []string{})
}
if p.Pos < p.TotalLen {
_, sepE3 := p.Match([]string{"Separator"})
if sepE3 != nil {
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1, "Separator exepcted (3)", []string{})
}
}
// match requester
if p.Pos < p.TotalLen {
res5, e5 = p.Match([]string{"Requester"})
if e5 != nil {
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1, "Request block exepcted (4)", []string{})
}
tempArr.ArrayAppend(res4)
tempArr.ArrayAppend(res5)
}
}
return tempArr, nil
}
func (p *Lama2Parser) Processor() (*gabs.Container, error) {
log.Trace().Msg("Within Processor")
// A Processor cannot start with any of the HTTP Verbs
res := p.LookAhead([]string{"HTTPVerb"})
log.Debug().Bool("HTTPVerb LookAhead result", res)
if res {
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1, "HTTPVerb found at start of block; cannot be a Requestor block", []string{})
}
temp := gabs.New()
res2, _ := p.MatchUntil("\n---\n")
temp.Set("processor", "type")
temp.Set(res2, "value")
log.Debug().Str("Processor block parsed", res2.String()).Msg("")
return temp, nil
}
func (p *Lama2Parser) Separator() (*gabs.Container, error) {
log.Trace().Msg("Within Separator")
temp := gabs.New()
s, e := p.Keyword("---\n", false, false, false)
temp.Set(string(s))
if e != nil {
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1, "Couldnt' find separator", []string{})
}
return temp, nil
}
// Requester applies the rule:
// HTTPVerb Multipart? TheURL Details?
func (p *Lama2Parser) Requester() (*gabs.Container, error) {
log.Trace().Msg("Within Requester")
res, e := p.Match([]string{"HTTPVerb"})
temp := gabs.New()
if e == nil {
temp.Set(res, "verb")
temp.Set("Lama2File", "type")
} else {
return nil, e
}
res, e = p.Match([]string{"Multipart"})
if e == nil {
temp.Set(res, "multipart")
}
res, e = p.Match([]string{"Form"})
if e == nil {
temp.Set(res, "form")
}
res, e = p.Match([]string{"TheURL"})
if e == nil {
temp.Set(res, "url")
} else {
return nil, e
}
res, e = p.Match([]string{"Details"})
if e == nil {
temp.Set(res, "details")
}
return temp, nil
}
func (p *Lama2Parser) TheURL() (*gabs.Container, error) {
res := []string{}
/*
kw, e := p.Keyword("http", true, false, true)
if e == nil {
res = append(res, string(kw))
} else {
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1, "Couldn't find URL (starting with http(s)", []string{})
}
_, e = p.CharClass("s")
if e == nil {
res = append(res, "s")
}
_, e = p.Keyword("://", false, false, true)
if e == nil {
res = append(res, "://")
} else {
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1, "Couldn't find URL (starting with http(s)://", []string{})
}
*/
for {
up, err := p.CharClass("A-Za-z0-9-._~:/?#[]@!$&'()*+,;%=}{")
if err == nil {
res = append(res, string(up))
} else {
break
}
}
val := strings.Join(res, "")
temp := gabs.New()
temp.Set(val, "value")
temp.Set("TheURL", "type")
return temp, nil
}
func (p *Lama2Parser) HTTPVerb() (*gabs.Container, error) {
log.Trace().Msg("Within HTTPVerb")
verbList := []string{
"get", "head", "post", "put",
"delete", "connect", "trace", "patch",
}
for _, k := range verbList {
r, e := p.Keyword(k, true, true, true)
if e == nil {
temp := gabs.New()
temp.Set(string(r), "value")
temp.Set("HTTPVerb", "type")
return temp, nil
}
}
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1,
"Couldn't find the http verb (get/post/put/delete/connect/options/trace/patch/head", []string{})
}
func (p *Lama2Parser) Multipart() (*gabs.Container, error) {
_, e := p.Keyword("multipart", true, true, true)
if e == nil {
temp := gabs.New()
temp.Set(true, "value")
temp.Set("Multipart", "type")
p.Context["multipart"] = true
return temp, nil
}
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1,
"Expected 'multipart', but couldn't find", []string{})
}
func (p *Lama2Parser) Form() (*gabs.Container, error) {
_, e := p.Keyword("form", true, true, true)
if e == nil {
temp := gabs.New()
temp.Set(true, "value")
temp.Set("Form", "type")
p.Context["form"] = true
return temp, nil
}
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1,
"Expected 'form', but couldn't find", []string{})
}
func (p *Lama2Parser) Details() (*gabs.Container, error) {
res, e := p.Match([]string{"HeaderData", "DataHeader"})
if e == nil {
return res, nil
}
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1,
"No details packet found", []string{})
}