This repository has been archived by the owner on Nov 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
httpserver-request.lua
124 lines (111 loc) · 3.43 KB
/
httpserver-request.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
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
-- httpserver-request
-- Part of nodemcu-httpserver, parses incoming client requests.
-- Author: Marcos Kirsch
local function validateMethod(method)
local httpMethods = {GET=true, HEAD=true, POST=true, PUT=true, DELETE=true, TRACE=true, OPTIONS=true, CONNECT=true, PATCH=true}
-- default for non-existent attributes returns nil, which evaluates to false
return httpMethods[method] --
end
local function uriToFilename(uri)
return "http/" .. string.sub(uri, 2, -1)
end
local function hex_to_char(x)
return string.char(tonumber(x, 16))
end
local function uri_decode(input)
return input:gsub("%+", " "):gsub("%%(%x%x)", hex_to_char)
end
local function parseArgs(args)
local r = {}
local i = 1
if args == nil or args == "" then return r end
for arg in string.gmatch(args, "([^&]+)") do
local name, value = string.match(arg, "(.*)=(.*)")
if name ~= nil then r[name] = uri_decode(value) end
i = i + 1
end
return r
end
local function parseFormData(body)
local data = {}
assert(body~=nil, "empty body")
for kv in body.gmatch(body, "%s*&?([^=]+=[^&]+)") do
local key, value = string.match(kv, "(.*)=(.*)")
-- print("Parsed: " .. key .. " => " .. value)
data[key] = uri_decode(value)
end
return data
end
local function getRequestData(payload)
local requestData
return function ()
if requestData then
return requestData
else
local mimeType = string.match(payload, "Content%-Type: ([%w/-]+)")
local body_start = payload:find("\r\n\r\n", 1, true)
local body = payload:sub(body_start, #payload)
payload = nil
collectgarbage()
if mimeType == "application/json" then
-- print("JSON: " .. body)
requestData = cjson.decode(body)
elseif mimeType == "application/x-www-form-urlencoded" then
requestData = parseFormData(body)
else
requestData = {}
end
return requestData
end
end
end
local function parseUri(uri)
local r = {}
local filename
local ext
local fullExt = {}
if uri == nil then return r end
if uri == "/" then uri = "/index.html" end
questionMarkPos, b, c, d, e, f = uri:find("?")
if questionMarkPos == nil then
r.file = uri:sub(1, questionMarkPos)
r.args = {}
else
r.file = uri:sub(1, questionMarkPos - 1)
r.args = parseArgs(uri:sub(questionMarkPos+1, #uri))
end
filename = r.file
while filename:match("%.") do
filename,ext = filename:match("(.+)%.(.+)")
table.insert(fullExt,1,ext)
end
if #fullExt > 1 and fullExt[#fullExt] == 'gz' then
r.ext = fullExt[#fullExt-1] --
r.isGzipped = true
elseif #fullExt >= 1 then
r.ext = fullExt[#fullExt] --
end
r.isScript = r.ext == "lua" or r.ext == "lc"
r.file = uriToFilename(r.file)
return r
end
-- Parses the client's request. Returns a dictionary containing pretty much everything
-- the server needs to know about the uri.
return function (request)
local r = {}
local e = request:find("\r\n", 1, true)
if not e then
print("httpserver-request.lc: nil e")
r.methodIsValid = false
return r
end
local line = request:sub(1, e - 1)
local _ = {}
local i = {}
_, i, r.method, r.request = line:find("^([A-Z]+) (.-) HTTP/[1-9]+.[0-9]+$")
r.methodIsValid = validateMethod(r.method)
r.uri = parseUri(r.request)
r.getRequestData = getRequestData(request)
collectgarbage()
return r
end