-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.lua
45 lines (38 loc) · 1.1 KB
/
parser.lua
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
-- Module declaration
local HttpParser = {}
local function isEmpty(s)
return s == nil or s == ''
end
local function split(value, pattern)
local outResults = { }
local start = 1
local splitStart, splitEnd = string.find( value, pattern, start )
while splitStart do
table.insert( outResults, string.sub( value, start, splitStart-1 ) )
start = splitStart + 1
splitStart, splitEnd = string.find( value, pattern, start )
end
table.insert( outResults, string.sub( value, start ) )
return outResults
end
function HttpParser.parse(payload)
local request = {}
local splitPayload = split(payload, "\r\n\r\n")
local httpRequest = split((splitPayload[1]), "\r\n")
if not isEmpty((splitPayload[2])) then
request.content = (splitPayload[2])
end
local splitHeader = split((httpRequest[1]), "%s+")
if ((splitHeader[1]) == 'POST' or (splitHeader[1]) == 'GET')
then
request.method = (splitHeader[1])
request.path = split(splitHeader[2], '/')
request.protocal = (splitHeader[3])
end
httpRequest = nil
splitHeader = nil
splitPayload = nil
collectgarbage()
return request
end
return HttpParser