comparison Sources/RestCallbacks.cpp @ 45:ee76cced46a5

Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 03 Aug 2020 18:13:10 +0200
parents fd58eb5749ed
children 664a12539073
comparison
equal deleted inserted replaced
44:2468e2f2456c 45:ee76cced46a5
17 **/ 17 **/
18 18
19 19
20 #include "RestCallbacks.h" 20 #include "RestCallbacks.h"
21 21
22 #include "PythonObject.h" 22 #include "PythonString.h"
23 #include "Autogenerated/sdk.h" 23 #include "Autogenerated/sdk.h"
24 24
25 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" 25 #include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h"
26 26
27 #include <boost/regex.hpp> 27 #include <boost/regex.hpp>
88 88
89 89
90 /** 90 /**
91 * Construct the arguments tuple (output, uri) 91 * Construct the arguments tuple (output, uri)
92 **/ 92 **/
93 PythonString str(lock, uri);
94
93 PythonObject args2(lock, PyTuple_New(2)); 95 PythonObject args2(lock, PyTuple_New(2));
94 PyTuple_SetItem(args2.GetPyObject(), 0, pInst); 96 PyTuple_SetItem(args2.GetPyObject(), 0, pInst);
95 PyTuple_SetItem(args2.GetPyObject(), 1, PyUnicode_FromString(uri)); 97 PyTuple_SetItem(args2.GetPyObject(), 1, str.Release());
96 // No need to decrement refcount with "PyTuple_SetItem()" 98 // No need to decrement refcount with "PyTuple_SetItem()"
97 99
98 /** 100 /**
99 * Construct the named arguments from the "request" argument 101 * Construct the named arguments from the "request" argument
100 **/ 102 **/
118 break; 120 break;
119 121
120 default: 122 default:
121 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 123 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
122 } 124 }
123 125
124 PythonObject kw(lock, PyDict_New()); 126 PythonObject kw(lock, PyDict_New());
125 PyDict_SetItemString(kw.GetPyObject(), "method", PyUnicode_FromString(method)); 127
128 {
129 PythonString str(lock, method);
130 PyDict_SetItemString(kw.GetPyObject(), "method", str.Release());
131 }
126 132
127 { 133 {
128 PythonObject groups(lock, PyTuple_New(request->groupsCount)); 134 PythonObject groups(lock, PyTuple_New(request->groupsCount));
129 135
130 for (uint32_t i = 0; i < request->groupsCount; i++) 136 for (uint32_t i = 0; i < request->groupsCount; i++)
131 { 137 {
132 PyTuple_SetItem(groups.GetPyObject(), i, PyUnicode_FromString(request->groups[i])); 138 PythonString str(lock, request->groups[i]);
139 PyTuple_SetItem(groups.GetPyObject(), i, str.Release());
133 } 140 }
134 141
135 PyDict_SetItemString(kw.GetPyObject(), "groups", groups.Release()); 142 PyDict_SetItemString(kw.GetPyObject(), "groups", groups.Release());
136 } 143 }
137 144
139 { 146 {
140 PythonObject get(lock, PyDict_New()); 147 PythonObject get(lock, PyDict_New());
141 148
142 for (uint32_t i = 0; i < request->getCount; i++) 149 for (uint32_t i = 0; i < request->getCount; i++)
143 { 150 {
144 PyDict_SetItemString(get.GetPyObject(), request->getKeys[i], 151 PythonString value(lock, request->getValues[i]);
145 PyUnicode_FromString(request->getValues[i])); 152 PyDict_SetItemString(get.GetPyObject(), request->getKeys[i], value.Release());
146 } 153 }
147 154
148 PyDict_SetItemString(kw.GetPyObject(), "get", get.Release()); 155 PyDict_SetItemString(kw.GetPyObject(), "get", get.Release());
149 } 156 }
150 157
151 { 158 {
152 PythonObject headers(lock, PyDict_New()); 159 PythonObject headers(lock, PyDict_New());
153 160
154 for (uint32_t i = 0; i < request->headersCount; i++) 161 for (uint32_t i = 0; i < request->headersCount; i++)
155 { 162 {
156 PyDict_SetItemString(headers.GetPyObject(), request->headersKeys[i], 163 PythonString value(lock, request->headersValues[i]);
157 PyUnicode_FromString(request->headersValues[i])); 164 PyDict_SetItemString(headers.GetPyObject(), request->headersKeys[i], value.Release());
158 } 165 }
159 166
160 PyDict_SetItemString(kw.GetPyObject(), "headers", headers.Release()); 167 PyDict_SetItemString(kw.GetPyObject(), "headers", headers.Release());
161 } 168 }
162 169