int LuaContext::CallHttpGet(lua_State *state) { LuaContext& that = GetLuaContext(state); // Check the types of the arguments int nArgs = lua_gettop(state); if (nArgs != 1 || !lua_isstring(state, 1)) // URL { LOG(ERROR) << "Lua: Bad parameters to HttpGet()"; lua_pushnil(state); return 1; } // Configure the HTTP client class const char* url = lua_tostring(state, 1); that.httpClient_.SetMethod(HttpMethod_Get); that.httpClient_.SetUrl(url); // Do the HTTP GET request if (!that.AnswerHttpQuery(state)) { LOG(ERROR) << "Lua: Error in HttpGet() for URL " << url; lua_pushnil(state); } return 1; }
int LuaContext::CallHttpDelete(lua_State *state) { LuaContext& that = GetLuaContext(state); // Check the types of the arguments int nArgs = lua_gettop(state); if (nArgs != 1 || !lua_isstring(state, 1)) // URL { LOG(ERROR) << "Lua: Bad parameters to HttpDelete()"; lua_pushnil(state); return 1; } // Configure the HTTP client class const char* url = lua_tostring(state, 1); that.httpClient_.SetMethod(HttpMethod_Delete); that.httpClient_.SetUrl(url); // Do the HTTP DELETE request std::string s; if (!that.httpClient_.Apply(s)) { LOG(ERROR) << "Lua: Error in HttpDelete() for URL " << url; lua_pushnil(state); } else { lua_pushstring(state, "SUCCESS"); } return 1; }
int LuaContext::DumpJson(lua_State *state) { LuaContext& that = GetLuaContext(state); int nArgs = lua_gettop(state); if ((nArgs != 1 && nArgs != 2) || (nArgs == 2 && !lua_isboolean(state, 2))) { lua_pushnil(state); return 1; } bool keepStrings = false; if (nArgs == 2) { keepStrings = lua_toboolean(state, 2) ? true : false; } Json::Value json; that.GetJson(json, 1, keepStrings); Json::FastWriter writer; std::string s = writer.write(json); lua_pushlstring(state, s.c_str(), s.size()); return 1; }
int LuaContext::ParseJson(lua_State *state) { LuaContext& that = GetLuaContext(state); int nArgs = lua_gettop(state); if (nArgs != 1 || !lua_isstring(state, 1)) // Password { lua_pushnil(state); return 1; } const char* str = lua_tostring(state, 1); Json::Value value; Json::Reader reader; if (reader.parse(str, str + strlen(str), value)) { that.PushJson(value); } else { lua_pushnil(state); } return 1; }
int LuaContext::DumpJson(lua_State *state) { LuaContext& that = GetLuaContext(state); int nArgs = lua_gettop(state); if (nArgs != 1) { lua_pushnil(state); return 1; } Json::Value json; that.GetJson(json, 1); Json::FastWriter writer; std::string s = writer.write(json); lua_pushlstring(state, s.c_str(), s.size()); return 1; }
int LuaContext::CallHttpPostOrPut(lua_State *state, HttpMethod method) { LuaContext& that = GetLuaContext(state); // Check the types of the arguments int nArgs = lua_gettop(state); if ((nArgs != 1 && nArgs != 2) || !lua_isstring(state, 1) || // URL (nArgs >= 2 && !lua_isstring(state, 2))) // Body data { LOG(ERROR) << "Lua: Bad parameters to HttpPost() or HttpPut()"; lua_pushnil(state); return 1; } // Configure the HTTP client class const char* url = lua_tostring(state, 1); that.httpClient_.SetMethod(method); that.httpClient_.SetUrl(url); if (nArgs >= 2) { that.httpClient_.SetBody(lua_tostring(state, 2)); } else { that.httpClient_.GetBody().clear(); } // Do the HTTP POST/PUT request if (!that.AnswerHttpQuery(state)) { LOG(ERROR) << "Lua: Error in HttpPost() or HttpPut() for URL " << url; lua_pushnil(state); } return 1; }
int LuaContext::SetHttpCredentials(lua_State *state) { LuaContext& that = GetLuaContext(state); // Check the types of the arguments int nArgs = lua_gettop(state); if (nArgs != 2 || !lua_isstring(state, 1) || // Username !lua_isstring(state, 2)) // Password { LOG(ERROR) << "Lua: Bad parameters to SetHttpCredentials()"; } else { // Configure the HTTP client const char* username = lua_tostring(state, 1); const char* password = lua_tostring(state, 2); that.httpClient_.SetCredentials(username, password); } return 0; }
int LuaContext::PrintToLog(lua_State *state) { LuaContext& that = GetLuaContext(state); // http://medek.wordpress.com/2009/02/03/wrapping-lua-errors-and-print-function/ int nArgs = lua_gettop(state); lua_getglobal(state, "tostring"); // Make sure you start at 1 *NOT* 0 for arrays in Lua. std::string result; for (int i = 1; i <= nArgs; i++) { const char *s; lua_pushvalue(state, -1); lua_pushvalue(state, i); lua_call(state, 1, 1); s = lua_tostring(state, -1); if (result.size() > 0) result.append(", "); if (s == NULL) result.append("<No conversion to string>"); else result.append(s); lua_pop(state, 1); } LOG(WARNING) << "Lua says: " << result; that.log_.append(result); that.log_.append("\n"); return 0; }