]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Unrestricted HTTP API for Client, Server and Main Menu
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 22 Aug 2020 17:38:36 +0000 (19:38 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 22 Aug 2020 17:38:36 +0000 (19:38 +0200)
builtin/init.lua
clientmods/mods.conf
clientmods/test/init.lua [new file with mode: 0644]
doc/client_lua_api.txt
doc/lua_api.txt
src/script/lua_api/l_http.cpp
src/script/scripting_client.cpp

index 75bb3db8507cbf2bf26f929ac8ad947660c50e39..f76174be7d5c727fe85b5b20a9a83a584e39646d 100644 (file)
@@ -36,7 +36,6 @@ dofile(commonpath .. "misc_helpers.lua")
 
 if INIT == "game" then
        dofile(gamepath .. "init.lua")
-       assert(not core.get_http_api)
 elseif INIT == "mainmenu" then
        local mm_script = core.settings:get("main_menu_script")
        if mm_script and mm_script ~= "" then
index a2a29c06ff30b1fea043721f7ee70cb05b2e75a7..3fcd30dfd583e336abf61f80735b8bf13e531e3c 100644 (file)
@@ -4,4 +4,4 @@ load_mod_respawn = true
 load_mod_inventory = true
 load_mod_commands = true
 load_mod_chat = true
-load_mod_invertory = false
+load_mod_test = true
diff --git a/clientmods/test/init.lua b/clientmods/test/init.lua
new file mode 100644 (file)
index 0000000..3020f9a
--- /dev/null
@@ -0,0 +1,3 @@
+local HTTPApiTable = minetest.get_http_api()
+
+print(dump(HTTPApiTable.fetch_sync({url = "https://example.org"})))
index e2cebcb10aa5ced99508d47935b241bba3325f8c..0b63838b7ccc1fc3da7c8374a0e7f82bf37398af 100644 (file)
@@ -878,6 +878,73 @@ Call these functions only at load time!
 * `minetest.send_respawn()`
     * Sends a respawn request to the server.
 
+### HTTP Requests
+
+* `minetest.get_http_api()`
+    * returns `HTTPApiTable` containing http functions.
+    * The returned table contains the functions `fetch_sync`, `fetch_async` and
+      `fetch_async_get` described below.
+    * Function only exists if minetest server was built with cURL support.
+* `HTTPApiTable.fetch_sync(HTTPRequest req)`: returns HTTPRequestResult
+    * Performs given request synchronously
+* `HTTPApiTable.fetch_async(HTTPRequest req)`: returns handle
+    * Performs given request asynchronously and returns handle for
+      `HTTPApiTable.fetch_async_get`
+* `HTTPApiTable.fetch_async_get(handle)`: returns HTTPRequestResult
+    * Return response data for given asynchronous HTTP request
+
+### `HTTPRequest` definition
+
+Used by `HTTPApiTable.fetch` and `HTTPApiTable.fetch_async`.
+
+    {
+        url = "http://example.org",
+
+        timeout = 10,
+        -- Timeout for connection in seconds. Default is 3 seconds.
+
+        post_data = "Raw POST request data string" OR {field1 = "data1", field2 = "data2"},
+        -- Optional, if specified a POST request with post_data is performed.
+        -- Accepts both a string and a table. If a table is specified, encodes
+        -- table as x-www-form-urlencoded key-value pairs.
+        -- If post_data is not specified, a GET request is performed instead.
+
+        user_agent = "ExampleUserAgent",
+        -- Optional, if specified replaces the default minetest user agent with
+        -- given string
+
+        extra_headers = { "Accept-Language: en-us", "Accept-Charset: utf-8" },
+        -- Optional, if specified adds additional headers to the HTTP request.
+        -- You must make sure that the header strings follow HTTP specification
+        -- ("Key: Value").
+
+        multipart = boolean
+        -- Optional, if true performs a multipart HTTP request.
+        -- Default is false.
+    }
+
+### `HTTPRequestResult` definition
+
+Passed to `HTTPApiTable.fetch` callback. Returned by
+`HTTPApiTable.fetch_async_get`.
+
+    {
+        completed = true,
+        -- If true, the request has finished (either succeeded, failed or timed
+        -- out)
+
+        succeeded = true,
+        -- If true, the request was successful
+
+        timeout = false,
+        -- If true, the request timed out
+
+        code = 200,
+        -- HTTP status code
+
+        data = "response"
+    }
+
 ### Storage API
 * `minetest.get_mod_storage()`:
     * returns reference to mod private `StorageRef`
@@ -1566,3 +1633,4 @@ Same as `image`, but does not accept a `position`; the position is instead deter
         texture = "image.png",
     --  ^ Uses texture (string)
     }
