comparison Plugins/Samples/ServeFolders/Plugin.cpp @ 2061:8856f15b3e02

disabling cache in ServeFolders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 02 Jul 2016 11:16:25 +0200
parents 7a05144cb919
children 40ffd0e8676a
comparison
equal deleted inserted replaced
2060:e12d83e9ee59 2061:8856f15b3e02
26 26
27 27
28 static OrthancPluginContext* context_ = NULL; 28 static OrthancPluginContext* context_ = NULL;
29 static std::map<std::string, std::string> folders_; 29 static std::map<std::string, std::string> folders_;
30 static const char* INDEX_URI = "/app/plugin-serve-folders.html"; 30 static const char* INDEX_URI = "/app/plugin-serve-folders.html";
31 static bool allowCache_ = true;
32
33
34 static void SetHttpHeaders(OrthancPluginRestOutput* output)
35 {
36 if (!allowCache_)
37 {
38 // http://stackoverflow.com/a/2068407/881731
39 OrthancPluginSetHttpHeader(context_, output, "Cache-Control", "no-cache, no-store, must-revalidate");
40 OrthancPluginSetHttpHeader(context_, output, "Pragma", "no-cache");
41 OrthancPluginSetHttpHeader(context_, output, "Expires", "0");
42 }
43 }
31 44
32 45
33 static const char* GetMimeType(const std::string& path) 46 static const char* GetMimeType(const std::string& path)
34 { 47 {
35 size_t dot = path.find_last_of('.'); 48 size_t dot = path.find_last_of('.');
55 } 68 }
56 else if (extension == ".svg") 69 else if (extension == ".svg")
57 { 70 {
58 return "image/svg+xml"; 71 return "image/svg+xml";
59 } 72 }
60 else if (extension == ".json") 73 else if (extension == ".json" ||
74 extension == ".nmf")
61 { 75 {
62 return "application/json"; 76 return "application/json";
63 } 77 }
64 else if (extension == ".xml") 78 else if (extension == ".xml")
65 { 79 {
67 } 81 }
68 else if (extension == ".png") 82 else if (extension == ".png")
69 { 83 {
70 return "image/png"; 84 return "image/png";
71 } 85 }
72 else if (extension == ".jpg" || extension == ".jpeg") 86 else if (extension == ".jpg" ||
87 extension == ".jpeg")
73 { 88 {
74 return "image/jpeg"; 89 return "image/jpeg";
75 } 90 }
76 else if (extension == ".woff") 91 else if (extension == ".woff")
77 { 92 {
78 return "application/x-font-woff"; 93 return "application/x-font-woff";
94 }
95 else if (extension == ".pexe")
96 {
97 return "application/x-pnacl";
98 }
99 else if (extension == ".nexe")
100 {
101 return "application/x-nacl";
79 } 102 }
80 else 103 else
81 { 104 {
82 std::string s = "Unknown MIME type for extension: " + extension; 105 std::string s = "Unknown MIME type for extension: " + extension;
83 OrthancPluginLogWarning(context_, s.c_str()); 106 OrthancPluginLogWarning(context_, s.c_str());
210 233
211 s += " </ul>\n"; 234 s += " </ul>\n";
212 s += " </body>\n"; 235 s += " </body>\n";
213 s += "</html>\n"; 236 s += "</html>\n";
214 237
238 SetHttpHeaders(output);
215 OrthancPluginAnswerBuffer(context_, output, s.c_str(), s.size(), "text/html"); 239 OrthancPluginAnswerBuffer(context_, output, s.c_str(), s.size(), "text/html");
216 } 240 }
217 else 241 else
218 { 242 {
219 std::string path = folder + "/" + item.string(); 243 std::string path = folder + "/" + item.string();
221 245
222 std::string s; 246 std::string s;
223 if (ReadFile(s, path)) 247 if (ReadFile(s, path))
224 { 248 {
225 const char* resource = s.size() ? s.c_str() : NULL; 249 const char* resource = s.size() ? s.c_str() : NULL;
250 SetHttpHeaders(output);
226 OrthancPluginAnswerBuffer(context_, output, resource, s.size(), mime); 251 OrthancPluginAnswerBuffer(context_, output, resource, s.size(), mime);
227 } 252 }
228 else 253 else
229 { 254 {
230 std::string s = "Inexistent file in served folder: " + path; 255 std::string s = "Inexistent file in served folder: " + path;
267 s += "</ul>\n"; 292 s += "</ul>\n";
268 } 293 }
269 294
270 s += "</body></html>\n"; 295 s += "</body></html>\n";
271 296
297 SetHttpHeaders(output);
272 OrthancPluginAnswerBuffer(context_, output, s.c_str(), s.size(), "text/html"); 298 OrthancPluginAnswerBuffer(context_, output, s.c_str(), s.size(), "text/html");
273 299
274 return OrthancPluginErrorCode_Success; 300 return OrthancPluginErrorCode_Success;
275 } 301 }
276 302
300 if (!ReadConfiguration(configuration, context_)) 326 if (!ReadConfiguration(configuration, context_))
301 { 327 {
302 return -1; 328 return -1;
303 } 329 }
304 330
331 if (configuration.isMember("ServeFoldersNoCache"))
332 {
333 OrthancPluginLogWarning(context_, "Disabling the cache");
334 allowCache_ = false;
335 }
336
305 if (configuration.isMember("ServeFolders")) 337 if (configuration.isMember("ServeFolders"))
306 { 338 {
307 if (configuration["ServeFolders"].type() != Json::objectValue) 339 if (configuration["ServeFolders"].type() != Json::objectValue)
308 { 340 {
309 OrthancPluginLogError(context_, "The \"ServeFolders\" configuration section is badly formatted (must be a JSON object)"); 341 OrthancPluginLogError(context_, "The \"ServeFolders\" configuration section is badly formatted (must be a JSON object)");