Mercurial > hg > orthanc-python
annotate Sources/RestCallbacks.cpp @ 108:2389ec6ec803
preparing for release
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 23 Feb 2022 18:40:20 +0100 |
parents | eb6ac5a801d1 |
children | 65ec5597ec70 5f0b7795afaf |
rev | line source |
---|---|
0 | 1 /** |
2 * Python plugin for Orthanc | |
101 | 3 * Copyright (C) 2020-2022 Osimis S.A., Belgium |
4 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium | |
0 | 5 * |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU Affero General Public License | |
8 * as published by the Free Software Foundation, either version 3 of | |
9 * the License, or (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * Affero General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Affero General Public License | |
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 **/ | |
19 | |
20 | |
21 #include "RestCallbacks.h" | |
22 | |
68
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
23 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" |
0 | 24 #include "Autogenerated/sdk.h" |
68
0b3ef425db31
refactoring using ICallbackRegistration::Apply()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
63
diff
changeset
|
25 #include "PythonString.h" |
0 | 26 |
27 #include <boost/regex.hpp> | |
28 | |
29 | |
30 class RestCallback : public boost::noncopyable | |
31 { | |
32 private: | |
33 boost::regex regex_; | |
34 PyObject* callback_; | |
35 | |
36 public: | |
37 RestCallback(const std::string& uri, | |
38 PyObject* callback) : | |
39 regex_(uri), | |
40 callback_(callback) | |
41 { | |
42 Py_XINCREF(callback_); | |
43 } | |
44 | |
45 ~RestCallback() | |
46 { | |
47 Py_XDECREF(callback_); | |
48 } | |
49 | |
50 bool IsMatch(const std::string& uri) const | |
51 { | |
52 return boost::regex_match(uri, regex_); | |
53 } | |
54 | |
55 PyObject* GetCallback() | |
56 { | |
57 return callback_; | |
58 } | |
59 }; | |
60 | |
61 | |
62 // Concurrent accesses to the callbacks are protected by the | |
63 // "PythonLock" (GIL mutex) | |
64 static std::list<RestCallback*> restCallbacks_; | |
65 | |
66 | |
98
62f2731094ae
fix build on ubuntu 16.04
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
68
diff
changeset
|
67 void RestCallbackHandler(OrthancPluginRestOutput* output, |
62f2731094ae
fix build on ubuntu 16.04
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
68
diff
changeset
|
68 const char* uri, |
62f2731094ae
fix build on ubuntu 16.04
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
68
diff
changeset
|
69 const OrthancPluginHttpRequest* request) |
0 | 70 { |
71 PythonLock lock; | |
72 | |
73 for (std::list<RestCallback*>::const_iterator it = restCallbacks_.begin(); | |
74 it != restCallbacks_.end(); ++it) | |
75 { | |
76 assert(*it != NULL); | |
77 if ((*it)->IsMatch(uri)) | |
78 { | |
79 /** | |
80 * Construct an instance object of the "orthanc.RestOutput" | |
81 * class. This is done by calling the constructor function | |
82 * "sdk_OrthancPluginRestOutput_Type". | |
83 **/ | |
84 PythonObject args(lock, PyTuple_New(2)); | |
85 PyTuple_SetItem(args.GetPyObject(), 0, PyLong_FromSsize_t((intptr_t) output)); | |
86 PyTuple_SetItem(args.GetPyObject(), 1, PyBool_FromLong(true /* borrowed, don't destruct */)); | |
63
32de70a1e4c7
New functions from the SDK wrapped in Python: CreateDicom, RegisterFindCallback, RegisterMoveCallback
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
87 PyObject *pInst = PyObject_CallObject((PyObject*) GetOrthancPluginRestOutputType(), args.GetPyObject()); |
0 | 88 |
89 | |
90 /** | |
91 * Construct the arguments tuple (output, uri) | |
92 **/ | |
45
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
93 PythonString str(lock, uri); |
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
94 |
0 | 95 PythonObject args2(lock, PyTuple_New(2)); |
96 PyTuple_SetItem(args2.GetPyObject(), 0, pInst); | |
45
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
97 PyTuple_SetItem(args2.GetPyObject(), 1, str.Release()); |
0 | 98 // No need to decrement refcount with "PyTuple_SetItem()" |
99 | |
100 /** | |
101 * Construct the named arguments from the "request" argument | |
102 **/ | |
103 const char* method; | |
104 switch (request->method) | |
105 { | |
106 case OrthancPluginHttpMethod_Get: | |
107 method = "GET"; | |
108 break; | |
109 | |
110 case OrthancPluginHttpMethod_Post: | |
111 method = "POST"; | |
112 break; | |
113 | |
114 case OrthancPluginHttpMethod_Put: | |
115 method = "PUT"; | |
116 break; | |
117 | |
118 case OrthancPluginHttpMethod_Delete: | |
119 method = "DELETE"; | |
120 break; | |
121 | |
122 default: | |
123 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); | |
124 } | |
45
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
125 |
0 | 126 PythonObject kw(lock, PyDict_New()); |
45
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
127 |
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
128 { |
53 | 129 PythonString tmp(lock, method); |
130 PyDict_SetItemString(kw.GetPyObject(), "method", tmp.Release()); | |
45
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
131 } |
0 | 132 |
133 { | |
134 PythonObject groups(lock, PyTuple_New(request->groupsCount)); | |
135 | |
136 for (uint32_t i = 0; i < request->groupsCount; i++) | |
137 { | |
53 | 138 PythonString tmp(lock, request->groups[i]); |
139 PyTuple_SetItem(groups.GetPyObject(), i, tmp.Release()); | |
0 | 140 } |
141 | |
142 PyDict_SetItemString(kw.GetPyObject(), "groups", groups.Release()); | |
143 } | |
144 | |
145 if (request->method == OrthancPluginHttpMethod_Get) | |
146 { | |
147 PythonObject get(lock, PyDict_New()); | |
148 | |
149 for (uint32_t i = 0; i < request->getCount; i++) | |
150 { | |
45
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
151 PythonString value(lock, request->getValues[i]); |
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
152 PyDict_SetItemString(get.GetPyObject(), request->getKeys[i], value.Release()); |
0 | 153 } |
154 | |
155 PyDict_SetItemString(kw.GetPyObject(), "get", get.Release()); | |
156 } | |
157 | |
158 { | |
159 PythonObject headers(lock, PyDict_New()); | |
160 | |
161 for (uint32_t i = 0; i < request->headersCount; i++) | |
162 { | |
45
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
163 PythonString value(lock, request->headersValues[i]); |
ee76cced46a5
Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
36
diff
changeset
|
164 PyDict_SetItemString(headers.GetPyObject(), request->headersKeys[i], value.Release()); |
0 | 165 } |
166 | |
167 PyDict_SetItemString(kw.GetPyObject(), "headers", headers.Release()); | |
168 } | |
169 | |
170 if (request->method == OrthancPluginHttpMethod_Post || | |
171 request->method == OrthancPluginHttpMethod_Put) | |
172 { | |
173 PyDict_SetItemString(kw.GetPyObject(), "body", PyBytes_FromStringAndSize( | |
174 reinterpret_cast<const char*>(request->body), request->bodySize)); | |
175 } | |
176 | |
177 /** | |
178 * Call the user-defined function | |
179 **/ | |
180 PythonObject result(lock, PyObject_Call( | |
181 (*it)->GetCallback(), args2.GetPyObject(), kw.GetPyObject())); | |
3
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
182 |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
183 std::string traceback; |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
184 if (lock.HasErrorOccurred(traceback)) |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
185 { |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
186 OrthancPlugins::LogError("Error in the REST callback, traceback:\n" + traceback); |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
187 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
188 } |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
189 |
0 | 190 return; |
191 } | |
192 } | |
193 | |
194 // Should never happen | |
195 OrthancPlugins::LogError("Unable to find the Python handler for URI: " + std::string(uri)); | |
196 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
197 } | |
198 | |
199 | |
200 | |
201 PyObject* RegisterRestCallback(PyObject* module, PyObject* args) | |
202 { | |
203 // The GIL is locked at this point (no need to create "PythonLock") | |
204 | |
205 // https://docs.python.org/3/extending/extending.html#calling-python-functions-from-c | |
206 const char* uri = NULL; | |
207 PyObject* callback = NULL; | |
208 | |
209 if (!PyArg_ParseTuple(args, "sO", &uri, &callback) || | |
210 uri == NULL || | |
211 callback == NULL) | |
212 { | |
213 PyErr_SetString(PyExc_ValueError, "Expected a string (URI) and a callback function"); | |
214 return NULL; | |
215 } | |
216 | |
217 OrthancPlugins::LogInfo("Registering a Python REST callback on URI: " + std::string(uri)); | |
218 OrthancPlugins::RegisterRestCallback<RestCallbackHandler>(uri, true /* thread safe */); | |
219 | |
220 restCallbacks_.push_back(new RestCallback(uri, callback)); | |
221 | |
222 Py_INCREF(Py_None); | |
223 return Py_None; | |
224 } | |
225 | |
226 | |
227 | |
228 void FinalizeRestCallbacks() | |
229 { | |
230 PythonLock lock; | |
231 | |
232 for (std::list<RestCallback*>::iterator it = restCallbacks_.begin(); | |
233 it != restCallbacks_.end(); ++it) | |
234 { | |
235 assert(*it != NULL); | |
236 delete *it; | |
237 } | |
238 | |
239 restCallbacks_.clear(); | |
240 } |