+
index e0c895c97c7c75e1b2b3fdd53546ecba096edba1..353ccc9243d8b1a20ac2931d1eeacbdd1a732d8a 100644 (file)
@@ -5425,28 +5425,72 @@ Schematics
 
 HTTP Requests
 -------------
-
-* `minetest.request_http_api()`:
-    * returns `HTTPApiTable` containing http functions if the calling mod has
-      been granted access by being listed in the `secure.http_mods` or
-      `secure.trusted_mods` setting, otherwise returns `nil`.
-    * The returned table contains the functions `fetch`, `fetch_async` and
+* `minetest.get_http_api()`
+    * returns `HTTPApiTable` containing http functions.
+    * The returned table contains the functions `fetch_sync`, `fetch_async` and
       `fetch_async_get` described below.
-    * Only works at init time and must be called from the mod's main scope
-      (not from a function).
     * Function only exists if minetest server was built with cURL support.
-    * **DO NOT ALLOW ANY OTHER MODS TO ACCESS THE RETURNED TABLE, STORE IT IN
-      A LOCAL VARIABLE!**
-* `HTTPApiTable.fetch(HTTPRequest req, callback)`
-    * Performs given request asynchronously and calls callback upon completion
-    * callback: `function(HTTPRequestResult res)`
-    * Use this HTTP function if you are unsure, the others are for advanced use
+* `HTTPApiTable.fetch_sync(HTTPRequest req)`: returns HTTPRequestResult
+    * Performs given request synchronously
 * `HTTPApiTable.fetch_async(HTTPRequest req)`: returns handle
     * Performs given request asynchronously and returns handle for
       `HTTPApiTable.fetch_async_get`
 * `HTTPApiTable.fetch_async_get(handle)`: returns HTTPRequestResult
     * Return response data for given asynchronous HTTP request
 
+### `HTTPRequest` definition
+
+Used by `HTTPApiTable.fetch` and `HTTPApiTable.fetch_async`.
+
+    {
+        url = "http://example.org",
+
+        timeout = 10,
+        -- Timeout for connection in seconds. Default is 3 seconds.
+
+        post_data = "Raw POST request data string" OR {field1 = "data1", field2 = "data2"},
+        -- Optional, if specified a POST request with post_data is performed.
+        -- Accepts both a string and a table. If a table is specified, encodes
+        -- table as x-www-form-urlencoded key-value pairs.
+        -- If post_data is not specified, a GET request is performed instead.
+
+        user_agent = "ExampleUserAgent",
+        -- Optional, if specified replaces the default minetest user agent with
+        -- given string
+
+        extra_headers = { "Accept-Language: en-us", "Accept-Charset: utf-8" },
+        -- Optional, if specified adds additional headers to the HTTP request.
+        -- You must make sure that the header strings follow HTTP specification
+        -- ("Key: Value").
+
+        multipart = boolean
+        -- Optional, if true performs a multipart HTTP request.
+        -- Default is false.
+    }
+
+### `HTTPRequestResult` definition
+
+Passed to `HTTPApiTable.fetch` callback. Returned by
+`HTTPApiTable.fetch_async_get`.
+
+    {
+        completed = true,
+        -- If true, the request has finished (either succeeded, failed or timed
+        -- out)
+
+        succeeded = true,
+        -- If true, the request was successful
+
+        timeout = false,
+        -- If true, the request timed out
+
+        code = 200,
+        -- HTTP status code
+
+        data = "response"
+    }
+
+
 Storage API
 -----------
 
