http.lua

main 279 lines · 7.6 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1HTTP = {
2 timeout=5,
3 websocketTimeout=15,
4 agent="Mozilla/5.0",
5 imageId=1000,
6 images={},
7 operations={},
8}
9
10function HTTP.get(url, callback)
11 if not g_http or not g_http.get then
12 return error("HTTP.get is not supported")
13 end
14 local operation = g_http.get(url, HTTP.timeout)
15 HTTP.operations[operation] = {type="get", url=url, callback=callback}
16 return operation
17end
18
19function HTTP.getJSON(url, callback)
20 if not g_http or not g_http.get then
21 return error("HTTP.getJSON is not supported")
22 end
23 local operation = g_http.get(url, HTTP.timeout)
24 HTTP.operations[operation] = {type="get", json=true, url=url, callback=callback}
25 return operation
26end
27
28function HTTP.post(url, data, callback)
29 if not g_http or not g_http.post then
30 return error("HTTP.post is not supported")
31 end
32 if type(data) == "table" then
33 data = json.encode(data)
34 end
35 local operation = g_http.post(url, data, HTTP.timeout)
36 HTTP.operations[operation] = {type="post", url=url, callback=callback}
37 return operation
38end
39
40function HTTP.postJSON(url, data, callback)
41 if not g_http or not g_http.post then
42 return error("HTTP.postJSON is not supported")
43 end
44 if type(data) == "table" then
45 data = json.encode(data)
46 end
47 local operation = g_http.post(url, data, HTTP.timeout)
48 HTTP.operations[operation] = {type="post", json=true, url=url, callback=callback}
49 return operation
50end
51
52function HTTP.download(url, file, callback, progressCallback)
53 if not g_http or not g_http.download then
54 return error("HTTP.download is not supported")
55 end
56 local operation = g_http.download(url, file, HTTP.timeout)
57 HTTP.operations[operation] = {type="download", url=url, file=file, callback=callback, progressCallback=progressCallback}
58 return operation
59end
60
61function HTTP.downloadImage(url, callback)
62 if not g_http or not g_http.download then
63 return error("HTTP.downloadImage is not supported")
64 end
65 if HTTP.images[url] ~= nil then
66 if callback then
67 callback('/downloads/' .. HTTP.images[url], nil)
68 end
69 return
70 end
71 local file = "autoimage_" .. HTTP.imageId .. ".png"
72 HTTP.imageId = HTTP.imageId + 1
73 local operation = g_http.download(url, file, HTTP.timeout)
74 HTTP.operations[operation] = {type="image", url=url, file=file, callback=callback}
75 return operation
76end
77
78function HTTP.webSocket(url, callbacks, timeout, jsonWebsocket)
79 if not g_http or not g_http.ws then
80 return error("WebSocket is not supported")
81 end
82 if not timeout or timeout < 1 then
83 timeout = HTTP.websocketTimeout
84 end
85 local operation = g_http.ws(url, timeout)
86 HTTP.operations[operation] = {type="ws", json=jsonWebsocket, url=url, callbacks=callbacks}
87 return {
88 id = operation,
89 url = url,
90 close = function()
91 g_http.wsClose(operation)
92 end,
93 send = function(message)
94 if type(message) == "table" then
95 message = json.encode(message)
96 end
97 g_http.wsSend(operation, message)
98 end
99 }
100end
101HTTP.WebSocket = HTTP.webSocket
102
103function HTTP.webSocketJSON(url, callbacks, timeout)
104 return HTTP.webSocket(url, callbacks, timeout, true)
105end
106HTTP.WebSocketJSON = HTTP.webSocketJSON
107
108function HTTP.cancel(operationId)
109 if not g_http or not g_http.cancel then
110 return
111 end
112 HTTP.operations[operationId] = nil
113 return g_http.cancel(operationId)
114end
115
116function HTTP.onGet(operationId, url, err, data)
117 local operation = HTTP.operations[operationId]
118 if operation == nil then
119 return
120 end
121 if err and err:len() == 0 then
122 err = nil
123 end
124 if not err and operation.json then
125 local status, result = pcall(function() return json.decode(data) end)
126 if not status then
127 err = "JSON ERROR: " .. result
128 if data and data:len() > 0 then
129 err = err .. " (" .. data:sub(1, 100) .. ")"
130 end
131 end
132 data = result
133 end
134 if operation.callback then
135 operation.callback(data, err)
136 end
137end
138
139function HTTP.onGetProgress(operationId, url, progress)
140 local operation = HTTP.operations[operationId]
141 if operation == nil then
142 return
143 end
144
145end
146
147function HTTP.onPost(operationId, url, err, data)
148 local operation = HTTP.operations[operationId]
149 if operation == nil then
150 return
151 end
152 if err and err:len() == 0 then
153 err = nil
154 end
155 if not err and operation.json then
156 local status, result = pcall(function() return json.decode(data) end)
157 if not status then
158 err = "JSON ERROR: " .. result
159 if data and data:len() > 0 then
160 err = err .. " (" .. data:sub(1, 100) .. ")"
161 end
162 end
163 data = result
164 end
165 if operation.callback then
166 operation.callback(data, err)
167 end
168end
169
170function HTTP.onPostProgress(operationId, url, progress)
171 local operation = HTTP.operations[operationId]
172 if operation == nil then
173 return
174 end
175end
176
177function HTTP.onDownload(operationId, url, err, path, checksum)
178 local operation = HTTP.operations[operationId]
179 if operation == nil then
180 return
181 end
182 if err and err:len() == 0 then
183 err = nil
184 end
185 if operation.callback then
186 if operation["type"] == "image" then
187 if not err then
188 HTTP.images[url] = path
189 end
190 operation.callback('/downloads/' .. path, err)
191 else
192 operation.callback(path, checksum, err)
193 end
194 end
195end
196
197function HTTP.onDownloadProgress(operationId, url, progress, speed)
198 local operation = HTTP.operations[operationId]
199 if operation == nil then
200 return
201 end
202 if operation.progressCallback then
203 operation.progressCallback(progress, speed)
204 end
205end
206
207function HTTP.onWsOpen(operationId, message)
208 local operation = HTTP.operations[operationId]
209 if operation == nil then
210 return
211 end
212 if operation.callbacks.onOpen then
213 operation.callbacks.onOpen(message, operationId)
214 end
215end
216
217function HTTP.onWsMessage(operationId, message)
218 local operation = HTTP.operations[operationId]
219 if operation == nil then
220 return
221 end
222 if operation.callbacks.onMessage then
223 if operation.json then
224 local status, result = pcall(function() return json.decode(message) end)
225 local err = nil
226 if not status then
227 err = "JSON ERROR: " .. result
228 if message and message:len() > 0 then
229 err = err .. " (" .. message:sub(1, 100) .. ")"
230 end
231 end
232 if err then
233 if operation.callbacks.onError then
234 operation.callbacks.onError(err, operationId)
235 end
236 else
237 operation.callbacks.onMessage(result, operationId)
238 end
239 else
240 operation.callbacks.onMessage(message, operationId)
241 end
242 end
243end
244
245function HTTP.onWsClose(operationId, message)
246 local operation = HTTP.operations[operationId]
247 if operation == nil then
248 return
249 end
250 if operation.callbacks.onClose then
251 operation.callbacks.onClose(message, operationId)
252 end
253end
254
255function HTTP.onWsError(operationId, message)
256 local operation = HTTP.operations[operationId]
257 if operation == nil then
258 return
259 end
260 if operation.callbacks.onError then
261 operation.callbacks.onError(message, operationId)
262 end
263end
264
265connect(g_http,
266 {
267 onGet = HTTP.onGet,
268 onGetProgress = HTTP.onGetProgress,
269 onPost = HTTP.onPost,
270 onPostProgress = HTTP.onPostProgress,
271 onDownload = HTTP.onDownload,
272 onDownloadProgress = HTTP.onDownloadProgress,
273 onWsOpen = HTTP.onWsOpen,
274 onWsMessage = HTTP.onWsMessage,
275 onWsClose = HTTP.onWsClose,
276 onWsError = HTTP.onWsError,
277 })
278g_http.setUserAgent(HTTP.agent)
279
Top