From c8f13ea311c945a1e3f5d9adcc56d2859ad4c570 Mon Sep 17 00:00:00 2001 From: Malte0621 <50240920+Malte0621@users.noreply.github.com> Date: Sat, 11 Dec 2021 00:03:09 +0100 Subject: [PATCH] V1 - Upload --- info.txt | 4 +-- main.lua | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++---- mhttp.lua | 58 -------------------------------------- shared.lua | 63 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+), 66 deletions(-) delete mode 100644 mhttp.lua create mode 100644 shared.lua diff --git a/info.txt b/info.txt index 2411e97..1a47ddd 100644 --- a/info.txt +++ b/info.txt @@ -1,3 +1,3 @@ -name = ExternalHttp V2 +name = ExternalHttp author = Malte0621 -description = A prototype http requesting mod for teardown. (V2) \ No newline at end of file +description = A prototype http requesting mod for teardown. \ No newline at end of file diff --git a/main.lua b/main.lua index 3b8ee64..7782f9c 100644 --- a/main.lua +++ b/main.lua @@ -1,6 +1,49 @@ -#include "mhttp.lua" +--[[ +Malte0621's ExternalHttp Module. -local testingMode = true +--> Usage +#include "shared.lua" + +local id2 = 0 +local c = 0 + +function tick(dt) + if c >= 60*1.25 then + c = 0 + id2 = id2 + 1 + local function cb(data) + DebugPrint(data["Body"]) + end + http.post("http://localhost/?rnd=" .. id2,{msg = "hello"},cb) + -- http.get("http://localhost/?rnd" .. id2,cb) + else + c = c + 1 + end +end + +--> API +-------------------------- +"shared.lua" +-------------------------- +http = { + get = function(,) + post = function(,,) +} + +callback arguments: +1. { + Success = -- true if request was sent successfully and false if not. + StatusCode = -- HTTP Status code for the requested url (0 if not Success) + StatusMessage = -- HTTP Status message for the requested url ("" if not Success) + Headers = -- Table with all the headers {HeaderName = HeaderValue} + Body = -- The body (text) of the requested url. +} +-------------------------- +]] + +#include "shared.lua" -- for sending requests in a mod. + +local testingMode = false local loaded = false local showTicks = 60*1.5 @@ -23,17 +66,45 @@ local text = "" local c = 0 +SetString("savegame.mod.request","null") + +local id = 0 + +local id2 = 0 -- For sending + function tick(dt) + local tmp = GetString("game.http.request") + if tmp ~= nil and tmp ~= "null" and not tonumber(tmp) then + local split = Split(tmp,"|") + local set = false + id = id + 1 + if #split == 3 and split[1]:sub(1,4) == "http" and split[2] == "POST" and split[3]:sub(1,1) == "{" then + SetString("savegame.mod.request",tmp .. "|" .. id) + set = true + elseif #split == 2 and split[1]:sub(1,4) == "http" and split[2] == "GET" then + SetString("savegame.mod.request",tmp .. "|" .. id) + set = true + end + if set then + SetString("game.http.request",tostring(id)) + else + SetString("game.http.request","null") + end + end if testingMode then if c >= 60*1.25 then c = 0 - -- http.PostAsyncA("http://localhost/",{msg = "hello"},cb) - local body = http.GetAsync("http://localhost/hi.txt") - DebugPrint(body) + id2 = id2 + 1 + local function cb(data) + DebugPrint(data["Body"]) + end + -- http.post("http://localhost/?rnd=" .. id2,{msg = "hello"},cb) + http.get("http://localhost/?rnd=" .. id2,cb) else c = c + 1 end end + http_tick() end function draw() @@ -41,7 +112,7 @@ function draw() UiTranslate((UiWidth() / 2), (UiHeight() - (UiHeight() / 8))) UiAlign("center center") UiFont("bold.ttf", 32) - UiText("ExternalHttp (V2) by Malte0621, Loaded.") + UiText("ExternalHttp by Malte0621, Loaded.") showTicks = showTicks - 1 end end \ No newline at end of file diff --git a/mhttp.lua b/mhttp.lua deleted file mode 100644 index 6167e1c..0000000 --- a/mhttp.lua +++ /dev/null @@ -1,58 +0,0 @@ ---[[ -Malte0621's ExternalHttp Module (V2). - ---> Usage -#include "mhttp.lua" - -local body = http.PostAsyncA("http://localhost/",{msg = "hello"}) -- PostAsyncA urlencodes for you. While PostAsync does not and requires manual urlencoding. --- local body = http.GetAsync("http://localhost/") - ---> API --------------------------- -"mhttp.lua" --------------------------- -http = { - GetAsync = function() -> - PostAsyncA = function(,) -> - - PostAsync = function(,) -> - - UrlEncode = function() -> - UrlDecode = function() -> -} --------------------------- -]] - -local UrlEncode = function(str) - str = string.gsub (str, "([^0-9a-zA-Z !'()*._~-])", -- locale independent - function (c) return string.format ("%%%02X", string.byte(c)) end) - str = string.gsub (str, " ", "+") - return str - end; - local UrlDecode = function(str) - str = string.gsub (str, "+", " ") - str = string.gsub (str, "%%(%x%x)", function(h) return string.char(tonumber(h,16)) end) - return str - end; - -http = { - UrlEncode = UrlEncode; - UrlDecode = UrlDecode; - GetAsync = function(url) - return http_get(url) - end; - PostAsync = function(url,body) - return http_post(url,body) - end; - PostAsyncA = function(url,body) - local data = "" - for k, v in pairs(dataFields) do - data = data .. ("&%s=%s"):format( - UrlEncode(k), - UrlEncode(v) - ) - end - data = data:sub(2) - return http_post(url,data) - end; -} \ No newline at end of file diff --git a/shared.lua b/shared.lua new file mode 100644 index 0000000..d9bd507 --- /dev/null +++ b/shared.lua @@ -0,0 +1,63 @@ +--[[ +Malte0621's ExternalHttp Module. + +Check main.lua for api/usage +]] + +-- Notice: Requests need a randomize due to teardown's cache system. Just add a random mumber within "?rnd=" at the end of the url. +-- http.get("http://localhost/" .. "?rnd=" .. math.random(1,9999)) -- You might have to change ? to & if you're already using ? in the url. +#include "json.lua" + +local callbacks = {} + +local pendingCallbacks = {} + +function http_tick(dt) + local id = tonumber(GetString("game.http.request")) + if id then + callbacks[id] = table.remove(pendingCallbacks,1) + SetString("game.http.request","null") + end + local success = {} + for id,callback in pairs(callbacks) do + local s,f = pcall(function() + dofile("C:\\teardown_http_temp\\http_response" .. tostring(id) .. ".lua") + return result + end) + if s then + callback(json.decode(f)) + table.insert(success,id) + end + end + for i,v in ipairs(success) do + callbacks[v] = nil + end +end + +http = { + get = function(url,callback) + if type(url) ~= "string" then + error("url is required to be set as a table.") + end + if type(callback) ~= "function" then + error("callback is required to be set as a function : function(response) end") + end + -- DebugPrint(url .. "|GET") -- For debugging + SetString("game.http.request",url .. "|GET") + table.insert(pendingCallbacks,callback) + end, + post = function(url,data,callback) + if type(url) ~= "string" then + error("url is required to be set as a table.") + end + if type(data) ~= "table" then + error("data is required to be set as a table.") + end + if type(callback) ~= "function" then + error("callback is required to be set as a function : function(response) end") + end + -- DebugPrint(url .. "|POST|" .. json.encode(data)) -- For debugging + SetString("game.http.request",url .. "|POST|" .. json.encode(data)) + table.insert(pendingCallbacks,callback) + end +} \ No newline at end of file