Mercurial > hg > orthanc
comparison Plugins/Engine/PluginsHttpHandler.cpp @ 1041:2c49b7dffcec
plugins have access to the HTTP headers
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 17 Jul 2014 14:14:15 +0200 |
parents | 6208ab500ffd |
children | 8d1845feb277 |
comparison
equal
deleted
inserted
replaced
1040:d06186cdc502 | 1041:2c49b7dffcec |
---|---|
101 PluginsHttpHandler::~PluginsHttpHandler() | 101 PluginsHttpHandler::~PluginsHttpHandler() |
102 { | 102 { |
103 for (PImpl::Callbacks::iterator it = pimpl_->callbacks_.begin(); | 103 for (PImpl::Callbacks::iterator it = pimpl_->callbacks_.begin(); |
104 it != pimpl_->callbacks_.end(); ++it) | 104 it != pimpl_->callbacks_.end(); ++it) |
105 { | 105 { |
106 // Delete the regular expression associated with this callback | |
106 delete it->first; | 107 delete it->first; |
108 } | |
109 } | |
110 | |
111 | |
112 static void ArgumentsToPlugin(std::vector<const char*>& keys, | |
113 std::vector<const char*>& values, | |
114 const HttpHandler::Arguments& arguments) | |
115 { | |
116 keys.resize(arguments.size()); | |
117 values.resize(arguments.size()); | |
118 | |
119 size_t pos = 0; | |
120 for (HttpHandler::Arguments::const_iterator | |
121 it = arguments.begin(); it != arguments.end(); ++it) | |
122 { | |
123 keys[pos] = it->first.c_str(); | |
124 values[pos] = it->second.c_str(); | |
125 pos++; | |
107 } | 126 } |
108 } | 127 } |
109 | 128 |
110 | 129 |
111 bool PluginsHttpHandler::Handle(HttpOutput& output, | 130 bool PluginsHttpHandler::Handle(HttpOutput& output, |
119 OrthancPluginRestCallback callback = NULL; | 138 OrthancPluginRestCallback callback = NULL; |
120 | 139 |
121 std::vector<std::string> groups; | 140 std::vector<std::string> groups; |
122 std::vector<const char*> cgroups; | 141 std::vector<const char*> cgroups; |
123 | 142 |
143 // Loop over the callbacks registered by the plugins | |
124 bool found = false; | 144 bool found = false; |
125 for (PImpl::Callbacks::const_iterator it = pimpl_->callbacks_.begin(); | 145 for (PImpl::Callbacks::const_iterator it = pimpl_->callbacks_.begin(); |
126 it != pimpl_->callbacks_.end() && !found; ++it) | 146 it != pimpl_->callbacks_.end() && !found; ++it) |
127 { | 147 { |
148 // Check whether the regular expression associated to this | |
149 // callback matches the URI | |
128 boost::cmatch what; | 150 boost::cmatch what; |
129 if (boost::regex_match(flatUri.c_str(), what, *(it->first))) | 151 if (boost::regex_match(flatUri.c_str(), what, *(it->first))) |
130 { | 152 { |
131 callback = it->second; | 153 callback = it->second; |
132 | 154 |
155 // Extract the value of the free parameters of the regular expression | |
133 if (what.size() > 1) | 156 if (what.size() > 1) |
134 { | 157 { |
135 groups.resize(what.size() - 1); | 158 groups.resize(what.size() - 1); |
136 cgroups.resize(what.size() - 1); | 159 cgroups.resize(what.size() - 1); |
137 for (size_t i = 1; i < what.size(); i++) | 160 for (size_t i = 1; i < what.size(); i++) |
150 return false; | 173 return false; |
151 } | 174 } |
152 | 175 |
153 LOG(INFO) << "Delegating HTTP request to plugin for URI: " << flatUri; | 176 LOG(INFO) << "Delegating HTTP request to plugin for URI: " << flatUri; |
154 | 177 |
155 std::vector<const char*> getKeys(getArguments.size()); | 178 std::vector<const char*> getKeys, getValues, headersKeys, headersValues; |
156 std::vector<const char*> getValues(getArguments.size()); | |
157 | 179 |
158 OrthancPluginHttpRequest request; | 180 OrthancPluginHttpRequest request; |
159 memset(&request, 0, sizeof(OrthancPluginHttpRequest)); | 181 memset(&request, 0, sizeof(OrthancPluginHttpRequest)); |
160 | 182 |
183 ArgumentsToPlugin(headersKeys, headersValues, headers); | |
184 | |
161 switch (method) | 185 switch (method) |
162 { | 186 { |
163 case HttpMethod_Get: | 187 case HttpMethod_Get: |
164 { | |
165 request.method = OrthancPluginHttpMethod_Get; | 188 request.method = OrthancPluginHttpMethod_Get; |
166 | 189 ArgumentsToPlugin(getKeys, getValues, getArguments); |
167 size_t i = 0; | 190 break; |
168 for (Arguments::const_iterator it = getArguments.begin(); | |
169 it != getArguments.end(); ++it, ++i) | |
170 { | |
171 getKeys[i] = it->first.c_str(); | |
172 getValues[i] = it->second.c_str(); | |
173 } | |
174 | |
175 break; | |
176 } | |
177 | 191 |
178 case HttpMethod_Post: | 192 case HttpMethod_Post: |
179 request.method = OrthancPluginHttpMethod_Post; | 193 request.method = OrthancPluginHttpMethod_Post; |
180 break; | 194 break; |
181 | 195 |
195 request.groups = (cgroups.size() ? &cgroups[0] : NULL); | 209 request.groups = (cgroups.size() ? &cgroups[0] : NULL); |
196 request.groupsCount = cgroups.size(); | 210 request.groupsCount = cgroups.size(); |
197 request.getCount = getArguments.size(); | 211 request.getCount = getArguments.size(); |
198 request.body = (postData.size() ? &postData[0] : NULL); | 212 request.body = (postData.size() ? &postData[0] : NULL); |
199 request.bodySize = postData.size(); | 213 request.bodySize = postData.size(); |
200 | 214 request.headersCount = headers.size(); |
215 | |
201 if (getArguments.size() > 0) | 216 if (getArguments.size() > 0) |
202 { | 217 { |
203 request.getKeys = &getKeys[0]; | 218 request.getKeys = &getKeys[0]; |
204 request.getValues = &getValues[0]; | 219 request.getValues = &getValues[0]; |
220 } | |
221 | |
222 if (headers.size() > 0) | |
223 { | |
224 request.headersKeys = &headersKeys[0]; | |
225 request.headersValues = &headersValues[0]; | |
205 } | 226 } |
206 | 227 |
207 assert(callback != NULL); | 228 assert(callback != NULL); |
208 int32_t error = callback(reinterpret_cast<OrthancPluginRestOutput*>(&output), | 229 int32_t error = callback(reinterpret_cast<OrthancPluginRestOutput*>(&output), |
209 flatUri.c_str(), | 230 flatUri.c_str(), |