Engine 0.0.4
Game engine in lua
Loading...
Searching...
No Matches
http.lua
Go to the documentation of this file.
1local function is_ok(status)
2 return (status and 200 <= status and status < 300) or false
3end
4
5local function is_redirect(status)
6 return (status and 300 <= status and status < 400) or false
7end
8
9local function url_search_param(param_list, param_dict)
10 local index, params = 1, ''
11 while param_list and param_dict and index <= #param_list do
12 local param = param_list[index]
13 local value = param_dict[param]
14 if #params == 0 then
15 params = params..'?'
16 else
17 params = params..'&'
18 end
19 params = params..param..'='..(value or '')
20 index = index + 1
21 end
22 return params
23end
24
25local function create_request(method, uri)
26 local self = {
27 body_content = '',
28 header_list = {},
29 header_dict = {},
30 header_imutable = {}
31 }
32
33 self.add_body_content = function (body)
34 self.body_content = self.body_content..(body or '')
35 return self
36 end
37
38 self.add_imutable_header = function (header, value, cond)
39 if cond == false then return self end
40 if self.header_imutable[header] == nil then
41 self.header_list[#self.header_list + 1] = header
42 self.header_dict[header] = value
43 elseif self.header_imutable[header] == false then
44 self.header_dict[header] = value
45 end
46 self.header_imutable[header] = true
47 return self
48 end
49
50 self.add_mutable_header = function (header, value, cond)
51 if cond == false then return self end
52 if self.header_imutable[header] == nil then
53 self.header_list[#self.header_list + 1] = header
54 self.header_imutable[header] = false
55 self.header_dict[header] = value
56 end
57 return self
58 end
59
60 self.add_custom_headers = function(header_list, header_dict)
61 local index = 1
62 while header_list and #header_list >= index do
63 local header = header_list[index]
64 local value = header_dict[header]
65
66 if self.header_imutable[header] == nil then
67 self.header_list[#self.header_list + 1] = header
68 self.header_imutable[header] = false
69 self.header_dict[header] = value
70 elseif self.header_imutable[header] == false then
71 self.header_dict[header] = value
72 end
73
74 index = index + 1
75 end
76 return self
77 end
78
79 self.to_http_protocol = function ()
80 local index = 1
81 local request = method..' '..uri..' HTTP/1.1\r\n'
82
83 while index <= #self.header_list do
84 local header = self.header_list[index]
85 local value = self.header_dict[header]
86 request = request..header..': '..value..'\r\n'
87 index = index + 1
88 end
89
90 request = request..'\r\n'
91
92 if method ~= 'GET' and method ~= 'HEAD' and #self.body_content > 0 then
93 request = request..self.body_content..'\r\n\r\n'
94 end
95 self = nil
96 return request, function() end
97 end
98
99 self.to_curl_cmd = function ()
100 local index = 1
101 local request = 'curl -L \x2D\x2Dsilent \x2D\x2Dinsecure -w "\n%{http_code}" '
102
103 if method == 'HEAD' then
104 request = request..'\x2D\x2DHEAD '
105 else
106 request = request..'-X '..method..' '
107 end
108
109 while index <= #self.header_list do
110 local header = self.header_list[index]
111 local value = self.header_dict[header]
112 request = request..'-H "'..header..': '..value..'" '
113 index = index + 1
114 end
115
116 if method ~= 'GET' and method ~= 'HEAD' and #self.body_content > 0 then
117 request = request..'-d \''..self.body_content..'\' '
118 end
119
120 request = request..uri
121
122 self = nil
123 return request, function() end
124 end
125
126 return self
127end
128
129return {
130 is_ok=is_ok,
131 is_redirect=is_redirect,
132 url_search_param=url_search_param,
133 create_request=create_request
134}
local function header(self, name, value)
local function body(self, content)
local function param(self, name, value)
create_request
Definition http.lua:29
url_search_param
Definition http.lua:28
is_redirect
Definition http.lua:27
is_ok
Definition http.lua:26