# HG changeset patch # User Sebastien Jodogne # Date 1467464943 -7200 # Node ID 40ffd0e8676a8b8ed40c66bd1e372531a7f34033 # Parent 8856f15b3e02764d95df1fe366527b859865cce6 generation of etag in ServeFolders diff -r 8856f15b3e02 -r 40ffd0e8676a CMakeLists.txt --- a/CMakeLists.txt Sat Jul 02 11:16:25 2016 +0200 +++ b/CMakeLists.txt Sat Jul 02 15:09:03 2016 +0200 @@ -546,6 +546,7 @@ ${BOOST_SOURCES} ${JSONCPP_SOURCES} Plugins/Samples/ServeFolders/Plugin.cpp + Plugins/Samples/Common/OrthancPluginCppWrapper.cpp ${SERVE_FOLDERS_RESOURCES} ) diff -r 8856f15b3e02 -r 40ffd0e8676a Plugins/Samples/Common/OrthancPluginCppWrapper.cpp --- a/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Sat Jul 02 11:16:25 2016 +0200 +++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Sat Jul 02 15:09:03 2016 +0200 @@ -61,6 +61,18 @@ } + void MemoryBuffer::Check(OrthancPluginErrorCode code) + { + if (code != OrthancPluginErrorCode_Success) + { + // Prevent using garbage information + buffer_.data = NULL; + buffer_.size = 0; + throw PluginException(code); + } + } + + MemoryBuffer::MemoryBuffer(OrthancPluginContext* context) : context_(context) { @@ -250,7 +262,14 @@ Json::FastWriter writer; std::string s = writer.write(tags); - PluginException::Check(OrthancPluginCreateDicom(context_, &buffer_, s.c_str(), NULL, flags)); + Check(OrthancPluginCreateDicom(context_, &buffer_, s.c_str(), NULL, flags)); + } + + + void MemoryBuffer::ReadFile(const std::string& path) + { + Clear(); + Check(OrthancPluginReadFile(context_, &buffer_, path.c_str())); } diff -r 8856f15b3e02 -r 40ffd0e8676a Plugins/Samples/Common/OrthancPluginCppWrapper.h --- a/Plugins/Samples/Common/OrthancPluginCppWrapper.h Sat Jul 02 11:16:25 2016 +0200 +++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.h Sat Jul 02 15:09:03 2016 +0200 @@ -76,6 +76,8 @@ OrthancPluginContext* context_; OrthancPluginMemoryBuffer buffer_; + void Check(OrthancPluginErrorCode code); + public: MemoryBuffer(OrthancPluginContext* context); @@ -152,6 +154,8 @@ void CreateDicom(const Json::Value& tags, OrthancPluginCreateDicomFlags flags); + + void ReadFile(const std::string& path); }; diff -r 8856f15b3e02 -r 40ffd0e8676a Plugins/Samples/ServeFolders/CMakeLists.txt --- a/Plugins/Samples/ServeFolders/CMakeLists.txt Sat Jul 02 11:16:25 2016 +0200 +++ b/Plugins/Samples/ServeFolders/CMakeLists.txt Sat Jul 02 15:09:03 2016 +0200 @@ -16,6 +16,7 @@ add_library(ServeFolders SHARED Plugin.cpp + ${CMAKE_SOURCE_DIR}/../Common/OrthancPluginCppWrapper.cpp ${JSONCPP_SOURCES} ${BOOST_SOURCES} ) diff -r 8856f15b3e02 -r 40ffd0e8676a Plugins/Samples/ServeFolders/Plugin.cpp --- a/Plugins/Samples/ServeFolders/Plugin.cpp Sat Jul 02 11:16:25 2016 +0200 +++ b/Plugins/Samples/ServeFolders/Plugin.cpp Sat Jul 02 15:09:03 2016 +0200 @@ -18,17 +18,19 @@ **/ -#include +#include "../Common/OrthancPluginCppWrapper.h" #include #include #include +#include static OrthancPluginContext* context_ = NULL; static std::map folders_; static const char* INDEX_URI = "/app/plugin-serve-folders.html"; static bool allowCache_ = true; +static bool generateETag_ = true; // TODO parameter static void SetHttpHeaders(OrthancPluginRestOutput* output) @@ -112,17 +114,17 @@ static bool ReadFile(std::string& target, const std::string& path) { - OrthancPluginMemoryBuffer buffer; - if (OrthancPluginReadFile(context_, &buffer, path.c_str())) + try + { + OrthancPlugins::MemoryBuffer buffer(context_); + buffer.ReadFile(path); + buffer.ToString(target); + return true; + } + catch (OrthancPlugins::PluginException) { return false; } - else - { - target.assign(reinterpret_cast(buffer.data), buffer.size); - OrthancPluginFreeMemoryBuffer(context_, &buffer); - return true; - } } @@ -247,6 +249,18 @@ if (ReadFile(s, path)) { const char* resource = s.size() ? s.c_str() : NULL; + + if (generateETag_) + { + OrthancPlugins::OrthancString md5(context_, OrthancPluginComputeMd5(context_, resource, s.size())); + std::string etag = "\"" + std::string(md5.GetContent()) + "\""; + OrthancPluginSetHttpHeader(context_, output, "ETag", etag.c_str()); + } + + boost::posix_time::ptime lastModification = boost::posix_time::from_time_t(fs::last_write_time(path)); + std::string t = boost::posix_time::to_iso_string(lastModification); + OrthancPluginSetHttpHeader(context_, output, "Last-Modified", t.c_str()); + SetHttpHeaders(output); OrthancPluginAnswerBuffer(context_, output, resource, s.size(), mime); }