forked from marcoskirsch/nodemcu-httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver-request.lua
More file actions
102 lines (84 loc) · 2.87 KB
/
httpserver-request.lua
File metadata and controls
102 lines (84 loc) · 2.87 KB
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
-- 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}
if httpMethods[method] then return true else return false end
end
local function uriToFilename(uri)
return "http/" .. string.sub(uri, 2, -1)
end
local function parseArgs(args)
local r = {}; 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] = value end
i = i + 1
end
return r
end
local function parseHeaders(content)
local r = {}
for line in content:gmatch("[^\n]+") do
local _, _, name, value = line:find("^([^:]+)%s*:%s*(.+)")
if name ~= nil then
r[name] = value
end
end
return r
end
local function parseForm(content, headers)
local contentType = headers["Content-Type"]
local r = {}
if contentType ~= nil and contentType:find("x-www-form-urlencoded", 1, true) then
local length = headers["Content-Length"]
if length ~= nil then
r = parseArgs(content:sub(content:len() - length):match("^%s*(.-)%s*$"))
end
end
return r
end
local function parseUri(uri)
local r = {}
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
_, r.ext = r.file:match("(.+)%.(.+)")
r.file = uriToFilename(r.file)
if r.ext == nil then
local supportedExtensions = {"lua", "lc", "html"}
for i,extension in pairs(supportedExtensions) do
local fileExists = file.open(r.file .. "." .. extension, "r")
file.close()
if fileExists then
r.ext = extension
r.file = r.file .. "." .. extension
break
end
end
end
r.isScript = r.ext == "lua" or r.ext == "lc"
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 e = request:find("\n", 1, true)
if not e then return nil end
local line = request:sub(1, e - 1):match("^%s*(.-)%s*$")
local r = {}
_, 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.headers = parseHeaders(request)
r.form = parseForm(request, r.headers)
return r
end