From 5e0183cc1c7293651c5fd5162ff23a97b294bace Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Fri, 13 Jan 2023 23:43:01 +0100 Subject: [PATCH] Added error checking to Lua libs, added http_failure event to http.requestAsync --- Capy64/Assets/Lua/boot/03_timer.lua | 5 +++++ Capy64/Assets/Lua/boot/04_http.lua | 30 ++++++++++++++++++++++++++--- Capy64/Assets/default.json | 2 +- Capy64/LuaRuntime/Libraries/HTTP.cs | 12 ++++++++++-- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/Capy64/Assets/Lua/boot/03_timer.lua b/Capy64/Assets/Lua/boot/03_timer.lua index cbbb943..fe06efa 100644 --- a/Capy64/Assets/Lua/boot/03_timer.lua +++ b/Capy64/Assets/Lua/boot/03_timer.lua @@ -1,6 +1,11 @@ local timer = require("timer") +local expect = require("expect").expect +local range = require("expect").range function timer.sleep(n) + expect(1, n, "number") + range(1, 1) + local timerId = timer.start(n) repeat local _, par = coroutine.yield("timer") diff --git a/Capy64/Assets/Lua/boot/04_http.lua b/Capy64/Assets/Lua/boot/04_http.lua index ffea64a..020a900 100644 --- a/Capy64/Assets/Lua/boot/04_http.lua +++ b/Capy64/Assets/Lua/boot/04_http.lua @@ -1,19 +1,43 @@ local http = require("http") local event = require("event") +local expect = require("expect").expect function http.request(url, body, headers, options) + expect(1, url, "string") + expect(2, body, "string", "nil") + expect(3, headers, "table", "nil") + expect(4, options, "table", "nil") + + if not http.checkURL(url) then + return nil, "Invalid URL" + end + local requestId = http.requestAsync(url, body, headers, options) - local _, response + local ev, id, par repeat - _, id, response = event.pull("http_response") + ev, id, par = event.pull("http_response", "http_failure") until id == requestId - return response + + if ev == "http_failure" then + return nil, par + end + + return par end function http.get(url, headers, options) + expect(1, url, "string") + expect(2, headers, "table", "nil") + expect(3, options, "table", "nil") + return http.request(url, nil, headers, options) end function http.post(url, body, headers, options) + expect(1, url, "string") + expect(2, body, "string", "nil") + expect(3, headers, "table", "nil") + expect(4, options, "table", "nil") + return http.request(url, body, headers, options) end \ No newline at end of file diff --git a/Capy64/Assets/default.json b/Capy64/Assets/default.json index f5d72f9..648f6a8 100644 --- a/Capy64/Assets/default.json +++ b/Capy64/Assets/default.json @@ -1,6 +1,6 @@ { - "HTTP": { + "Enable": true, "Blacklist": [], "WebSockets": { "Enable": true, diff --git a/Capy64/LuaRuntime/Libraries/HTTP.cs b/Capy64/LuaRuntime/Libraries/HTTP.cs index d92aac6..45dc99a 100644 --- a/Capy64/LuaRuntime/Libraries/HTTP.cs +++ b/Capy64/LuaRuntime/Libraries/HTTP.cs @@ -2,6 +2,7 @@ using Capy64.LuaRuntime.Extensions; using Capy64.LuaRuntime.Handlers; using KeraLua; +using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.IO; @@ -15,7 +16,6 @@ public class HTTP : IPlugin private static IGame _game; private static HttpClient _client; private static long RequestId; - private static List ReadHandles = new(); private readonly LuaRegister[] HttpLib = new LuaRegister[] { @@ -31,7 +31,7 @@ public class HTTP : IPlugin }, new(), }; - public HTTP(IGame game) + public HTTP(IGame game, IConfiguration configuration) { _game = game; RequestId = 0; @@ -60,6 +60,7 @@ public class HTTP : IPlugin private static int L_Request(IntPtr state) { var L = Lua.FromIntPtr(state); + var request = new HttpRequestMessage(); var url = L.CheckString(1); @@ -153,6 +154,13 @@ public class HTTP : IPlugin var reqTask = _client.SendAsync(request); reqTask.ContinueWith(async (task) => { + + if(task.IsFaulted || task.IsCanceled) + { + _game.LuaRuntime.PushEvent("http_failure", requestId, task.Exception?.Message); + return; + } + var response = await task; /*object content; if ((bool)options["binary"])