@@ -163,6 +163,20 @@ int ModApiHttp::l_http_fetch_async_get(lua_State *L)
163163 return 1 ;
164164}
165165
166+ int ModApiHttp::l_set_http_api_lua (lua_State *L)
167+ {
168+ NO_MAP_LOCK_REQUIRED ;
169+
170+ // This is called by builtin to give us a function that will later
171+ // populate the http_api table with additional method(s).
172+ // We need this because access to the HTTP api is security-relevant and
173+ // any mod could just mess with a global variable.
174+ luaL_checktype (L, 1 , LUA_TFUNCTION );
175+ lua_rawseti (L, LUA_REGISTRYINDEX , CUSTOM_RIDX_HTTP_API_LUA );
176+
177+ return 0 ;
178+ }
179+
166180int ModApiHttp::l_request_http_api (lua_State *L)
167181{
168182 NO_MAP_LOCK_REQUIRED ;
@@ -205,16 +219,16 @@ int ModApiHttp::l_request_http_api(lua_State *L)
205219 return 1 ;
206220 }
207221
208- lua_getglobal (L, " core " );
209- lua_getfield ( L, -1 , " http_add_fetch " );
222+ lua_rawgeti (L, LUA_REGISTRYINDEX , CUSTOM_RIDX_HTTP_API_LUA );
223+ assert ( lua_isfunction ( L, -1 ) );
210224
211225 lua_newtable (L);
212226 HTTP_API (fetch_async);
213227 HTTP_API (fetch_async_get);
214228
215229 // Stack now looks like this:
216- // <core.http_add_fetch > <table with fetch_async, fetch_async_get>
217- // Now call core.http_add_fetch to append .fetch(request, callback) to table
230+ // <function > <table with fetch_async, fetch_async_get>
231+ // Now call it to append .fetch(request, callback) to table
218232 lua_call (L, 1 , 1 );
219233
220234 return 1 ;
@@ -247,6 +261,7 @@ void ModApiHttp::Initialize(lua_State *L, int top)
247261 API_FCT (get_http_api);
248262 } else {
249263 API_FCT (request_http_api);
264+ API_FCT (set_http_api_lua);
250265 }
251266
252267#endif
0 commit comments