summaryrefslogtreecommitdiffstats
path: root/Server
diff options
context:
space:
mode:
Diffstat (limited to 'Server')
-rw-r--r--Server/Plugins/APIDump/Classes/Network.lua174
-rw-r--r--Server/Plugins/Debuggers/Debuggers.lua81
-rw-r--r--Server/Plugins/Debuggers/Info.lua12
3 files changed, 265 insertions, 2 deletions
diff --git a/Server/Plugins/APIDump/Classes/Network.lua b/Server/Plugins/APIDump/Classes/Network.lua
index 74f0a6488..e517b8022 100644
--- a/Server/Plugins/APIDump/Classes/Network.lua
+++ b/Server/Plugins/APIDump/Classes/Network.lua
@@ -1,7 +1,7 @@
-- Network.lua
--- Defines the documentation for the cNetwork-related classes
+-- Defines the documentation for the cNetwork-related classes and cUrlClient
@@ -365,6 +365,178 @@ g_Server = nil
Send = { Params = "RawData, RemoteHost, RemotePort", Return = "bool", Notes = "Sends the specified raw data (string) to the specified remote host. The RemoteHost can be either a hostname or an IP address; if it is a hostname, the endpoint will queue a DNS lookup first, if it is an IP address, the send operation is executed immediately. Returns true if there was no immediate error, false on any failure. Note that the return value needn't represent whether the packet was actually sent, only if it was successfully queued." },
},
}, -- cUDPEndpoint
+
+
+ cUrlClient =
+ {
+ Desc =
+ [[
+ Implements high-level asynchronous access to URLs, such as downloading webpages over HTTP(S).</p>
+ <p>
+ Note that unlike other languages' URL access libraries, this class implements asynchronous requests.
+ This means that the functions only start a request and return immediately. The request is then
+ fulfilled in the background, while the server continues to run. The response is delivered back to the
+ plugin using callbacks. This allows the plugin to start requests and not block the server until the
+ response is received.</p>
+ <p>
+ The functions that make network requests are all static and have a dual interface. Either you can use
+ a single callback function, which gets called once the entire response is received or an error is
+ encountered. Or you can use a table of callback functions, each function being called whenever the
+ specific event happens during the request and response lifetime. See the Simple Callback and Callback
+ Table chapters later on this page for details and examples.</p>
+ <p>
+ All the request function also support optional parameters for further customization of the request -
+ the Headers parameter specifies additional HTTP headers that are to be sent (as a dictionary-table of
+ key -> value), the RequestBody parameter specifying the optional body of the request (used mainly for
+ POST and PUT requests), and an Options parameter specifying additional options specific to the protocol
+ used.
+ ]],
+
+ AdditionalInfo =
+ {
+ {
+ Header = "Simple Callback",
+ Contents =
+ [[
+ When you don't need fine control for receiving the requests and are interested only in the result,
+ you can use the simple callback approach. Pass a single function as the Callback parameter, the
+ function will get called when the response is fully processed, either with the body of the response,
+ or with an error message:
+<pre class="prettyprint lang-lua">
+cUrlClient:Get(url,
+ function (a_Body, a_Data)
+ if (a_Body) then
+ -- Response received correctly, a_Body contains the entire response body,
+ -- a_Data is a dictionary-table of the response's HTTP headers
+ else
+ -- There was an error, a_Data is the error message string
+ end
+ end
+)
+</pre>
+ ]],
+ },
+ {
+ Header = "Callback Table",
+ Contents =
+ [[
+ To provide complete control over the request and response handling, Cuberite allows plugins to pass
+ a table of callbacks as the Callback parameter. Then the respective functions are called for their
+ respective events during the lifetime of the request and response. This way it is possible to
+ process huge downloads that wouldn't fit into memory otherwise, or display detailed progress.</p>
+ Each callback function receives a "self" as its first parameter, this allows the functions to
+ access the Callback table and any of its other members, allowing the use of Lua object idiom for
+ the table. See <a href="https://forum.cuberite.org/thread-2062.html">this forum post</a> for an
+ example.
+ <p>
+ The following callback functions are used by Cuberite. Any callback that is not assigned is
+ silently ignored. The table may also contain other functions and other values, those are silently
+ ignored.</p>
+ <table>
+ <tr><th>Callback</th><th>Params</th><th>Notes</th></tr>
+ <tr><td>OnConnected</td><td>self, {{cTCPLink}}, RemoteIP, RemotePort</td><td>Called when the connection to the remote host is established. <i>Note that current implementation doesn't provide the {{cTCPLink}} parameter and passes nil instead.</i></td></tr>
+ <tr><td>OnCertificateReceived</td><td>self</td><td>Called for HTTPS URLs when the server's certificate is received. If the callback returns anything else than true, the connection is aborted. <i>Note that the current implementation doesn't provide the certificate because there is no representation for the cert in Lua.</i></td></tr>
+ <tr><td>OnTlsHandshakeCompleted</td><td>self</td><td>Called for HTTPS URLs when the TLS is established on the connection.</td></tr>
+ <tr><td>OnRequestSent</td><td>self</td><td>Called after the entire request is sent to the server.</td></tr>
+ <tr><td>OnStatusLine</td><td>self, HttpVersion, StatusCode, Rest</td><td>The initial line of the response has been parsed. HttpVersion is typically "HTTP/1.1", StatusCode is the numerical HTTP status code reported by the server (200 being OK), Rest is the rest of the line, usually a short message in case of an error.</td></tr>
+ <tr><td>OnHeader</td><td>self, Name, Value</td><td>A new HTTP response header line has been received.</td></tr>
+ <tr><td>OnHeadersFinished</td><td>self, AllHeaders</td><td>All HTTP response headers have been parsed. AllHeaders is a dictionary-table containing all the headers received.</td></tr>
+ <tr><td>OnBodyData</td><td>self, Data</td><td>A piece of the response body has been received. This callback is called repeatedly until the entire body is reported through its Data parameter.</td></tr>
+ <tr><td>OnBodyFinished</td><td>self</td><td>The entire response body has been reported by OnBodyData(), the response has finished.</td></tr>
+ <tr><td>OnError</td><td>self, ErrorMsg</td><td>Called whenever an error is detected. After this call, no other callback will get called.</td></tr>
+ <tr><td>OnRedirecting</td><td>self, NewUrl</td><td>Called if the server returned a valid redirection HTTP status code and a Location header, and redirection is allowed by the Options.</td></tr>
+ </table>
+ <p>
+ The following example is adapted from the Debuggers plugin's "download" command, it downloads the
+ contents of an URL into a file.
+<pre class="prettyprint lang-lua">
+function HandleConsoleDownload(a_Split) -- Console command handler
+ -- Read the params from the command:
+ local url = a_Split[2]
+ local fnam = a_Split[3]
+ if (not(url) or not(fnam)) then
+ return true, "Missing parameters. Usage: download <url> <filename>"
+ end
+
+ -- Define the cUrlClient callbacks
+ local callbacks =
+ {
+ OnStatusLine = function (self, a_HttpVersion, a_Status, a_Rest)
+ -- Only open the output file if the server reports a success:
+ 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 "<no message>"))
+ return
+ end
+ self.m_File = f
+ end,
+
+ OnBodyData = function (self, a_Data)
+ -- If the file has been opened, write the data:
+ if (self.m_File) then
+ self.m_File:write(a_Data)
+ end
+ end,
+
+ OnBodyFinished = function (self)
+ -- If the file has been opened, close it and report success
+ if (self.m_File) then
+ self.m_File:close()
+ LOG("File " .. fnam .. " has been downloaded.")
+ end
+ end,
+ }
+
+ -- Start the URL download:
+ local isSuccess, msg = cUrlClient:Get(url, callbacks)
+ if not(isSuccess) then
+ LOG("Cannot start an URL download: " .. (msg or "<no message>"))
+ return true
+ end
+ return true
+end
+</pre>
+]],
+ },
+ {
+ Header = "Options",
+ Contents =
+ [[
+ The requests support the following options, specified in the optional Options table parameter:
+ <table>
+ <tr><th>Option name</th><th>Description</th></tr>
+ <tr><td>MaxRedirects</td><td>Maximum number of HTTP redirects that the cUrlClient will follow. If the server still reports a redirect after reaching this many redirects, the cUrlClient reports an error. May be specified as either a number or a string parsable into a number. Default: 30.</td></tr>
+ <tr><td>OwnCert</td><td>The client certificate to use, if requested by the server. A string containing a PEM- or DER-encoded cert is expected.</td></tr>
+ <tr><td>OwnPrivKey</td><td>The private key appropriate for OwnCert. A string containing a PEM- or DER-encoded private key is expected.</td></tr>
+ <tr><td>OwnPrivKeyPassword</td><td>The password for OwnPrivKey. If not present or empty, no password is assumed.</td></tr>
+ </table>
+ <p>
+ Redirection:
+ <ul>
+ <li>If a redirect is received, and redirection is allowed by MaxRedirects, the redirection is
+ reported via OnRedirecting() callback and the request is restarted at the redirect URL, without
+ reporting any of the redirect's headers nor body.</li>
+ <li>If a redirect is received and redirection is not allowed (maximum redirection attempts have
+ been reached), the OnRedirecting() callback is called with the redirect URL and then the request
+ terminates with an OnError() callback, without reporting the redirect's headers nor body.</li>
+ </ul>
+ ]],
+ },
+ },
+
+ Functions =
+ {
+ Delete = { Params = "URL, Callbacks, [Headers], [RequestBody], [Options]", Return = "bool, [ErrMsg]", IsStatic = true, Notes = "Starts a HTTP DELETE request. Alias for Request(\"DELETE\", ...). Returns true on succes, false and error message on immediate failure (unparsable URL etc.)."},
+ Get = { Params = "URL, Callbacks, [Headers], [RequestBody], [Options]", Return = "bool, [ErrMsg]", IsStatic = true, Notes = "Starts a HTTP GET request. Alias for Request(\"GET\", ...). Returns true on succes, false and error message on immediate failure (unparsable URL etc.)."},
+ Post = { Params = "URL, Callbacks, [Headers], [RequestBody], [Options]", Return = "bool, [ErrMsg]", IsStatic = true, Notes = "Starts a HTTP POST request. Alias for Request(\"POST\", ...). Returns true on succes, false and error message on immediate failure (unparsable URL etc.)."},
+ Put = { Params = "URL, Callbacks, [Headers], [RequestBody], [Options]", Return = "bool, [ErrMsg]", IsStatic = true, Notes = "Starts a HTTP PUT request. Alias for Request(\"PUT\", ...). Returns true on succes, false and error message on immediate failure (unparsable URL etc.)."},
+ Request = { Params = "Method, URL, Callbacks, [Headers], [RequestBody], [Options]", Return = "bool, [ErrMsg]", IsStatic = true, Notes = "Starts a request with the specified Method. Returns true on succes, false and error message on immediate failure (unparsable URL etc.)."},
+ },
+ }, -- cUrlClient
}
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 "<no message>"))
+ 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 "<no message>"))
+ 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 <url> <filename>"
+ 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 "<no message>"))
+ 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 "<no message>"))
+ 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();
diff --git a/Server/Plugins/Debuggers/Info.lua b/Server/Plugins/Debuggers/Info.lua
index 8fd04d0ae..6c7eabbf2 100644
--- a/Server/Plugins/Debuggers/Info.lua
+++ b/Server/Plugins/Debuggers/Info.lua
@@ -224,6 +224,12 @@ g_PluginInfo =
HelpString = "Performs cBoundingBox API tests",
},
+ ["download"] =
+ {
+ Handler = HandleConsoleDownload,
+ HelpString = "Downloads a file from a specified URL",
+ },
+
["hash"] =
{
Handler = HandleConsoleHash,
@@ -278,6 +284,12 @@ g_PluginInfo =
HelpString = "Tests the cLineBlockTracer",
},
+ ["testurlclient"] =
+ {
+ Handler = HandleConsoleTestUrlClient,
+ HelpString = "Tests the cUrlClient",
+ },
+
["testurlparser"] =
{
Handler = HandleConsoleTestUrlParser,