Mercurial > hg > orthanc
changeset 2258:cd70a86618b4
added Http headers support to HttpPost/Get/Put/Delete
author | amazy |
---|---|
date | Thu, 02 Feb 2017 21:09:06 +0100 |
parents | b8e07269da72 |
children | 8233c92b8226 |
files | Core/Lua/LuaContext.cpp Core/Lua/LuaContext.h |
diffstat | 2 files changed, 36 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/Core/Lua/LuaContext.cpp Thu Feb 02 15:08:33 2017 +0100 +++ b/Core/Lua/LuaContext.cpp Thu Feb 02 21:09:06 2017 +0100 @@ -62,7 +62,6 @@ return true; } - LuaContext& LuaContext::GetLuaContext(lua_State *state) { const void* value = GetGlobalVariable(state, "_LuaContext"); @@ -210,14 +209,37 @@ return true; } + void LuaContext::SetHttpHeaders(lua_State *state, int top) + { + this->httpClient_.ClearHeaders(); // always reset headers in case they have been set in a previous request + + if (lua_gettop(state) >= top) + { + Json::Value headers; + this->GetJson(headers, top, true); + + Json::Value::Members members = headers.getMemberNames(); + + for (Json::Value::Members::const_iterator + it = members.begin(); it != members.end(); ++it) + { + this->httpClient_.AddHeader(*it, headers[*it].asString()); + } + LOG(ERROR) << "4"; + } + + } + + 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 + if ((nArgs < 1 or nArgs > 2) || // check args count + !lua_isstring(state, 1)) // URL is a string { LOG(ERROR) << "Lua: Bad parameters to HttpGet()"; lua_pushnil(state); @@ -228,6 +250,8 @@ const char* url = lua_tostring(state, 1); that.httpClient_.SetMethod(HttpMethod_Get); that.httpClient_.SetUrl(url); + that.httpClient_.GetBody().clear(); + that.SetHttpHeaders(state, 2); // Do the HTTP GET request if (!that.AnswerHttpQuery(state)) @@ -247,9 +271,9 @@ // 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 + if ((nArgs < 1 || nArgs > 3) || // check arg count + !lua_isstring(state, 1) || // URL is a string + (nArgs >= 2 && (!lua_isstring(state, 2) && !lua_isnil(state, 2)))) // Body data is null or is a string { LOG(ERROR) << "Lua: Bad parameters to HttpPost() or HttpPut()"; lua_pushnil(state); @@ -260,8 +284,9 @@ const char* url = lua_tostring(state, 1); that.httpClient_.SetMethod(method); that.httpClient_.SetUrl(url); + that.SetHttpHeaders(state, 3); - if (nArgs >= 2) + if (nArgs >= 2 && !lua_isnil(state, 2)) { that.httpClient_.SetBody(lua_tostring(state, 2)); } @@ -299,7 +324,7 @@ // Check the types of the arguments int nArgs = lua_gettop(state); - if (nArgs != 1 || !lua_isstring(state, 1)) // URL + if (nArgs < 1 || nArgs > 2 || !lua_isstring(state, 1)) // URL { LOG(ERROR) << "Lua: Bad parameters to HttpDelete()"; lua_pushnil(state); @@ -310,6 +335,8 @@ const char* url = lua_tostring(state, 1); that.httpClient_.SetMethod(HttpMethod_Delete); that.httpClient_.SetUrl(url); + that.httpClient_.GetBody().clear(); + that.SetHttpHeaders(state, 2); // Do the HTTP DELETE request std::string s;