]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_http.cpp
script: Put getGuiEngine() inside a client-only #ifdef
[dragonfireclient.git] / src / script / lua_api / l_http.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "lua_api/l_internal.h"
21 #include "common/c_converter.h"
22 #include "common/c_content.h"
23 #include "lua_api/l_http.h"
24 #include "httpfetch.h"
25 #include "settings.h"
26 #include "debug.h"
27 #include "log.h"
28
29 #include <algorithm>
30 #include <iomanip>
31 #include <cctype>
32
33 #define HTTP_API(name) \
34         lua_pushstring(L, #name); \
35         lua_pushcfunction(L, l_http_##name); \
36         lua_settable(L, -3);
37
38 #if USE_CURL
39 void ModApiHttp::read_http_fetch_request(lua_State *L, HTTPFetchRequest &req)
40 {
41         luaL_checktype(L, 1, LUA_TTABLE);
42
43         req.caller = httpfetch_caller_alloc_secure();
44         getstringfield(L, 1, "url", req.url);
45         lua_getfield(L, 1, "user_agent");
46         if (lua_isstring(L, -1))
47                 req.useragent = getstringfield_default(L, 1, "user_agent", "");
48         lua_pop(L, 1);
49         req.multipart = getboolfield_default(L, 1, "multipart", false);
50         req.timeout = getintfield_default(L, 1, "timeout", 3) * 1000;
51
52         // post_data: if table, post form data, otherwise raw data
53         lua_getfield(L, 1, "post_data");
54         if (lua_istable(L, 2)) {
55                 lua_pushnil(L);
56                 while (lua_next(L, 2) != 0) {
57                         req.post_fields[readParam<std::string>(L, -2)] = readParam<std::string>(L, -1);
58                         lua_pop(L, 1);
59                 }
60         } else if (lua_isstring(L, 2)) {
61                 req.post_data = readParam<std::string>(L, 2);
62         }
63         lua_pop(L, 1);
64
65         lua_getfield(L, 1, "extra_headers");
66         if (lua_istable(L, 2)) {
67                 lua_pushnil(L);
68                 while (lua_next(L, 2) != 0) {
69                         req.extra_headers.emplace_back(readParam<std::string>(L, -1));
70                         lua_pop(L, 1);
71                 }
72         }
73         lua_pop(L, 1);
74 }
75
76 void ModApiHttp::push_http_fetch_result(lua_State *L, HTTPFetchResult &res, bool completed)
77 {
78         lua_newtable(L);
79         setboolfield(L, -1, "succeeded", res.succeeded);
80         setboolfield(L, -1, "timeout", res.timeout);
81         setboolfield(L, -1, "completed", completed);
82         setintfield(L, -1, "code", res.response_code);
83         setstringfield(L, -1, "data", res.data);
84 }
85
86 // http_api.fetch_async(HTTPRequest definition)
87 int ModApiHttp::l_http_fetch_async(lua_State *L)
88 {
89         NO_MAP_LOCK_REQUIRED;
90
91         HTTPFetchRequest req;
92         read_http_fetch_request(L, req);
93
94         infostream << "Mod performs HTTP request with URL " << req.url << std::endl;
95         httpfetch_async(req);
96
97         // Convert handle to hex string since lua can't handle 64-bit integers
98         std::stringstream handle_conversion_stream;
99         handle_conversion_stream << std::hex << req.caller;
100         std::string caller_handle(handle_conversion_stream.str());
101
102         lua_pushstring(L, caller_handle.c_str());
103         return 1;
104 }
105
106 // http_api.fetch_async_get(handle)
107 int ModApiHttp::l_http_fetch_async_get(lua_State *L)
108 {
109         NO_MAP_LOCK_REQUIRED;
110
111         std::string handle_str = luaL_checkstring(L, 1);
112
113         // Convert hex string back to 64-bit handle
114         u64 handle;
115         std::stringstream handle_conversion_stream;
116         handle_conversion_stream << std::hex << handle_str;
117         handle_conversion_stream >> handle;
118
119         HTTPFetchResult res;
120         bool completed = httpfetch_async_get(handle, res);
121
122         push_http_fetch_result(L, res, completed);
123
124         return 1;
125 }
126
127 int ModApiHttp::l_request_http_api(lua_State *L)
128 {
129         NO_MAP_LOCK_REQUIRED;
130
131         // We have to make sure that this function is being called directly by
132         // a mod, otherwise a malicious mod could override this function and
133         // steal its return value.
134         lua_Debug info;
135
136         // Make sure there's only one item below this function on the stack...
137         if (lua_getstack(L, 2, &info)) {
138                 return 0;
139         }
140         FATAL_ERROR_IF(!lua_getstack(L, 1, &info), "lua_getstack() failed");
141         FATAL_ERROR_IF(!lua_getinfo(L, "S", &info), "lua_getinfo() failed");
142
143         // ...and that that item is the main file scope.
144         if (strcmp(info.what, "main") != 0) {
145                 return 0;
146         }
147
148         // Mod must be listed in secure.http_mods or secure.trusted_mods
149         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
150         if (!lua_isstring(L, -1)) {
151                 return 0;
152         }
153
154         std::string mod_name = readParam<std::string>(L, -1);
155         std::string http_mods = g_settings->get("secure.http_mods");
156         http_mods.erase(std::remove(http_mods.begin(), http_mods.end(), ' '), http_mods.end());
157         std::vector<std::string> mod_list_http = str_split(http_mods, ',');
158
159         std::string trusted_mods = g_settings->get("secure.trusted_mods");
160         trusted_mods.erase(std::remove(trusted_mods.begin(), trusted_mods.end(), ' '), trusted_mods.end());
161         std::vector<std::string> mod_list_trusted = str_split(trusted_mods, ',');
162
163         mod_list_http.insert(mod_list_http.end(), mod_list_trusted.begin(), mod_list_trusted.end());
164         if (std::find(mod_list_http.begin(), mod_list_http.end(), mod_name) == mod_list_http.end()) {
165                 lua_pushnil(L);
166                 return 1;
167         }
168
169         lua_getglobal(L, "core");
170         lua_getfield(L, -1, "http_add_fetch");
171
172         lua_newtable(L);
173         HTTP_API(fetch_async);
174         HTTP_API(fetch_async_get);
175
176         // Stack now looks like this:
177         // <core.http_add_fetch> <table with fetch_async, fetch_async_get>
178         // Now call core.http_add_fetch to append .fetch(request, callback) to table
179         lua_call(L, 1, 1);
180
181         return 1;
182 }
183 #endif
184
185 void ModApiHttp::Initialize(lua_State *L, int top)
186 {
187 #if USE_CURL
188         API_FCT(request_http_api);
189 #endif
190 }