comparison Core/Lua/LuaContext.cpp @ 2258:cd70a86618b4

added Http headers support to HttpPost/Get/Put/Delete
author amazy
date Thu, 02 Feb 2017 21:09:06 +0100
parents a3a65de1840f
children c4b16f107f19
comparison
equal deleted inserted replaced
2257:b8e07269da72 2258:cd70a86618b4
60 } 60 }
61 61
62 return true; 62 return true;
63 } 63 }
64 64
65
66 LuaContext& LuaContext::GetLuaContext(lua_State *state) 65 LuaContext& LuaContext::GetLuaContext(lua_State *state)
67 { 66 {
68 const void* value = GetGlobalVariable(state, "_LuaContext"); 67 const void* value = GetGlobalVariable(state, "_LuaContext");
69 assert(value != NULL); 68 assert(value != NULL);
70 69
208 lua_pushlstring(state, str.c_str(), str.size()); 207 lua_pushlstring(state, str.c_str(), str.size());
209 208
210 return true; 209 return true;
211 } 210 }
212 211
212 void LuaContext::SetHttpHeaders(lua_State *state, int top)
213 {
214 this->httpClient_.ClearHeaders(); // always reset headers in case they have been set in a previous request
215
216 if (lua_gettop(state) >= top)
217 {
218 Json::Value headers;
219 this->GetJson(headers, top, true);
220
221 Json::Value::Members members = headers.getMemberNames();
222
223 for (Json::Value::Members::const_iterator
224 it = members.begin(); it != members.end(); ++it)
225 {
226 this->httpClient_.AddHeader(*it, headers[*it].asString());
227 }
228 LOG(ERROR) << "4";
229 }
230
231 }
213 232
233
234
214 int LuaContext::CallHttpGet(lua_State *state) 235 int LuaContext::CallHttpGet(lua_State *state)
215 { 236 {
216 LuaContext& that = GetLuaContext(state); 237 LuaContext& that = GetLuaContext(state);
217 238
218 // Check the types of the arguments 239 // Check the types of the arguments
219 int nArgs = lua_gettop(state); 240 int nArgs = lua_gettop(state);
220 if (nArgs != 1 || !lua_isstring(state, 1)) // URL 241 if ((nArgs < 1 or nArgs > 2) || // check args count
242 !lua_isstring(state, 1)) // URL is a string
221 { 243 {
222 LOG(ERROR) << "Lua: Bad parameters to HttpGet()"; 244 LOG(ERROR) << "Lua: Bad parameters to HttpGet()";
223 lua_pushnil(state); 245 lua_pushnil(state);
224 return 1; 246 return 1;
225 } 247 }
226 248
227 // Configure the HTTP client class 249 // Configure the HTTP client class
228 const char* url = lua_tostring(state, 1); 250 const char* url = lua_tostring(state, 1);
229 that.httpClient_.SetMethod(HttpMethod_Get); 251 that.httpClient_.SetMethod(HttpMethod_Get);
230 that.httpClient_.SetUrl(url); 252 that.httpClient_.SetUrl(url);
253 that.httpClient_.GetBody().clear();
254 that.SetHttpHeaders(state, 2);
231 255
232 // Do the HTTP GET request 256 // Do the HTTP GET request
233 if (!that.AnswerHttpQuery(state)) 257 if (!that.AnswerHttpQuery(state))
234 { 258 {
235 LOG(ERROR) << "Lua: Error in HttpGet() for URL " << url; 259 LOG(ERROR) << "Lua: Error in HttpGet() for URL " << url;
245 { 269 {
246 LuaContext& that = GetLuaContext(state); 270 LuaContext& that = GetLuaContext(state);
247 271
248 // Check the types of the arguments 272 // Check the types of the arguments
249 int nArgs = lua_gettop(state); 273 int nArgs = lua_gettop(state);
250 if ((nArgs != 1 && nArgs != 2) || 274 if ((nArgs < 1 || nArgs > 3) || // check arg count
251 !lua_isstring(state, 1) || // URL 275 !lua_isstring(state, 1) || // URL is a string
252 (nArgs >= 2 && !lua_isstring(state, 2))) // Body data 276 (nArgs >= 2 && (!lua_isstring(state, 2) && !lua_isnil(state, 2)))) // Body data is null or is a string
253 { 277 {
254 LOG(ERROR) << "Lua: Bad parameters to HttpPost() or HttpPut()"; 278 LOG(ERROR) << "Lua: Bad parameters to HttpPost() or HttpPut()";
255 lua_pushnil(state); 279 lua_pushnil(state);
256 return 1; 280 return 1;
257 } 281 }
258 282
259 // Configure the HTTP client class 283 // Configure the HTTP client class
260 const char* url = lua_tostring(state, 1); 284 const char* url = lua_tostring(state, 1);
261 that.httpClient_.SetMethod(method); 285 that.httpClient_.SetMethod(method);
262 that.httpClient_.SetUrl(url); 286 that.httpClient_.SetUrl(url);
263 287 that.SetHttpHeaders(state, 3);
264 if (nArgs >= 2) 288
289 if (nArgs >= 2 && !lua_isnil(state, 2))
265 { 290 {
266 that.httpClient_.SetBody(lua_tostring(state, 2)); 291 that.httpClient_.SetBody(lua_tostring(state, 2));
267 } 292 }
268 else 293 else
269 { 294 {
297 { 322 {
298 LuaContext& that = GetLuaContext(state); 323 LuaContext& that = GetLuaContext(state);
299 324
300 // Check the types of the arguments 325 // Check the types of the arguments
301 int nArgs = lua_gettop(state); 326 int nArgs = lua_gettop(state);
302 if (nArgs != 1 || !lua_isstring(state, 1)) // URL 327 if (nArgs < 1 || nArgs > 2 || !lua_isstring(state, 1)) // URL
303 { 328 {
304 LOG(ERROR) << "Lua: Bad parameters to HttpDelete()"; 329 LOG(ERROR) << "Lua: Bad parameters to HttpDelete()";
305 lua_pushnil(state); 330 lua_pushnil(state);
306 return 1; 331 return 1;
307 } 332 }
308 333
309 // Configure the HTTP client class 334 // Configure the HTTP client class
310 const char* url = lua_tostring(state, 1); 335 const char* url = lua_tostring(state, 1);
311 that.httpClient_.SetMethod(HttpMethod_Delete); 336 that.httpClient_.SetMethod(HttpMethod_Delete);
312 that.httpClient_.SetUrl(url); 337 that.httpClient_.SetUrl(url);
338 that.httpClient_.GetBody().clear();
339 that.SetHttpHeaders(state, 2);
313 340
314 // Do the HTTP DELETE request 341 // Do the HTTP DELETE request
315 std::string s; 342 std::string s;
316 if (!that.httpClient_.Apply(s)) 343 if (!that.httpClient_.Apply(s))
317 { 344 {