HTTP API: Allow binary downloads and headers (#8573)
authorSmallJoker <SmallJoker@users.noreply.github.com>
Thu, 6 Jun 2019 17:13:29 +0000 (19:13 +0200)
committerGitHub <noreply@github.com>
Thu, 6 Jun 2019 17:13:29 +0000 (19:13 +0200)
Add minetest.features.httpfetch_binary_data

builtin/game/features.lua
doc/lua_api.txt
src/script/common/c_converter.cpp
src/script/common/c_converter.h
src/script/common/helper.cpp
src/script/lua_api/l_http.cpp

index 8e5104867b78aeda816a8ed4acd7813da9ffc24b..cf21303c7c74420f68ef9d765561e3a5b3a01591 100644 (file)
@@ -12,6 +12,7 @@ core.features = {
        no_chat_message_prediction = true,
        object_use_texture_alpha = true,
        object_independent_selectionbox = true,
+       httpfetch_binary_data = true,
 }
 
 function core.has_feature(arg)
index f29b7d439798996d867447b610dac8661976a4ae..352b04cb0d4347bd0387597f2d3003e8bd85aa64 100644 (file)
@@ -3527,11 +3527,14 @@ Utilities
           -- Chat messages are no longer predicted (0.4.16)
           no_chat_message_prediction = true,
           -- The transparency channel of textures can optionally be used on
-          -- objects (ie: players and lua entities) (5.0)
+          -- objects (ie: players and lua entities) (5.0.0)
           object_use_texture_alpha = true,
           -- Object selectionbox is settable independently from collisionbox
-          -- (5.0)
+          -- (5.0.0)
           object_independent_selectionbox = true,
+          -- Specifies whether binary data can be uploaded or downloaded using
+          -- the HTTP API (5.1.0)
+          httpfetch_binary_data = true,
       }
 
 * `minetest.has_feature(arg)`: returns `boolean, missing_features`
index dfd3f5cea9ed394b0a67c364b14790f965f8dceb..b9d6f0494c6df4755b7083551cca52c5e14c57c6 100644 (file)
@@ -540,9 +540,9 @@ v3s16 getv3s16field_default(lua_State *L, int table,
 }
 
 void setstringfield(lua_State *L, int table,
-               const char *fieldname, const char *value)
+               const char *fieldname, const std::string &value)
 {
-       lua_pushstring(L, value);
+       lua_pushlstring(L, value.c_str(), value.length());
        if(table < 0)
                table -= 1;
        lua_setfield(L, table, fieldname);
index 87bc35ac6d34c4ac6356d21ffceed7ac5141902e..f84494c8d4978f4767de8b40030d0799396b7071 100644 (file)
@@ -91,7 +91,7 @@ std::string        checkstringfield(lua_State *L, int table,
                              const char *fieldname);
 
 void               setstringfield(lua_State *L, int table,
-                             const char *fieldname, const char *value);
+                             const char *fieldname, const std::string &value);
 void               setintfield(lua_State *L, int table,
                              const char *fieldname, int value);
 void               setfloatfield(lua_State *L, int table,
index 59bde57abe6778b1058d9d4b4228ceae83e4101c..f53a2b7e8086d3a3cd4c4eaabd44c2970ec19167 100644 (file)
@@ -107,9 +107,10 @@ template <> v2f LuaHelper::readParam(lua_State *L, int index)
 
 template <> std::string LuaHelper::readParam(lua_State *L, int index)
 {
+       size_t length;
        std::string result;
-       const char *str = luaL_checkstring(L, index);
-       result.append(str);
+       const char *str = luaL_checklstring(L, index, &length);
+       result.assign(str, length);
        return result;
 }
 
index f27f789ada3966708ef460f91f4ce1ffbe3f53b3..2ff651cb527acc65b3d12cba92b36e9f1a258e99 100644 (file)
@@ -53,9 +53,8 @@ void ModApiHttp::read_http_fetch_request(lua_State *L, HTTPFetchRequest &req)
        lua_getfield(L, 1, "post_data");
        if (lua_istable(L, 2)) {
                lua_pushnil(L);
-               while (lua_next(L, 2) != 0)
-               {
-                       req.post_fields[luaL_checkstring(L, -2)] = luaL_checkstring(L, -1);
+               while (lua_next(L, 2) != 0) {
+                       req.post_fields[readParam<std::string>(L, -2)] = readParam<std::string>(L, -1);
                        lua_pop(L, 1);
                }
        } else if (lua_isstring(L, 2)) {
@@ -66,10 +65,8 @@ void ModApiHttp::read_http_fetch_request(lua_State *L, HTTPFetchRequest &req)
        lua_getfield(L, 1, "extra_headers");
        if (lua_istable(L, 2)) {
                lua_pushnil(L);
-               while (lua_next(L, 2) != 0)
-               {
-                       const char *header = luaL_checkstring(L, -1);
-                       req.extra_headers.emplace_back(header);
+               while (lua_next(L, 2) != 0) {
+                       req.extra_headers.emplace_back(readParam<std::string>(L, -1));
                        lua_pop(L, 1);
                }
        }
@@ -83,7 +80,7 @@ void ModApiHttp::push_http_fetch_result(lua_State *L, HTTPFetchResult &res, bool
        setboolfield(L, -1, "timeout", res.timeout);
        setboolfield(L, -1, "completed", completed);
        setintfield(L, -1, "code", res.response_code);
-       setstringfield(L, -1, "data", res.data.c_str());
+       setstringfield(L, -1, "data", res.data);
 }
 
 // http_api.fetch_async(HTTPRequest definition)