Mercurial > hg > orthanc
comparison OrthancServer/Plugins/Samples/ServeFolders/Plugin.cpp @ 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 | Plugins/Samples/ServeFolders/Plugin.cpp@94f4a18a79cc |
children | c5cdb6dc6865 |
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 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "../Common/OrthancPluginCppWrapper.h" | |
23 | |
24 #include <json/reader.h> | |
25 #include <json/value.h> | |
26 #include <boost/filesystem.hpp> | |
27 #include <boost/date_time/posix_time/posix_time.hpp> | |
28 | |
29 | |
30 #if HAS_ORTHANC_EXCEPTION == 1 | |
31 # error The macro HAS_ORTHANC_EXCEPTION must be set to 0 to compile this plugin | |
32 #endif | |
33 | |
34 | |
35 | |
36 static std::map<std::string, std::string> extensions_; | |
37 static std::map<std::string, std::string> folders_; | |
38 static const char* INDEX_URI = "/app/plugin-serve-folders.html"; | |
39 static bool allowCache_ = false; | |
40 static bool generateETag_ = true; | |
41 | |
42 | |
43 static void SetHttpHeaders(OrthancPluginRestOutput* output) | |
44 { | |
45 if (!allowCache_) | |
46 { | |
47 // http://stackoverflow.com/a/2068407/881731 | |
48 OrthancPluginContext* context = OrthancPlugins::GetGlobalContext(); | |
49 OrthancPluginSetHttpHeader(context, output, "Cache-Control", "no-cache, no-store, must-revalidate"); | |
50 OrthancPluginSetHttpHeader(context, output, "Pragma", "no-cache"); | |
51 OrthancPluginSetHttpHeader(context, output, "Expires", "0"); | |
52 } | |
53 } | |
54 | |
55 | |
56 static void RegisterDefaultExtensions() | |
57 { | |
58 extensions_["css"] = "text/css"; | |
59 extensions_["gif"] = "image/gif"; | |
60 extensions_["html"] = "text/html"; | |
61 extensions_["jpeg"] = "image/jpeg"; | |
62 extensions_["jpg"] = "image/jpeg"; | |
63 extensions_["js"] = "application/javascript"; | |
64 extensions_["json"] = "application/json"; | |
65 extensions_["nexe"] = "application/x-nacl"; | |
66 extensions_["nmf"] = "application/json"; | |
67 extensions_["pexe"] = "application/x-pnacl"; | |
68 extensions_["png"] = "image/png"; | |
69 extensions_["svg"] = "image/svg+xml"; | |
70 extensions_["wasm"] = "application/wasm"; | |
71 extensions_["woff"] = "application/x-font-woff"; | |
72 extensions_["xml"] = "application/xml"; | |
73 } | |
74 | |
75 | |
76 static std::string GetMimeType(const std::string& path) | |
77 { | |
78 size_t dot = path.find_last_of('.'); | |
79 | |
80 std::string extension = (dot == std::string::npos) ? "" : path.substr(dot + 1); | |
81 std::transform(extension.begin(), extension.end(), extension.begin(), tolower); | |
82 | |
83 std::map<std::string, std::string>::const_iterator found = extensions_.find(extension); | |
84 | |
85 if (found != extensions_.end() && | |
86 !found->second.empty()) | |
87 { | |
88 return found->second; | |
89 } | |
90 else | |
91 { | |
92 OrthancPlugins::LogWarning("ServeFolders: Unknown MIME type for extension \"" + extension + "\""); | |
93 return "application/octet-stream"; | |
94 } | |
95 } | |
96 | |
97 | |
98 static bool LookupFolder(std::string& folder, | |
99 OrthancPluginRestOutput* output, | |
100 const OrthancPluginHttpRequest* request) | |
101 { | |
102 const std::string uri = request->groups[0]; | |
103 | |
104 std::map<std::string, std::string>::const_iterator found = folders_.find(uri); | |
105 if (found == folders_.end()) | |
106 { | |
107 OrthancPlugins::LogError("Unknown URI in plugin server-folders: " + uri); | |
108 OrthancPluginSendHttpStatusCode(OrthancPlugins::GetGlobalContext(), output, 404); | |
109 return false; | |
110 } | |
111 else | |
112 { | |
113 folder = found->second; | |
114 return true; | |
115 } | |
116 } | |
117 | |
118 | |
119 static void Answer(OrthancPluginRestOutput* output, | |
120 const char* content, | |
121 size_t size, | |
122 const std::string& mime) | |
123 { | |
124 if (generateETag_) | |
125 { | |
126 OrthancPlugins::OrthancString md5; | |
127 md5.Assign(OrthancPluginComputeMd5(OrthancPlugins::GetGlobalContext(), content, size)); | |
128 | |
129 std::string etag = "\"" + std::string(md5.GetContent()) + "\""; | |
130 OrthancPluginSetHttpHeader(OrthancPlugins::GetGlobalContext(), output, "ETag", etag.c_str()); | |
131 } | |
132 | |
133 SetHttpHeaders(output); | |
134 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, content, size, mime.c_str()); | |
135 } | |
136 | |
137 | |
138 void ServeFolder(OrthancPluginRestOutput* output, | |
139 const char* url, | |
140 const OrthancPluginHttpRequest* request) | |
141 { | |
142 namespace fs = boost::filesystem; | |
143 | |
144 if (request->method != OrthancPluginHttpMethod_Get) | |
145 { | |
146 OrthancPluginSendMethodNotAllowed(OrthancPlugins::GetGlobalContext(), output, "GET"); | |
147 return; | |
148 } | |
149 | |
150 std::string folder; | |
151 | |
152 if (LookupFolder(folder, output, request)) | |
153 { | |
154 const fs::path item(request->groups[1]); | |
155 const fs::path parent((fs::path(folder) / item).parent_path()); | |
156 | |
157 if (item.filename().string() == "index.html" && | |
158 fs::is_directory(parent) && | |
159 !fs::is_regular_file(fs::path(folder) / item)) | |
160 { | |
161 // On-the-fly generation of an "index.html" | |
162 std::string s; | |
163 s += "<html>\n"; | |
164 s += " <body>\n"; | |
165 s += " <ul>\n"; | |
166 | |
167 fs::directory_iterator end; | |
168 | |
169 for (fs::directory_iterator it(parent) ; it != end; ++it) | |
170 { | |
171 if (fs::is_directory(it->status())) | |
172 { | |
173 std::string f = it->path().filename().string(); | |
174 s += " <li><a href=\"" + f + "/index.html\">" + f + "/</a></li>\n"; | |
175 } | |
176 } | |
177 | |
178 for (fs::directory_iterator it(parent) ; it != end; ++it) | |
179 { | |
180 fs::file_type type = it->status().type(); | |
181 | |
182 if (type == fs::regular_file || | |
183 type == fs::reparse_file) // cf. BitBucket issue #11 | |
184 { | |
185 std::string f = it->path().filename().string(); | |
186 s += " <li><a href=\"" + f + "\">" + f + "</a></li>\n"; | |
187 } | |
188 } | |
189 | |
190 s += " </ul>\n"; | |
191 s += " </body>\n"; | |
192 s += "</html>\n"; | |
193 | |
194 Answer(output, s.c_str(), s.size(), "text/html"); | |
195 } | |
196 else | |
197 { | |
198 std::string path = folder + "/" + item.string(); | |
199 std::string mime = GetMimeType(path); | |
200 | |
201 OrthancPlugins::MemoryBuffer content; | |
202 | |
203 try | |
204 { | |
205 content.ReadFile(path); | |
206 } | |
207 catch (...) | |
208 { | |
209 ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentFile); | |
210 } | |
211 | |
212 boost::posix_time::ptime lastModification = | |
213 boost::posix_time::from_time_t(fs::last_write_time(path)); | |
214 std::string t = boost::posix_time::to_iso_string(lastModification); | |
215 OrthancPluginSetHttpHeader(OrthancPlugins::GetGlobalContext(), | |
216 output, "Last-Modified", t.c_str()); | |
217 | |
218 Answer(output, content.GetData(), content.GetSize(), mime); | |
219 } | |
220 } | |
221 } | |
222 | |
223 | |
224 void ListServedFolders(OrthancPluginRestOutput* output, | |
225 const char* url, | |
226 const OrthancPluginHttpRequest* request) | |
227 { | |
228 if (request->method != OrthancPluginHttpMethod_Get) | |
229 { | |
230 OrthancPluginSendMethodNotAllowed(OrthancPlugins::GetGlobalContext(), output, "GET"); | |
231 return; | |
232 } | |
233 | |
234 std::string s = "<html><body><h1>Additional folders served by Orthanc</h1>\n"; | |
235 | |
236 if (folders_.empty()) | |
237 { | |
238 s += "<p>Empty section <tt>ServeFolders</tt> in your configuration file: No additional folder is served.</p>\n"; | |
239 } | |
240 else | |
241 { | |
242 s += "<ul>\n"; | |
243 for (std::map<std::string, std::string>::const_iterator | |
244 it = folders_.begin(); it != folders_.end(); ++it) | |
245 { | |
246 // The URI is relative to INDEX_URI ("/app/plugin-serve-folders.html") | |
247 s += "<li><a href=\"../" + it->first + "/index.html\">" + it->first + "</li>\n"; | |
248 } | |
249 | |
250 s += "</ul>\n"; | |
251 } | |
252 | |
253 s += "</body></html>\n"; | |
254 | |
255 Answer(output, s.c_str(), s.size(), "text/html"); | |
256 } | |
257 | |
258 | |
259 static void ConfigureFolders(const Json::Value& folders) | |
260 { | |
261 if (folders.type() != Json::objectValue) | |
262 { | |
263 OrthancPlugins::LogError("The list of folders to be served is badly formatted (must be a JSON object)"); | |
264 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
265 } | |
266 | |
267 Json::Value::Members members = folders.getMemberNames(); | |
268 | |
269 // Register the callback for each base URI | |
270 for (Json::Value::Members::const_iterator | |
271 it = members.begin(); it != members.end(); ++it) | |
272 { | |
273 if (folders[*it].type() != Json::stringValue) | |
274 { | |
275 OrthancPlugins::LogError("The folder to be served \"" + *it + | |
276 "\" must be associated with a string value (its mapped URI)"); | |
277 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
278 } | |
279 | |
280 std::string baseUri = *it; | |
281 | |
282 // Remove the heading and trailing slashes in the root URI, if any | |
283 while (!baseUri.empty() && | |
284 *baseUri.begin() == '/') | |
285 { | |
286 baseUri = baseUri.substr(1); | |
287 } | |
288 | |
289 while (!baseUri.empty() && | |
290 *baseUri.rbegin() == '/') | |
291 { | |
292 baseUri.resize(baseUri.size() - 1); | |
293 } | |
294 | |
295 if (baseUri.empty()) | |
296 { | |
297 OrthancPlugins::LogError("The URI of a folder to be served cannot be empty"); | |
298 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
299 } | |
300 | |
301 // Check whether the source folder exists and is indeed a directory | |
302 const std::string folder = folders[*it].asString(); | |
303 if (!boost::filesystem::is_directory(folder)) | |
304 { | |
305 OrthancPlugins::LogError("Trying to serve an inexistent folder: " + folder); | |
306 ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentFile); | |
307 } | |
308 | |
309 folders_[baseUri] = folder; | |
310 | |
311 // Register the callback to serve the folder | |
312 { | |
313 const std::string regex = "/(" + baseUri + ")/(.*)"; | |
314 OrthancPlugins::RegisterRestCallback<ServeFolder>(regex.c_str(), true); | |
315 } | |
316 } | |
317 } | |
318 | |
319 | |
320 static void ConfigureExtensions(const Json::Value& extensions) | |
321 { | |
322 if (extensions.type() != Json::objectValue) | |
323 { | |
324 OrthancPlugins::LogError("The list of extensions is badly formatted (must be a JSON object)"); | |
325 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
326 } | |
327 | |
328 Json::Value::Members members = extensions.getMemberNames(); | |
329 | |
330 for (Json::Value::Members::const_iterator | |
331 it = members.begin(); it != members.end(); ++it) | |
332 { | |
333 if (extensions[*it].type() != Json::stringValue) | |
334 { | |
335 OrthancPlugins::LogError("The file extension \"" + *it + | |
336 "\" must be associated with a string value (its MIME type)"); | |
337 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); | |
338 } | |
339 | |
340 const std::string& mime = extensions[*it].asString(); | |
341 | |
342 std::string name = *it; | |
343 | |
344 if (!name.empty() && | |
345 name[0] == '.') | |
346 { | |
347 name = name.substr(1); // Remove the leading dot "." | |
348 } | |
349 | |
350 extensions_[name] = mime; | |
351 | |
352 if (mime.empty()) | |
353 { | |
354 OrthancPlugins::LogWarning("ServeFolders: Removing MIME type for file extension \"." + | |
355 name + "\""); | |
356 } | |
357 else | |
358 { | |
359 OrthancPlugins::LogWarning("ServeFolders: Associating file extension \"." + name + | |
360 "\" with MIME type \"" + mime + "\""); | |
361 } | |
362 } | |
363 } | |
364 | |
365 | |
366 static void ReadConfiguration() | |
367 { | |
368 OrthancPlugins::OrthancConfiguration configuration; | |
369 | |
370 { | |
371 OrthancPlugins::OrthancConfiguration globalConfiguration; | |
372 globalConfiguration.GetSection(configuration, "ServeFolders"); | |
373 } | |
374 | |
375 if (!configuration.IsSection("Folders")) | |
376 { | |
377 // This is a basic configuration | |
378 ConfigureFolders(configuration.GetJson()); | |
379 } | |
380 else | |
381 { | |
382 // This is an advanced configuration | |
383 ConfigureFolders(configuration.GetJson()["Folders"]); | |
384 | |
385 bool tmp; | |
386 | |
387 if (configuration.LookupBooleanValue(tmp, "AllowCache")) | |
388 { | |
389 allowCache_ = tmp; | |
390 OrthancPlugins::LogWarning("ServeFolders: Requesting the HTTP client to " + | |
391 std::string(tmp ? "enable" : "disable") + | |
392 " its caching mechanism"); | |
393 } | |
394 | |
395 if (configuration.LookupBooleanValue(tmp, "GenerateETag")) | |
396 { | |
397 generateETag_ = tmp; | |
398 OrthancPlugins::LogWarning("ServeFolders: The computation of an ETag for the " | |
399 "served resources is " + | |
400 std::string(tmp ? "enabled" : "disabled")); | |
401 } | |
402 | |
403 OrthancPlugins::OrthancConfiguration extensions; | |
404 configuration.GetSection(extensions, "Extensions"); | |
405 ConfigureExtensions(extensions.GetJson()); | |
406 } | |
407 | |
408 if (folders_.empty()) | |
409 { | |
410 OrthancPlugins::LogWarning("ServeFolders: Empty configuration file: " | |
411 "No additional folder will be served!"); | |
412 } | |
413 } | |
414 | |
415 | |
416 extern "C" | |
417 { | |
418 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context) | |
419 { | |
420 OrthancPlugins::SetGlobalContext(context); | |
421 | |
422 /* Check the version of the Orthanc core */ | |
423 if (OrthancPluginCheckVersion(context) == 0) | |
424 { | |
425 OrthancPlugins::ReportMinimalOrthancVersion(ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER, | |
426 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER, | |
427 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); | |
428 return -1; | |
429 } | |
430 | |
431 RegisterDefaultExtensions(); | |
432 OrthancPluginSetDescription(context, "Serve additional folders with the HTTP server of Orthanc."); | |
433 OrthancPluginSetRootUri(context, INDEX_URI); | |
434 OrthancPlugins::RegisterRestCallback<ListServedFolders>(INDEX_URI, true); | |
435 | |
436 try | |
437 { | |
438 ReadConfiguration(); | |
439 } | |
440 catch (OrthancPlugins::PluginException& e) | |
441 { | |
442 OrthancPlugins::LogError("Error while initializing the ServeFolders plugin: " + | |
443 std::string(e.What(context))); | |
444 } | |
445 | |
446 return 0; | |
447 } | |
448 | |
449 | |
450 ORTHANC_PLUGINS_API void OrthancPluginFinalize() | |
451 { | |
452 } | |
453 | |
454 | |
455 ORTHANC_PLUGINS_API const char* OrthancPluginGetName() | |
456 { | |
457 return "serve-folders"; | |
458 } | |
459 | |
460 | |
461 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion() | |
462 { | |
463 return SERVE_FOLDERS_VERSION; | |
464 } | |
465 } |