@@ -8057,60 +8101,6 @@ Used by `minetest.add_particlespawner`.
         -- Otherwise, the default behavior is used. (currently: any random tile)
     }
 
-`HTTPRequest` definition
-------------------------
-
-Used by `HTTPApiTable.fetch` and `HTTPApiTable.fetch_async`.
-
-    {
-        url = "http://example.org",
-
-        timeout = 10,
-        -- Timeout for connection in seconds. Default is 3 seconds.
-
-        post_data = "Raw POST request data string" OR {field1 = "data1", field2 = "data2"},
-        -- Optional, if specified a POST request with post_data is performed.
-        -- Accepts both a string and a table. If a table is specified, encodes
-        -- table as x-www-form-urlencoded key-value pairs.
-        -- If post_data is not specified, a GET request is performed instead.
-
-        user_agent = "ExampleUserAgent",
-        -- Optional, if specified replaces the default minetest user agent with
-        -- given string
-
-        extra_headers = { "Accept-Language: en-us", "Accept-Charset: utf-8" },
-        -- Optional, if specified adds additional headers to the HTTP request.
-        -- You must make sure that the header strings follow HTTP specification
-        -- ("Key: Value").
-
-        multipart = boolean
-        -- Optional, if true performs a multipart HTTP request.
-        -- Default is false.
-    }
-
-`HTTPRequestResult` definition
-------------------------------
-
-Passed to `HTTPApiTable.fetch` callback. Returned by
-`HTTPApiTable.fetch_async_get`.
-
-    {
-        completed = true,
-        -- If true, the request has finished (either succeeded, failed or timed
-        -- out)
-
-        succeeded = true,
-        -- If true, the request was successful
-
-        timeout = false,
-        -- If true, the request timed out
-
-        code = 200,
-        -- HTTP status code
-
-        data = "response"
-    }
-
 Authentication handler definition
 ---------------------------------
 
index ec43bf174d2ba87ee9a5ca77d093219d41a313b1..84837e71b78c29974714cdc90a05e5a63bfea5cd 100644 (file)
@@ -216,18 +216,7 @@ int ModApiHttp::l_get_http_api(lua_State *L)
 void ModApiHttp::Initialize(lua_State *L, int top)
 {
 #if USE_CURL
-
-       bool isMainmenu = false;
-#ifndef SERVER
-       isMainmenu = ModApiBase::getGuiEngine(L) != nullptr;
-#endif
-
-       if (isMainmenu) {
-               API_FCT(get_http_api);
-       } else {
-               API_FCT(request_http_api);
-       }
-
+       API_FCT(get_http_api);
 #endif
 }
 
index d71c79f0da158a43e2befff7c97e6fa630de9714..83ec3e77761d7eac0ac040665545503c59508edd 100644 (file)
@@ -37,6 +37,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "lua_api/l_localplayer.h"
 #include "lua_api/l_camera.h"
 #include "lua_api/l_settings.h"
+#include "lua_api/l_http.h"
 
 ClientScripting::ClientScripting(Client *client):
        ScriptApiBase(ScriptingType::Client)
@@ -80,6 +81,7 @@ void ClientScripting::InitializeModApi(lua_State *L, int top)
 
        ModApiItemMod::Initialize(L, top);
        ModApiUtil::InitializeClient(L, top);
+       ModApiHttp::Initialize(L, top);
        ModApiClient::Initialize(L, top);
        ModApiStorage::Initialize(L, top);
        ModApiEnvMod::InitializeClient(L, top);