Mercurial > hg > orthanc
comparison Plugins/Samples/ServeFolders/Plugin.cpp @ 2062:40ffd0e8676a
generation of etag in ServeFolders
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sat, 02 Jul 2016 15:09:03 +0200 |
parents | 8856f15b3e02 |
children | 700dbb3baac9 |
comparison
equal
deleted
inserted
replaced
2061:8856f15b3e02 | 2062:40ffd0e8676a |
---|---|
16 * You should have received a copy of the GNU General Public License | 16 * You should have received a copy of the GNU General Public License |
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. | 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 **/ | 18 **/ |
19 | 19 |
20 | 20 |
21 #include <orthanc/OrthancCPlugin.h> | 21 #include "../Common/OrthancPluginCppWrapper.h" |
22 | 22 |
23 #include <json/reader.h> | 23 #include <json/reader.h> |
24 #include <json/value.h> | 24 #include <json/value.h> |
25 #include <boost/filesystem.hpp> | 25 #include <boost/filesystem.hpp> |
26 #include <boost/date_time/posix_time/posix_time.hpp> | |
26 | 27 |
27 | 28 |
28 static OrthancPluginContext* context_ = NULL; | 29 static OrthancPluginContext* context_ = NULL; |
29 static std::map<std::string, std::string> folders_; | 30 static std::map<std::string, std::string> folders_; |
30 static const char* INDEX_URI = "/app/plugin-serve-folders.html"; | 31 static const char* INDEX_URI = "/app/plugin-serve-folders.html"; |
31 static bool allowCache_ = true; | 32 static bool allowCache_ = true; |
33 static bool generateETag_ = true; // TODO parameter | |
32 | 34 |
33 | 35 |
34 static void SetHttpHeaders(OrthancPluginRestOutput* output) | 36 static void SetHttpHeaders(OrthancPluginRestOutput* output) |
35 { | 37 { |
36 if (!allowCache_) | 38 if (!allowCache_) |
110 | 112 |
111 | 113 |
112 static bool ReadFile(std::string& target, | 114 static bool ReadFile(std::string& target, |
113 const std::string& path) | 115 const std::string& path) |
114 { | 116 { |
115 OrthancPluginMemoryBuffer buffer; | 117 try |
116 if (OrthancPluginReadFile(context_, &buffer, path.c_str())) | 118 { |
119 OrthancPlugins::MemoryBuffer buffer(context_); | |
120 buffer.ReadFile(path); | |
121 buffer.ToString(target); | |
122 return true; | |
123 } | |
124 catch (OrthancPlugins::PluginException) | |
117 { | 125 { |
118 return false; | 126 return false; |
119 } | |
120 else | |
121 { | |
122 target.assign(reinterpret_cast<const char*>(buffer.data), buffer.size); | |
123 OrthancPluginFreeMemoryBuffer(context_, &buffer); | |
124 return true; | |
125 } | 127 } |
126 } | 128 } |
127 | 129 |
128 | 130 |
129 static bool ReadConfiguration(Json::Value& configuration, | 131 static bool ReadConfiguration(Json::Value& configuration, |
245 | 247 |
246 std::string s; | 248 std::string s; |
247 if (ReadFile(s, path)) | 249 if (ReadFile(s, path)) |
248 { | 250 { |
249 const char* resource = s.size() ? s.c_str() : NULL; | 251 const char* resource = s.size() ? s.c_str() : NULL; |
252 | |
253 if (generateETag_) | |
254 { | |
255 OrthancPlugins::OrthancString md5(context_, OrthancPluginComputeMd5(context_, resource, s.size())); | |
256 std::string etag = "\"" + std::string(md5.GetContent()) + "\""; | |
257 OrthancPluginSetHttpHeader(context_, output, "ETag", etag.c_str()); | |
258 } | |
259 | |
260 boost::posix_time::ptime lastModification = boost::posix_time::from_time_t(fs::last_write_time(path)); | |
261 std::string t = boost::posix_time::to_iso_string(lastModification); | |
262 OrthancPluginSetHttpHeader(context_, output, "Last-Modified", t.c_str()); | |
263 | |
250 SetHttpHeaders(output); | 264 SetHttpHeaders(output); |
251 OrthancPluginAnswerBuffer(context_, output, resource, s.size(), mime); | 265 OrthancPluginAnswerBuffer(context_, output, resource, s.size(), mime); |
252 } | 266 } |
253 else | 267 else |
254 { | 268 { |