changeset 418:b79bf2f4ab2e

execution of lua through REST
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 May 2013 12:03:25 +0200
parents 7441037663cd
children ab070786f478
files Core/HttpServer/MongooseServer.cpp Core/Lua/LuaContext.cpp Core/Lua/LuaContext.h OrthancServer/OrthancRestApi.cpp Resources/Toolbox.lua
diffstat 5 files changed, 56 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/Core/HttpServer/MongooseServer.cpp	Tue May 07 10:31:32 2013 +0200
+++ b/Core/HttpServer/MongooseServer.cpp	Tue May 07 12:03:25 2013 +0200
@@ -303,6 +303,7 @@
       {
         return PostDataStatus_Failure;
       }
+
       assert(r <= length);
       length -= r;
       pos += r;
--- a/Core/Lua/LuaContext.cpp	Tue May 07 10:31:32 2013 +0200
+++ b/Core/Lua/LuaContext.cpp	Tue May 07 12:03:25 2013 +0200
@@ -42,11 +42,18 @@
 
 namespace Orthanc
 {
-  int LuaContext::PrintToLog(lua_State *L)
+  int LuaContext::PrintToLog(lua_State *state)
   {
+    // Get the pointer to the "LuaContext" underlying object
+    lua_getglobal(state, "_LuaContext");
+    assert(lua_type(state, -1) == LUA_TLIGHTUSERDATA);
+    LuaContext* that = const_cast<LuaContext*>(reinterpret_cast<const LuaContext*>(lua_topointer(state, -1)));
+    assert(that != NULL);
+    lua_pop(state, 1);
+
     // http://medek.wordpress.com/2009/02/03/wrapping-lua-errors-and-print-function/
-    int nArgs = lua_gettop(L);
-    lua_getglobal(L, "tostring");
+    int nArgs = lua_gettop(state);
+    lua_getglobal(state, "tostring");
 
     // Make sure you start at 1 *NOT* 0 for arrays in Lua.
     std::string result;
@@ -54,10 +61,10 @@
     for (int i = 1; i <= nArgs; i++)
     {
       const char *s;
-      lua_pushvalue(L, -1);
-      lua_pushvalue(L, i);
-      lua_call(L, 1, 1);
-      s = lua_tostring(L, -1);
+      lua_pushvalue(state, -1);
+      lua_pushvalue(state, i);
+      lua_call(state, 1, 1);
+      s = lua_tostring(state, -1);
 
       if (result.size() > 0)
         result.append(", ");
@@ -67,10 +74,12 @@
       else
         result.append(s);
  
-      lua_pop(L, 1);
+      lua_pop(state, 1);
     }
 
     LOG(INFO) << "Lua says: " << result;         
+    that->log_.append(result);
+    that->log_.append("\n");
 
     return 0;
   }
@@ -86,6 +95,9 @@
 
     luaL_openlibs(lua_);
     lua_register(lua_, "print", PrintToLog);
+    
+    lua_pushlightuserdata(lua_, this);
+    lua_setglobal(lua_, "_LuaContext");
   }
 
 
@@ -95,12 +107,12 @@
   }
 
 
-  void LuaContext::Execute(const std::string& command)
+  void LuaContext::Execute(std::string* output,
+                           const std::string& command)
   {
     boost::mutex::scoped_lock lock(mutex_);
 
-    lua_settop(lua_, 0);
-
+    log_.clear();
     int error = (luaL_loadbuffer(lua_, command.c_str(), command.size(), "line") ||
                  lua_pcall(lua_, 0, 0, 0));
 
@@ -112,6 +124,11 @@
       lua_pop(lua_, 1); /* pop error message from the stack */
       throw LuaException(description);
     }
+
+    if (output != NULL)
+    {
+      *output = log_;
+    }
   }
 
 
--- a/Core/Lua/LuaContext.h	Tue May 07 10:31:32 2013 +0200
+++ b/Core/Lua/LuaContext.h	Tue May 07 12:03:25 2013 +0200
@@ -53,15 +53,28 @@
 
     lua_State *lua_;
     boost::mutex mutex_;
+    std::string log_;
 
     static int PrintToLog(lua_State *L);
 
+    void Execute(std::string* output,
+                 const std::string& command);
+
   public:
     LuaContext();
 
     ~LuaContext();
 
-    void Execute(const std::string& command);
+    void Execute(const std::string& command)
+    {
+      Execute(NULL, command);
+    }
+
+    void Execute(std::string& output,
+                 const std::string& command)
+    {
+      Execute(&output, command);
+    }
 
     void Execute(EmbeddedResources::FileResourceId resource);
 
--- a/OrthancServer/OrthancRestApi.cpp	Tue May 07 10:31:32 2013 +0200
+++ b/OrthancServer/OrthancRestApi.cpp	Tue May 07 12:03:25 2013 +0200
@@ -362,6 +362,16 @@
     }
   }
 
+  static void ExecuteScript(RestApi::PostCall& call)
+  {
+    std::string result;
+    RETRIEVE_CONTEXT(call);
+    context.GetLuaContext().Execute(result, call.GetPostBody());
+    call.GetOutput().AnswerBuffer(result, "text/plain");
+  }
+
+
+
 
   // List all the patients, studies, series or instances ----------------------
  
@@ -1556,5 +1566,6 @@
     Register("/patients/{id}/anonymize", AnonymizePatientInplace);
 
     Register("/tools/generate-uid", GenerateUid);
+    Register("/tools/execute-script", ExecuteScript);
   }
 }
--- a/Resources/Toolbox.lua	Tue May 07 10:31:32 2013 +0200
+++ b/Resources/Toolbox.lua	Tue May 07 12:03:25 2013 +0200
@@ -17,3 +17,5 @@
    end
    return l
 end	
+
+print('Lua toolbox installed')