From 5ca371bb9a65cc322eb8327d81709985daefe173 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Tue, 23 Aug 2016 13:20:43 +0200 Subject: cUrlClient: Exported to Lua API. --- Server/Plugins/Debuggers/Debuggers.lua | 81 +++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) (limited to 'Server/Plugins/Debuggers/Debuggers.lua') diff --git a/Server/Plugins/Debuggers/Debuggers.lua b/Server/Plugins/Debuggers/Debuggers.lua index da375cdff..f405d95ae 100644 --- a/Server/Plugins/Debuggers/Debuggers.lua +++ b/Server/Plugins/Debuggers/Debuggers.lua @@ -61,7 +61,7 @@ function Initialize(a_Plugin) -- TestUUIDFromName() -- TestRankMgr() TestFileExt() - TestFileLastMod() + -- TestFileLastMod() TestPluginInterface() local LastSelfMod = cFile:GetLastModificationTime(a_Plugin:GetLocalFolder() .. "/Debuggers.lua") @@ -2135,6 +2135,35 @@ end +function HandleConsoleTestUrlClient(a_Split, a_EntireCmd) + local url = a_Split[2] or "https://github.com" + local isSuccess, msg = cUrlClient:Get(url, + function (a_Body, a_SecondParam) + if not(a_Body) then + -- An error has occurred, a_SecondParam is the error message + LOG("Error while retrieving URL \"" .. url .. "\": " .. (a_SecondParam or "")) + return + end + -- Body received, a_SecondParam is the HTTP headers dictionary-table + assert(type(a_Body) == "string") + assert(type(a_SecondParam) == "table") + LOG("URL body received, length is " .. string.len(a_Body) .. " bytes and there are these headers:") + for k, v in pairs(a_SecondParam) do + LOG(" \"" .. k .. "\": \"" .. v .. "\"") + end + LOG("(headers list finished)") + end + ) + if not(isSuccess) then + LOG("cUrlClient request failed: " .. (msg or "")) + end + return true +end + + + + + function HandleConsoleTestUrlParser(a_Split, a_EntireCmd) LOG("Testing cUrlParser...") local UrlsToTest = @@ -2262,6 +2291,56 @@ end +function HandleConsoleDownload(a_Split) + -- Check params: + local url = a_Split[2] + local fnam = a_Split[3] + if (not(url) or not(fnam)) then + return true, "Missing parameters. Usage: download " + end + + local callbacks = + { + OnStatusLine = function (self, a_HttpVersion, a_Status, a_Rest) + if (a_Status ~= 200) then + LOG("Cannot download " .. url .. ", HTTP error code " .. a_Status) + return + end + + local f, err = io.open(fnam, "wb") + if not(f) then + LOG("Cannot download " .. url .. ", error opening the file " .. fnam .. ": " .. (err or "")) + return + end + self.m_File = f + end, + + OnBodyData = function (self, a_Data) + if (self.m_File) then + self.m_File:write(a_Data) + end + end, + + OnBodyFinished = function (self) + if (self.m_File) then + self.m_File:close() + LOG("File " .. fnam .. " has been downloaded.") + end + end, + } + + local isSuccess, msg = cUrlClient:Get(url, callbacks) + if not(isSuccess) then + LOG("Cannot start an URL download: " .. (msg or "")) + return true + end + return true +end + + + + + function HandleBlkCmd(a_Split, a_Player) -- Gets info about the block the player is looking at. local World = a_Player:GetWorld(); -- cgit v1.2.3