]> git.lizzy.rs Git - minetest.git/blob - src/script/lua_api/l_http.cpp
5566a8523318c75433d6e90dbd70d33fc5fdf574
[minetest.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 "cpp_api/s_security.h"
25 #include "httpfetch.h"
26 #include "settings.h"
27 #include "debug.h"
28 #include "log.h"
29
30 #include <iomanip>
31
32 #define HTTP_API(name) \
33         lua_pushstring(L, #name); \
34         lua_pushcfunction(L, l_http_##name); \
35         lua_settable(L, -3);
36
37 #if USE_CURL
38 void ModApiHttp::read_http_fetch_request(lua_State *L, HTTPFetchRequest &req)
39 {
40         luaL_checktype(L, 1, LUA_TTABLE);
41
42         req.caller = httpfetch_caller_alloc_secure();
43         getstringfield(L, 1, "url", req.url);
44         getstringfield(L, 1, "user_agent", req.useragent);
45         req.multipart = getboolfield_default(L, 1, "multipart", false);
46         if (getintfield(L, 1, "timeout", req.timeout))
47                 req.timeout *= 1000;
48
49         lua_getfield(L, 1, "method");
50         if (lua_isstring(L, -1)) {
51                 std::string mth = getstringfield_default(L, 1, "method", "");
52                 if (mth == "GET")
53                         req.method = HTTP_GET;
54                 else if (mth == "POST")
55                         req.method = HTTP_POST;
56                 else if (mth == "PUT")
57                         req.method = HTTP_PUT;
58                 else if (mth == "DELETE")
59                         req.method = HTTP_DELETE;
60         }
61         lua_pop(L, 1);
62
63         // post_data: if table, post form data, otherwise raw data DEPRECATED use data and method instead
64         lua_getfield(L, 1, "post_data");
65         if (lua_isnil(L, 2)) {
66                 lua_pop(L, 1);
67                 lua_getfield(L, 1, "data");
68         }
69         else {
70                 req.method = HTTP_POST;
71         }
72
73         if (lua_istable(L, 2)) {
74                 lua_pushnil(L);
75                 while (lua_next(L, 2) != 0) {
76                         req.fields[readParam<std::string>(L, -2)] = readParam<std::string>(L, -1);
77                         lua_pop(L, 1);
78                 }
79         } else if (lua_isstring(L, 2)) {
80                 req.raw_data = readParam<std::string>(L, 2);
81         }
82
83         lua_pop(L, 1);
84
85         lua_getfield(L, 1, "extra_headers");
86         if (lua_istable(L, 2)) {
87                 lua_pushnil(L);
88                 while (lua_next(L, 2) != 0) {
89                         req.extra_headers.emplace_back(readParam<std::string>(L, -1));
90                         lua_pop(L, 1);
91                 }
92         }
93         lua_pop(L, 1);
94 }
95
96 void ModApiHttp::push_http_fetch_result(lua_State *L, HTTPFetchResult &res, bool completed)
97 {
98         lua_newtable(L);
99         setboolfield(L, -1, "succeeded", res.succeeded);
100         setboolfield(L, -1, "timeout", res.timeout);
101         setboolfield(L, -1, "completed", completed);
102         setintfield(L, -1, "code", res.response_code);
103         setstringfield(L, -1, "data", res.data);
104 }
105
106 // http_api.fetch_sync(HTTPRequest definition)
107 int ModApiHttp::l_http_fetch_sync(lua_State *L)
108 {
109         NO_MAP_LOCK_REQUIRED;
110
111         HTTPFetchRequest req;
112         read_http_fetch_request(L, req);
113
114         infostream << "Mod performs HTTP request with URL " << req.url << std::endl;
115
116         HTTPFetchResult res;
117         httpfetch_sync(req, res);
118
119         push_http_fetch_result(L, res, true);
120
121         return 1;
122 }
123
124 // http_api.fetch_async(HTTPRequest definition)
125 int ModApiHttp::l_http_fetch_async(lua_State *L)
126 {
127         NO_MAP_LOCK_REQUIRED;
128
129         HTTPFetchRequest req;
130         read_http_fetch_request(L, req);
131
132         infostream << "Mod performs HTTP request with URL " << req.url << std::endl;
133         httpfetch_async(req);
134
135         // Convert handle to hex string since lua can't handle 64-bit integers
136         std::stringstream handle_conversion_stream;
137         handle_conversion_stream << std::hex << req.caller;
138         std::string caller_handle(handle_conversion_stream.str());
139
140         lua_pushstring(L, caller_handle.c_str());
141         return 1;
142 }
143
144 // http_api.fetch_async_get(handle)
145 int ModApiHttp::l_http_fetch_async_get(lua_State *L)
146 {
147         NO_MAP_LOCK_REQUIRED;
148
149         std::string handle_str = luaL_checkstring(L, 1);
150
151         // Convert hex string back to 64-bit handle
152         u64 handle;
153         std::stringstream handle_conversion_stream;
154         handle_conversion_stream << std::hex << handle_str;
155         handle_conversion_stream >> handle;
156
157         HTTPFetchResult res;
158         bool completed = httpfetch_async_get(handle, res);
159
160         push_http_fetch_result(L, res, completed);
161
162         return 1;
163 }
164
165 int ModApiHttp::l_request_http_api(lua_State *L)
166 {
167         NO_MAP_LOCK_REQUIRED;
168
169         if (!ScriptApiSecurity::checkWhitelisted(L, "secure.http_mods") &&
170                         !ScriptApiSecurity::checkWhitelisted(L, "secure.trusted_mods")) {
171                 lua_pushnil(L);
172                 return 1;
173         }
174
175         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_HTTP_API_LUA);
176         assert(lua_isfunction(L, -1));
177
178         lua_newtable(L);
179         HTTP_API(fetch_async);
180         HTTP_API(fetch_async_get);
181
182         // Stack now looks like this:
183         // <function> <table with fetch_async, fetch_async_get>
184         // Now call it to append .fetch(request, callback) to table
185         lua_call(L, 1, 1);
186
187         return 1;
188 }
189
190 int ModApiHttp::l_get_http_api(lua_State *L)
191 {
192         NO_MAP_LOCK_REQUIRED;
193
194         lua_newtable(L);
195         HTTP_API(fetch_async);
196         HTTP_API(fetch_async_get);
197         HTTP_API(fetch_sync);
198
199         return 1;
200 }
201
202 #endif
203
204 int ModApiHttp::l_set_http_api_lua(lua_State *L)
205 {
206         NO_MAP_LOCK_REQUIRED;
207
208 #if USE_CURL
209         // This is called by builtin to give us a function that will later
210         // populate the http_api table with additional method(s).
211         // We need this because access to the HTTP api is security-relevant and
212         // any mod could just mess with a global variable.
213         luaL_checktype(L, 1, LUA_TFUNCTION);
214         lua_rawseti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_HTTP_API_LUA);
215 #endif
216
217         return 0;
218 }
219
220 void ModApiHttp::Initialize(lua_State *L, int top)
221 {
222 #if USE_CURL
223
224         bool isMainmenu = false;
225 #ifndef SERVER
226         isMainmenu = ModApiBase::getGuiEngine(L) != nullptr;
227 #endif
228
229         if (isMainmenu) {
230                 API_FCT(get_http_api);
231         } else {
232                 API_FCT(request_http_api);
233                 API_FCT(set_http_api_lua);
234         }
235
236 #else
237
238         // Define this function anyway so builtin can call it without checking
239         API_FCT(set_http_api_lua);
240
241 #endif
242 }
243
244 void ModApiHttp::InitializeAsync(lua_State *L, int top)
245 {
246 #if USE_CURL
247         API_FCT(get_http_api);
248 #endif
249 }