Engine 0.0.4
Game engine in lua
Loading...
Searching...
No Matches
http_curl_love.lua
Go to the documentation of this file.
1local http_util = require('src/lib/util/http')
2
3local function http_handler(self)
4 local params = http_util.url_search_param(self.param_list, self.param_dict)
5 local command, cleanup = http_util.create_request(self.method, self.url..params)
6 .add_custom_headers(self.header_list, self.header_dict)
7 .add_body_content(self.body_content)
8 .to_curl_cmd()
9
10 local threadCode = 'local command, channel = ...\n'
11 ..'local handle = io and io.popen and io.popen(command)\n'
12 ..'if handle then\n'
13 ..' local stdout = handle:read(\'*a\')\n'
14 ..' local ok, stderr = handle:close()\n'
15 ..' love.thread.getChannel(channel..\'ok\'):push(ok)\n'
16 ..' love.thread.getChannel(channel..\'stdout\'):push(stdout)\n'
17 ..' love.thread.getChannel(channel..\'stderr\'):push(stderr)\n'
18 ..'else\n'
19 ..' love.thread.getChannel(channel..\'ok\'):push(false)\n'
20 ..' love.thread.getChannel(channel..\'stderr\'):push(\'command failed\')\n'
21 ..'end'
22
23 self.promise()
24 self.application.internal.http.queue[#self.application.internal.http.queue + 1] = self
25 thread = love.thread.newThread(threadCode)
26 thread:start(command, tostring(self))
27
28 cleanup()
29end
30
31local function http_callback(self)
32 local channel = tostring(self)
33 local ok = love.thread.getChannel(channel..'ok'):pop()
34 if ok ~= nil then
35 local stdout = love.thread.getChannel(channel..'stdout'):pop() or ''
36 local stderr = love.thread.getChannel(channel..'stderr'):pop() or ''
37 local index = stdout:find("[^\n]*$") or 1
38 local status = tonumber(stdout:sub(index))
39 if not ok then
40 self.std.http.ok = false
41 self.std.http.error = stderr or stdout or 'unknown error!'
42 else
43 self.std.http.ok = http_util.is_ok(status)
44 self.std.http.body = stdout:sub(1, index - 2)
45 self.std.http.status = status
46 end
47 self.resolve()
48 return true
49 end
50end
51
52local function install(std, game, application)
53 application.callbacks.loop = application.callbacks.loop or function () end
54 application.internal = application.internal or {}
55 application.internal.http = {queue = {}}
56
57 local update = function()
58 local index = 1
59 while index <= #application.internal.http.queue do
60 if http_callback(application.internal.http.queue[index]) then
61 table.remove(application.internal.http.queue, index)
62 end
63 index = index + 1
64 end
65 end
66
67 if love then
68 if love.update then
69 local old_update = love.update
70 love.update = function(dt)
71 old_update(dt)
72 update()
73 end
74 else
75 love.update = update
76 end
77 end
78
79 return {update=update}
80end
81
82local P = {
83 handler=http_handler,
85}
86
87return P
local function stdout(self, format)
local function install(std, game, application)
local function http_callback(self)
local function http_handler(self)
local http_util
local function require(std, game, application)
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 update(dt, std)
local function http(std, game)
create_request
Definition http.lua:29
url_search_param
Definition http.lua:28