split http.request into http.get and http.post (for api reasons)

It's cleaner together, but it's not a good api.
This commit is contained in:
jacob1 2020-01-20 00:30:30 -05:00
parent 7c916a528a
commit afefd045c8
2 changed files with 28 additions and 10 deletions

View File

@ -3819,25 +3819,29 @@ static int http_request_finish(lua_State *l)
return 0;
}
static int http_request(lua_State *l)
static int http_request(lua_State *l, bool isPost)
{
ByteString uri(luaL_checkstring(l, 1));
std::map<ByteString, ByteString> post_data;
if (lua_istable(l, 2))
if (isPost)
{
lua_pushnil(l);
while (lua_next(l, 2))
if (lua_istable(l, 2))
{
lua_pushvalue(l, -2);
post_data.emplace(lua_tostring(l, -1), lua_tostring(l, -2));
lua_pop(l, 2);
lua_pushnil(l);
while (lua_next(l, 2))
{
lua_pushvalue(l, -2);
post_data.emplace(lua_tostring(l, -1), lua_tostring(l, -2));
lua_pop(l, 2);
}
}
}
std::map<ByteString, ByteString> headers;
if (lua_istable(l, 3))
if (lua_istable(l, isPost ? 3 : 2))
{
lua_pushnil(l);
while (lua_next(l, 3))
while (lua_next(l, isPost ? 3 : 2))
{
lua_pushvalue(l, -2);
headers.emplace(lua_tostring(l, -1), lua_tostring(l, -2));
@ -3855,6 +3859,17 @@ static int http_request(lua_State *l)
return 1;
}
int LuaScriptInterface::http_get(lua_State * l)
{
return http_request(l, false);
}
int LuaScriptInterface::http_post(lua_State * l)
{
return http_request(l, true);
}
void LuaScriptInterface::initHttpAPI()
{
luaL_newmetatable(l, "HTTPRequest");
@ -3871,7 +3886,8 @@ void LuaScriptInterface::initHttpAPI()
lua_setfield(l, -2, "finish");
lua_setfield(l, -2, "__index");
struct luaL_Reg httpAPIMethods [] = {
{"request", http_request},
{"get", http_get},
{"post", http_post},
{NULL, NULL}
};
luaL_register(l, "http", httpAPIMethods);

View File

@ -181,6 +181,8 @@ class LuaScriptInterface: public CommandInterface
static int event_getmodifiers(lua_State * l);
void initHttpAPI();
static int http_get(lua_State * l);
static int http_post(lua_State * l);
std::vector<LuaSmartRef> lua_el_func_v, lua_gr_func_v, lua_cd_func_v;
std::vector<int> lua_el_mode_v;