3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
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"
32 #define HTTP_API(name) \
33 lua_pushstring(L, #name); \
34 lua_pushcfunction(L, l_http_##name); \
38 void ModApiHttp::read_http_fetch_request(lua_State *L, HTTPFetchRequest &req)
40 luaL_checktype(L, 1, LUA_TTABLE);
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))
49 lua_getfield(L, 1, "method");
50 if (lua_isstring(L, -1)) {
51 std::string mth = getstringfield_default(L, 1, "method", "");
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;
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)) {
67 lua_getfield(L, 1, "data");
70 req.method = HTTP_POST;
73 if (lua_istable(L, 2)) {
75 while (lua_next(L, 2) != 0) {
76 req.fields[readParam<std::string>(L, -2)] = readParam<std::string>(L, -1);
79 } else if (lua_isstring(L, 2)) {
80 req.raw_data = readParam<std::string>(L, 2);
85 lua_getfield(L, 1, "extra_headers");
86 if (lua_istable(L, 2)) {
88 while (lua_next(L, 2) != 0) {
89 req.extra_headers.emplace_back(readParam<std::string>(L, -1));
96 void ModApiHttp::push_http_fetch_result(lua_State *L, HTTPFetchResult &res, bool completed)
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);
106 // http_api.fetch_sync(HTTPRequest definition)
107 int ModApiHttp::l_http_fetch_sync(lua_State *L)
109 NO_MAP_LOCK_REQUIRED;
111 HTTPFetchRequest req;
112 read_http_fetch_request(L, req);
114 infostream << "Mod performs HTTP request with URL " << req.url << std::endl;
117 httpfetch_sync(req, res);
119 push_http_fetch_result(L, res, true);
124 // http_api.fetch_async(HTTPRequest definition)
125 int ModApiHttp::l_http_fetch_async(lua_State *L)
127 NO_MAP_LOCK_REQUIRED;
129 HTTPFetchRequest req;
130 read_http_fetch_request(L, req);
132 infostream << "Mod performs HTTP request with URL " << req.url << std::endl;
133 httpfetch_async(req);
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());
140 lua_pushstring(L, caller_handle.c_str());
144 // http_api.fetch_async_get(handle)
145 int ModApiHttp::l_http_fetch_async_get(lua_State *L)
147 NO_MAP_LOCK_REQUIRED;
149 std::string handle_str = luaL_checkstring(L, 1);
151 // Convert hex string back to 64-bit handle
153 std::stringstream handle_conversion_stream;
154 handle_conversion_stream << std::hex << handle_str;
155 handle_conversion_stream >> handle;
158 bool completed = httpfetch_async_get(handle, res);
160 push_http_fetch_result(L, res, completed);
165 int ModApiHttp::l_request_http_api(lua_State *L)
167 NO_MAP_LOCK_REQUIRED;
169 if (!ScriptApiSecurity::checkWhitelisted(L, "secure.http_mods") &&
170 !ScriptApiSecurity::checkWhitelisted(L, "secure.trusted_mods")) {
175 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_HTTP_API_LUA);
176 assert(lua_isfunction(L, -1));
179 HTTP_API(fetch_async);
180 HTTP_API(fetch_async_get);
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
190 int ModApiHttp::l_get_http_api(lua_State *L)
192 NO_MAP_LOCK_REQUIRED;
195 HTTP_API(fetch_async);
196 HTTP_API(fetch_async_get);
197 HTTP_API(fetch_sync);
204 int ModApiHttp::l_set_http_api_lua(lua_State *L)
206 NO_MAP_LOCK_REQUIRED;
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);
220 void ModApiHttp::Initialize(lua_State *L, int top)
224 bool isMainmenu = false;
226 isMainmenu = ModApiBase::getGuiEngine(L) != nullptr;
230 API_FCT(get_http_api);
232 API_FCT(request_http_api);
233 API_FCT(set_http_api_lua);
238 // Define this function anyway so builtin can call it without checking
239 API_FCT(set_http_api_lua);
244 void ModApiHttp::InitializeAsync(lua_State *L, int top)
247 API_FCT(get_http_api);