forked from fhirschmann/vohttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.lua
74 lines (58 loc) · 1.83 KB
/
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
---------------
-- ## HTTP Request Object.
--
-- [Github Page](https://github.com/fhirschmann/vohttp)
--
-- @author Fabian Hirschmann <[email protected]>
-- @copyright 2013
-- @license MIT/X11
vohttp.request = {}
vohttp.request.Request = {}
--- Creates a new empty HTTP Request Object.
-- @param con the connection context
function vohttp.request.Request:new(con)
local new = {}
for k, v in pairs(vohttp.request.Request) do
new[k] = v
end
--- the command set by the client (GET, POST, HEAD).
new.command = nil
--- the connection context.
new.con = con
--- the requested path.
new.path = nil
--- the headers the clients sent.
new.headers = {}
--- the HTTP version used by the client.
new.version = nil
--- the GET data sent by the client.
new.get_data = {}
-- the POST data sent by the client.
new.post_data = {}
return new
end
--- Contructs an already initialized Request from a query with a client.
-- @param query the query with the client (a table of lines)
function vohttp.request.Request:load_query(query)
self.command, self.path, self.version = query[1]:match("(.*) (.*) HTTP/(.*)")
if self.path:find("%?") then
self.path, self.get_data = self.path:match("(.*)%?(.*)")
self.get_data = vohttp.util.decode(self.get_data)
end
for n, h in ipairs(query) do
if n ~= 1 then
local name, value = h:match("(.*): (.*)")
if name then
self.headers[name] = value
end
end
end
if self.command == "POST" then
if self.headers["Content-Type"] == "application/x-www-form-urlencoded" then
self.post_data = vohttp.util.decode(query[table.getn(query)])
else
self.post_data = query[table.getn(query)]
end
end
return self
end