Engine 0.0.4
Game engine in lua
Loading...
Searching...
No Matches
http.lua
Go to the documentation of this file.
1local zeebo_pipeline = require('src/lib/util/pipeline')
2
3--! @defgroup std
4--! @{
5--! @defgroup http
6--! @pre require @c http
7--! @{
8
9--! @short reduced response
10--! @brief disconnect when receiving status
11local function fast(self)
12 self.speed = '_fast'
13 return self
14end
16local function body(self, content)
17 self.body_content=content
18 return self
19end
20
21local function param(self, name, value)
22 local index = #self.param_list + 1
23 self.param_list[index] = name
24 self.param_dict[name] = value
25 return self
26end
27
28local function header(self, name, value)
29 local index = #self.header_list + 1
30 self.header_list[index] = name
31 self.header_dict[name] = value
32 return self
33end
34
35local function success(self, handler_func)
36 self.success_handler = handler_func
37 return self
38end
40local function failed(self, handler_func)
41 self.failed_handler = handler_func
42 return self
43end
44
45local function error(self, handler_func)
46 self.error_handler = handler_func
47 return self
48end
49
50--! @}
51--! @}
52
53--! @cond
54local function request(method, std, game, application, protocol_handler)
55 local callback_handler = application and application.callbacks and application.callbacks.http
56
57 if not callback_handler then
58 callback_handler = function() end
59 end
60
61 return function (url)
62 local self = {
63 -- content
64 url = url,
65 speed = '',
66 method = method,
67 body_content = '',
68 header_list = {},
69 header_dict = {},
70 param_list = {},
71 param_dict = {},
72 callback_handler = callback_handler,
73 success_handler = function () end,
74 failed_handler = function () end,
75 error_handler = function () end,
76 -- objects
77 std = std,
78 game = game,
80 -- functions
81 fast = fast,
82 body = body,
83 param = param,
84 header = header,
86 failed = failed,
87 error = error,
89 -- internal
90 protocol_handler = protocol_handler
91 }
92
93 self.promise = function()
95 end
96
97 self.resolve = function()
99 end
100
101 self.set = function (key, value)
102 self.std.http[key] = value
103 end
104
105 self.pipeline = {
106 -- eval
107 function()
108 local response = self:protocol_handler()
109 end,
110 -- callbacks
111 function()
112 -- global handler
113 self.callback_handler(self.std, self.game)
114 -- local handlers
115 if self.std.http.ok then
116 self.success_handler(self.std, self.game)
117 elseif self.std.http.error or not self.std.http.status then
118 self.error_handler(self.std, self.game)
119 else
120 self.failed_handler(self.std, self.game)
121 end
122 end,
123 -- clean http
124 function ()
125 self.std.http.ok = nil
126 self.std.http.body = nil
127 self.std.http.error = nil
128 self.std.http.status = nil
129 end,
130 -- clean lists
131 function()
132 local index = 1
133 while index <= #self.param_list do
134 self.param_dict[self.param_list[index]] = nil
135 index = index + 1
136 end
137 index = 1
138 while index <= #self.header_list do
139 self.header_dict[self.header_list[index]] = nil
140 index = index + 1
141 end
142 end,
143 -- clean gc
144 function()
145 self.url = nil
146 self.body_content = nil
147 self.param_list = nil
148 self.param_dict = nil
149 self.header_list = nil
150 self.header_dict = nil
151 self.success_handler = nil
152 self.failed_handler = nil
153 self.std = nil
154 self.game = nil
155 self.application = nil
156 self.body = nil
157 self.param = nil
158 self.success = nil
159 self.failed = nil
160 self.run = nil
161 self.set = nil
162 self.promise = nil
163 self.resolve = nil
164 self.protocol_handler = nil
165 self.state = nil
167 end
168 }
169
170 return self
171 end
172end
173--! @endcond
174
175local function install(std, game, application, protocol)
176 local protocol_handler = protocol.handler
177 std = std or {}
178 std.http = std.http or {}
179 std.http.get=request('GET', std, game, application, protocol_handler)
180 std.http.head=request('HEAD', std, game, application, protocol_handler)
181 std.http.post=request('POST', std, game, application, protocol_handler)
182 std.http.put=request('PUT', std, game, application, protocol_handler)
183 std.http.delete=request('DELETE', std, game, application, protocol_handler)
184 std.http.patch=request('PATCH', std, game, application, protocol_handler)
185
186 if protocol.install then
187 protocol.install(std, game, application)
188 end
189
190 return std.http
191end
192
193local P = {
195}
196
197return P
local function run(self)
local function stop(self)
local function resume(self)
local function require(std, game, application)
local function success(self, handler_func)
local function header(self, name, value)
local function body(self, content)
local function error(self, handler_func)
local function fast(self)
reduced response
local function param(self, name, value)
local function failed(self, handler_func)
local application
Definition main.lua:16
local game
Definition main.lua:17
local std
Definition main.lua:18
local function clear(c)
local function http(std, game)
local function install(std, game, application, protocol)
local zeebo_pipeline
Definition http.lua:1