comparison OrthancFramework/Sources/Lua/LuaContext.h @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Core/Lua/LuaContext.h@e3b3af80732d
children bf7b9edf6b81
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #pragma once
35
36 // To have ORTHANC_ENABLE_LUA defined if using the shared library
37 #include "../OrthancFramework.h"
38
39 #if !defined(ORTHANC_ENABLE_LUA)
40 # error The macro ORTHANC_ENABLE_LUA must be defined
41 #endif
42
43 #if !defined(ORTHANC_ENABLE_CURL)
44 # error Macro ORTHANC_ENABLE_CURL must be defined
45 #endif
46
47 #if ORTHANC_ENABLE_LUA == 0
48 # error The Lua support is disabled, cannot include this file
49 #endif
50
51 #if ORTHANC_ENABLE_CURL == 1
52 # include "../HttpClient.h"
53 #endif
54
55 extern "C"
56 {
57 #include <lua.h>
58 }
59
60 #include <boost/noncopyable.hpp>
61 #include <json/value.h>
62
63 namespace Orthanc
64 {
65 class ORTHANC_PUBLIC LuaContext : public boost::noncopyable
66 {
67 private:
68 friend class LuaFunctionCall;
69
70 lua_State *lua_;
71 std::string log_;
72
73 #if ORTHANC_ENABLE_CURL == 1
74 HttpClient httpClient_;
75 #endif
76
77 static int PrintToLog(lua_State *state);
78 static int ParseJson(lua_State *state);
79 static int DumpJson(lua_State *state);
80
81 #if ORTHANC_ENABLE_CURL == 1
82 static int SetHttpCredentials(lua_State *state);
83 static int CallHttpPostOrPut(lua_State *state,
84 HttpMethod method);
85 static int CallHttpGet(lua_State *state);
86 static int CallHttpPost(lua_State *state);
87 static int CallHttpPut(lua_State *state);
88 static int CallHttpDelete(lua_State *state);
89 #endif
90
91 bool AnswerHttpQuery(lua_State* state);
92
93 void ExecuteInternal(std::string* output,
94 const std::string& command);
95
96 static void GetJson(Json::Value& result,
97 lua_State* state,
98 int top,
99 bool keepStrings);
100
101 void SetHttpHeaders(int top);
102
103 public:
104 LuaContext();
105
106 ~LuaContext();
107
108 void Execute(const std::string& command)
109 {
110 ExecuteInternal(NULL, command);
111 }
112
113 void Execute(std::string& output,
114 const std::string& command)
115 {
116 ExecuteInternal(&output, command);
117 }
118
119 void Execute(Json::Value& output,
120 const std::string& command);
121
122 bool IsExistingFunction(const char* name);
123
124 #if ORTHANC_ENABLE_CURL == 1
125 void SetHttpCredentials(const char* username,
126 const char* password)
127 {
128 httpClient_.SetCredentials(username, password);
129 }
130 #endif
131
132 void RegisterFunction(const char* name,
133 lua_CFunction func);
134
135 void SetGlobalVariable(const char* name,
136 void* value);
137
138 static LuaContext& GetLuaContext(lua_State *state);
139
140 static const void* GetGlobalVariable(lua_State* state,
141 const char* name);
142
143 void PushJson(const Json::Value& value);
144
145 static void GetDictionaryArgument(std::map<std::string, std::string>& target,
146 lua_State* state,
147 int top,
148 bool keyToLowerCase);
149 };
150 }