# HG changeset patch # User Sebastien Jodogne # Date 1540898995 -3600 # Node ID 2a504fef4ed77e3d7159db12056a1de6b29a133e # Parent ae20fccdd867d2d33aee4727433af5c79176f13e AutodetectMimeType() now using boost::filesystem diff -r ae20fccdd867 -r 2a504fef4ed7 Core/HttpServer/EmbeddedResourceHttpHandler.cpp --- a/Core/HttpServer/EmbeddedResourceHttpHandler.cpp Tue Oct 30 11:55:23 2018 +0100 +++ b/Core/HttpServer/EmbeddedResourceHttpHandler.cpp Tue Oct 30 12:29:55 2018 +0100 @@ -36,6 +36,7 @@ #include "../Logging.h" #include "../OrthancException.h" +#include "../SystemToolbox.h" #include "HttpOutput.h" #include @@ -77,7 +78,7 @@ } std::string resourcePath = Toolbox::FlattenUri(uri, baseUri_.size()); - std::string contentType = Toolbox::AutodetectMimeType(resourcePath); + std::string contentType = SystemToolbox::AutodetectMimeType(resourcePath); try { diff -r ae20fccdd867 -r 2a504fef4ed7 Core/HttpServer/HttpFileSender.cpp --- a/Core/HttpServer/HttpFileSender.cpp Tue Oct 30 11:55:23 2018 +0100 +++ b/Core/HttpServer/HttpFileSender.cpp Tue Oct 30 12:29:55 2018 +0100 @@ -36,6 +36,7 @@ #include "../OrthancException.h" #include "../Toolbox.h" +#include "../SystemToolbox.h" #include @@ -47,7 +48,7 @@ if (contentType_.empty()) { - contentType_ = Toolbox::AutodetectMimeType(filename); + contentType_ = SystemToolbox::AutodetectMimeType(filename); } } diff -r ae20fccdd867 -r 2a504fef4ed7 Core/SystemToolbox.cpp --- a/Core/SystemToolbox.cpp Tue Oct 30 11:55:23 2018 +0100 +++ b/Core/SystemToolbox.cpp Tue Oct 30 12:29:55 2018 +0100 @@ -578,4 +578,71 @@ return threads; } } + + + std::string SystemToolbox::AutodetectMimeType(const std::string& path) + { + std::string extension = boost::filesystem::extension(path); + Toolbox::ToLowerCase(extension); + + // http://en.wikipedia.org/wiki/Mime_types + // Text types + if (extension == ".txt") + { + return MIME_PLAIN_TEXT; + } + else if (extension == ".html") + { + return "text/html"; + } + else if (extension == ".xml") + { + return MIME_XML; + } + else if (extension == ".css") + { + return "text/css"; + } + + // Application types + else if (extension == ".js") + { + return "application/javascript"; + } + else if (extension == ".json") + { + return MIME_JSON; + } + else if (extension == ".pdf") + { + return MIME_PDF; + } + else if (extension == ".wasm") + { + return "application/wasm"; + } + + // Images types + else if (extension == ".jpg" || + extension == ".jpeg") + { + return MIME_JPEG; + } + else if (extension == ".gif") + { + return "image/gif"; + } + else if (extension == ".png") + { + return MIME_PNG; + } + else if (extension == ".pam") + { + return MIME_PAM; + } + else + { + return ""; + } + } } diff -r ae20fccdd867 -r 2a504fef4ed7 Core/SystemToolbox.h --- a/Core/SystemToolbox.h Tue Oct 30 11:55:23 2018 +0100 +++ b/Core/SystemToolbox.h Tue Oct 30 12:29:55 2018 +0100 @@ -100,5 +100,7 @@ bool utc); unsigned int GetHardwareConcurrency(); + + std::string AutodetectMimeType(const std::string& path); } } diff -r ae20fccdd867 -r 2a504fef4ed7 Core/Toolbox.cpp --- a/Core/Toolbox.cpp Tue Oct 30 11:55:23 2018 +0100 +++ b/Core/Toolbox.cpp Tue Oct 30 12:29:55 2018 +0100 @@ -310,57 +310,6 @@ } - std::string Toolbox::AutodetectMimeType(const std::string& path) - { - std::string contentType; - size_t lastDot = path.rfind('.'); - size_t lastSlash = path.rfind('/'); - - if (lastDot == std::string::npos || - (lastSlash != std::string::npos && lastDot < lastSlash)) - { - // No trailing dot, unable to detect the content type - } - else - { - const char* extension = &path[lastDot + 1]; - - // http://en.wikipedia.org/wiki/Mime_types - // Text types - if (!strcmp(extension, "txt")) - contentType = MIME_PLAIN_TEXT; - else if (!strcmp(extension, "html")) - contentType = "text/html"; - else if (!strcmp(extension, "xml")) - contentType = MIME_XML; - else if (!strcmp(extension, "css")) - contentType = "text/css"; - - // Application types - else if (!strcmp(extension, "js")) - contentType = "application/javascript"; - else if (!strcmp(extension, "json")) - contentType = MIME_JSON; - else if (!strcmp(extension, "pdf")) - contentType = MIME_PDF; - else if (!strcmp(extension, "wasm")) - contentType = "application/wasm"; - - // Images types - else if (!strcmp(extension, "jpg") || !strcmp(extension, "jpeg")) - contentType = MIME_JPEG; - else if (!strcmp(extension, "gif")) - contentType = "image/gif"; - else if (!strcmp(extension, "png")) - contentType = MIME_PNG; - else if (!strcmp(extension, "pam")) - contentType = MIME_PAM; - } - - return contentType; - } - - std::string Toolbox::FlattenUri(const UriComponents& components, size_t fromLevel) { diff -r ae20fccdd867 -r 2a504fef4ed7 Core/Toolbox.h --- a/Core/Toolbox.h Tue Oct 30 11:55:23 2018 +0100 +++ b/Core/Toolbox.h Tue Oct 30 12:29:55 2018 +0100 @@ -117,8 +117,6 @@ bool IsChildUri(const UriComponents& baseUri, const UriComponents& testedUri); - std::string AutodetectMimeType(const std::string& path); - std::string FlattenUri(const UriComponents& components, size_t fromLevel = 0); diff -r ae20fccdd867 -r 2a504fef4ed7 UnitTestsSources/UnitTestsMain.cpp --- a/UnitTestsSources/UnitTestsMain.cpp Tue Oct 30 11:55:23 2018 +0100 +++ b/UnitTestsSources/UnitTestsMain.cpp Tue Oct 30 12:29:55 2018 +0100 @@ -301,26 +301,30 @@ TEST(Uri, AutodetectMimeType) { - ASSERT_EQ("", Toolbox::AutodetectMimeType("../NOTES")); - ASSERT_EQ("", Toolbox::AutodetectMimeType("")); - ASSERT_EQ("", Toolbox::AutodetectMimeType("/")); - ASSERT_EQ("", Toolbox::AutodetectMimeType("a/a")); - - ASSERT_EQ("text/plain", Toolbox::AutodetectMimeType("../NOTES.txt")); - ASSERT_EQ("text/plain", Toolbox::AutodetectMimeType("../coucou.xml/NOTES.txt")); - ASSERT_EQ("application/xml", Toolbox::AutodetectMimeType("../.xml")); + ASSERT_EQ("", SystemToolbox::AutodetectMimeType("../NOTES")); + ASSERT_EQ("", SystemToolbox::AutodetectMimeType("")); + ASSERT_EQ("", SystemToolbox::AutodetectMimeType("/")); + ASSERT_EQ("", SystemToolbox::AutodetectMimeType("a/a")); + ASSERT_EQ("", SystemToolbox::AutodetectMimeType("..\\a\\")); + ASSERT_EQ("", SystemToolbox::AutodetectMimeType("..\\a\\a")); - ASSERT_EQ("application/javascript", Toolbox::AutodetectMimeType("NOTES.js")); - ASSERT_EQ("application/json", Toolbox::AutodetectMimeType("NOTES.json")); - ASSERT_EQ("application/pdf", Toolbox::AutodetectMimeType("NOTES.pdf")); - ASSERT_EQ("text/css", Toolbox::AutodetectMimeType("NOTES.css")); - ASSERT_EQ("text/html", Toolbox::AutodetectMimeType("NOTES.html")); - ASSERT_EQ("text/plain", Toolbox::AutodetectMimeType("NOTES.txt")); - ASSERT_EQ("application/xml", Toolbox::AutodetectMimeType("NOTES.xml")); - ASSERT_EQ("image/gif", Toolbox::AutodetectMimeType("NOTES.gif")); - ASSERT_EQ("image/jpeg", Toolbox::AutodetectMimeType("NOTES.jpg")); - ASSERT_EQ("image/jpeg", Toolbox::AutodetectMimeType("NOTES.jpeg")); - ASSERT_EQ("image/png", Toolbox::AutodetectMimeType("NOTES.png")); + ASSERT_EQ("text/plain", SystemToolbox::AutodetectMimeType("../NOTES.txt")); + ASSERT_EQ("text/plain", SystemToolbox::AutodetectMimeType("../coucou.xml/NOTES.txt")); + ASSERT_EQ("application/xml", SystemToolbox::AutodetectMimeType("..\\coucou.\\NOTES.xml")); + ASSERT_EQ("application/xml", SystemToolbox::AutodetectMimeType("../.xml")); + ASSERT_EQ("application/xml", SystemToolbox::AutodetectMimeType("../.XmL")); + + ASSERT_EQ("application/javascript", SystemToolbox::AutodetectMimeType("NOTES.js")); + ASSERT_EQ("application/json", SystemToolbox::AutodetectMimeType("NOTES.json")); + ASSERT_EQ("application/pdf", SystemToolbox::AutodetectMimeType("NOTES.pdf")); + ASSERT_EQ("text/css", SystemToolbox::AutodetectMimeType("NOTES.css")); + ASSERT_EQ("text/html", SystemToolbox::AutodetectMimeType("NOTES.html")); + ASSERT_EQ("text/plain", SystemToolbox::AutodetectMimeType("NOTES.txt")); + ASSERT_EQ("application/xml", SystemToolbox::AutodetectMimeType("NOTES.xml")); + ASSERT_EQ("image/gif", SystemToolbox::AutodetectMimeType("NOTES.gif")); + ASSERT_EQ("image/jpeg", SystemToolbox::AutodetectMimeType("NOTES.jpg")); + ASSERT_EQ("image/jpeg", SystemToolbox::AutodetectMimeType("NOTES.jpeg")); + ASSERT_EQ("image/png", SystemToolbox::AutodetectMimeType("NOTES.png")); } TEST(Toolbox, ComputeMD5)