Mercurial > hg > orthanc
comparison Plugins/Samples/ServeFolders/Plugin.cpp @ 2169:d15de5685ad8
ServeFolders: "Extensions" configuration option
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 22 Nov 2016 15:03:14 +0100 |
parents | 84033563f7f0 |
children | fead5549aaa7 |
comparison
equal
deleted
inserted
replaced
2168:84033563f7f0 | 2169:d15de5685ad8 |
---|---|
106 return true; | 106 return true; |
107 } | 107 } |
108 } | 108 } |
109 | 109 |
110 | 110 |
111 static void Answer(OrthancPluginRestOutput* output, | |
112 const char* content, | |
113 size_t size, | |
114 const std::string& mime) | |
115 { | |
116 if (generateETag_) | |
117 { | |
118 OrthancPlugins::OrthancString md5(context_, OrthancPluginComputeMd5(context_, content, size)); | |
119 std::string etag = "\"" + std::string(md5.GetContent()) + "\""; | |
120 OrthancPluginSetHttpHeader(context_, output, "ETag", etag.c_str()); | |
121 } | |
122 | |
123 SetHttpHeaders(output); | |
124 OrthancPluginAnswerBuffer(context_, output, content, size, mime.c_str()); | |
125 } | |
126 | |
127 | |
111 void ServeFolder(OrthancPluginRestOutput* output, | 128 void ServeFolder(OrthancPluginRestOutput* output, |
112 const char* url, | 129 const char* url, |
113 const OrthancPluginHttpRequest* request) | 130 const OrthancPluginHttpRequest* request) |
114 { | 131 { |
115 namespace fs = boost::filesystem; | 132 namespace fs = boost::filesystem; |
162 | 179 |
163 s += " </ul>\n"; | 180 s += " </ul>\n"; |
164 s += " </body>\n"; | 181 s += " </body>\n"; |
165 s += "</html>\n"; | 182 s += "</html>\n"; |
166 | 183 |
167 SetHttpHeaders(output); | 184 Answer(output, s.c_str(), s.size(), "text/html"); |
168 OrthancPluginAnswerBuffer(context_, output, s.c_str(), s.size(), "text/html"); | |
169 } | 185 } |
170 else | 186 else |
171 { | 187 { |
172 std::string path = folder + "/" + item.string(); | 188 std::string path = folder + "/" + item.string(); |
173 std::string mime = GetMimeType(path); | 189 std::string mime = GetMimeType(path); |
181 catch (OrthancPlugins::PluginException&) | 197 catch (OrthancPlugins::PluginException&) |
182 { | 198 { |
183 throw OrthancPlugins::PluginException(OrthancPluginErrorCode_InexistentFile); | 199 throw OrthancPlugins::PluginException(OrthancPluginErrorCode_InexistentFile); |
184 } | 200 } |
185 | 201 |
186 if (generateETag_) | |
187 { | |
188 OrthancPlugins::OrthancString md5(context_, OrthancPluginComputeMd5(context_, content.GetData(), content.GetSize())); | |
189 std::string etag = "\"" + std::string(md5.GetContent()) + "\""; | |
190 OrthancPluginSetHttpHeader(context_, output, "ETag", etag.c_str()); | |
191 } | |
192 | |
193 boost::posix_time::ptime lastModification = boost::posix_time::from_time_t(fs::last_write_time(path)); | 202 boost::posix_time::ptime lastModification = boost::posix_time::from_time_t(fs::last_write_time(path)); |
194 std::string t = boost::posix_time::to_iso_string(lastModification); | 203 std::string t = boost::posix_time::to_iso_string(lastModification); |
195 OrthancPluginSetHttpHeader(context_, output, "Last-Modified", t.c_str()); | 204 OrthancPluginSetHttpHeader(context_, output, "Last-Modified", t.c_str()); |
196 | 205 |
197 SetHttpHeaders(output); | 206 Answer(output, content.GetData(), content.GetSize(), mime); |
198 OrthancPluginAnswerBuffer(context_, output, content.GetData(), content.GetSize(), mime.c_str()); | |
199 } | 207 } |
200 } | 208 } |
201 } | 209 } |
202 | 210 |
203 | 211 |
230 s += "</ul>\n"; | 238 s += "</ul>\n"; |
231 } | 239 } |
232 | 240 |
233 s += "</body></html>\n"; | 241 s += "</body></html>\n"; |
234 | 242 |
235 SetHttpHeaders(output); | 243 Answer(output, s.c_str(), s.size(), "text/html"); |
236 OrthancPluginAnswerBuffer(context_, output, s.c_str(), s.size(), "text/html"); | |
237 } | 244 } |
238 | 245 |
239 | 246 |
240 static void ConfigureFolders(const Json::Value& folders) | 247 static void ConfigureFolders(const Json::Value& folders) |
241 { | 248 { |
251 for (Json::Value::Members::const_iterator | 258 for (Json::Value::Members::const_iterator |
252 it = members.begin(); it != members.end(); ++it) | 259 it = members.begin(); it != members.end(); ++it) |
253 { | 260 { |
254 if (folders[*it].type() != Json::stringValue) | 261 if (folders[*it].type() != Json::stringValue) |
255 { | 262 { |
256 OrthancPlugins::LogError(context_, "The folder to be server \"" + *it + | 263 OrthancPlugins::LogError(context_, "The folder to be served \"" + *it + |
257 "\" must be associated with a string value (its mapped URI)"); | 264 "\" must be associated with a string value (its mapped URI)"); |
258 throw OrthancPlugins::PluginException(OrthancPluginErrorCode_BadFileFormat); | 265 throw OrthancPlugins::PluginException(OrthancPluginErrorCode_BadFileFormat); |
259 } | 266 } |
260 | 267 |
261 std::string baseUri = *it; | 268 std::string baseUri = *it; |
293 { | 300 { |
294 const std::string regex = "/(" + baseUri + ")/(.*)"; | 301 const std::string regex = "/(" + baseUri + ")/(.*)"; |
295 OrthancPlugins::RegisterRestCallback<ServeFolder>(context_, regex.c_str(), true); | 302 OrthancPlugins::RegisterRestCallback<ServeFolder>(context_, regex.c_str(), true); |
296 } | 303 } |
297 } | 304 } |
305 } | |
306 | |
307 | |
308 static void ConfigureExtensions(const Json::Value& extensions) | |
309 { | |
310 if (extensions.type() != Json::objectValue) | |
311 { | |
312 OrthancPlugins::LogError(context_, "The list of extensions is badly formatted (must be a JSON object)"); | |
313 throw OrthancPlugins::PluginException(OrthancPluginErrorCode_BadFileFormat); | |
314 } | |
315 | |
316 Json::Value::Members members = extensions.getMemberNames(); | |
317 | |
318 for (Json::Value::Members::const_iterator | |
319 it = members.begin(); it != members.end(); ++it) | |
320 { | |
321 if (extensions[*it].type() != Json::stringValue) | |
322 { | |
323 OrthancPlugins::LogError(context_, "The file extension \"" + *it + | |
324 "\" must be associated with a string value (its MIME type)"); | |
325 throw OrthancPlugins::PluginException(OrthancPluginErrorCode_BadFileFormat); | |
326 } | |
327 | |
328 const std::string& mime = extensions[*it].asString(); | |
329 | |
330 std::string name = *it; | |
331 | |
332 if (!name.empty() && | |
333 name[0] == '.') | |
334 { | |
335 name = name.substr(1); // Remove the leading dot "." | |
336 } | |
337 | |
338 extensions_[name] = mime; | |
339 | |
340 if (mime.empty()) | |
341 { | |
342 OrthancPlugins::LogWarning(context_, "ServeFolders: Removing MIME type for file extension \"." + name + "\""); | |
343 } | |
344 else | |
345 { | |
346 OrthancPlugins::LogWarning(context_, "ServeFolders: Associating file extension \"." + name + | |
347 "\" with MIME type \"" + mime + "\""); | |
348 } | |
349 } | |
298 } | 350 } |
299 | 351 |
300 | 352 |
301 static void ReadConfiguration() | 353 static void ReadConfiguration() |
302 { | 354 { |
331 { | 383 { |
332 generateETag_ = tmp; | 384 generateETag_ = tmp; |
333 OrthancPlugins::LogWarning(context_, "ServeFolders: The computation of an ETag for the served resources is " + | 385 OrthancPlugins::LogWarning(context_, "ServeFolders: The computation of an ETag for the served resources is " + |
334 std::string(tmp ? "enabled" : "disabled")); | 386 std::string(tmp ? "enabled" : "disabled")); |
335 } | 387 } |
388 | |
389 OrthancPlugins::OrthancConfiguration extensions; | |
390 configuration.GetSection(extensions, "Extensions"); | |
391 ConfigureExtensions(extensions.GetJson()); | |
336 } | 392 } |
337 | 393 |
338 if (folders_.empty()) | 394 if (folders_.empty()) |
339 { | 395 { |
340 OrthancPlugins::LogWarning(context_, "ServeFolders: Empty configuration file: No additional folder will be served!"); | 396 OrthancPlugins::LogWarning(context_, "ServeFolders: Empty configuration file: No additional folder will be served!"); |