diff options
author | SmallJoker <SmallJoker@users.noreply.github.com> | 2019-06-06 19:13:29 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-06 19:13:29 +0200 |
commit | cb00632e23a41d8d171631de9d85e168b251b80e (patch) | |
tree | 684787f3056df0d5ab7b0e1cb504c2b6866ae334 /src/script | |
parent | 7379aa74cf98c7e4c7aa5325ef1531d412a0abac (diff) | |
download | minetest-cb00632e23a41d8d171631de9d85e168b251b80e.tar.gz minetest-cb00632e23a41d8d171631de9d85e168b251b80e.tar.bz2 minetest-cb00632e23a41d8d171631de9d85e168b251b80e.zip |
HTTP API: Allow binary downloads and headers (#8573)
Add minetest.features.httpfetch_binary_data
Diffstat (limited to 'src/script')
-rw-r--r-- | src/script/common/c_converter.cpp | 4 | ||||
-rw-r--r-- | src/script/common/c_converter.h | 2 | ||||
-rw-r--r-- | src/script/common/helper.cpp | 5 | ||||
-rw-r--r-- | src/script/lua_api/l_http.cpp | 13 |
4 files changed, 11 insertions, 13 deletions
diff --git a/src/script/common/c_converter.cpp b/src/script/common/c_converter.cpp index dfd3f5cea..b9d6f0494 100644 --- a/src/script/common/c_converter.cpp +++ b/src/script/common/c_converter.cpp @@ -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); diff --git a/src/script/common/c_converter.h b/src/script/common/c_converter.h index 87bc35ac6..f84494c8d 100644 --- a/src/script/common/c_converter.h +++ b/src/script/common/c_converter.h @@ -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, diff --git a/src/script/common/helper.cpp b/src/script/common/helper.cpp index 59bde57ab..f53a2b7e8 100644 --- a/src/script/common/helper.cpp +++ b/src/script/common/helper.cpp @@ -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; } diff --git a/src/script/lua_api/l_http.cpp b/src/script/lua_api/l_http.cpp index f27f789ad..2ff651cb5 100644 --- a/src/script/lua_api/l_http.cpp +++ b/src/script/lua_api/l_http.cpp @@ -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) |