diff --git a/src/oatpp-swagger/AsyncController.hpp b/src/oatpp-swagger/AsyncController.hpp index f1e5140..e4270ec 100644 --- a/src/oatpp-swagger/AsyncController.hpp +++ b/src/oatpp-swagger/AsyncController.hpp @@ -154,9 +154,14 @@ class AsyncController : public oatpp::web::server::api::ApiController { auto body = std::make_shared( controller->m_resources->getResourceStream(filename->c_str()) ); - return _return(OutgoingResponse::createShared(Status::CODE_200, body)); + auto resp = OutgoingResponse::createShared(Status::CODE_200, body); + resp->putHeader("Content-Type", controller->m_resources->getMimeType(filename)); + return _return(resp); } - return _return(controller->createResponse(Status::CODE_200, controller->m_resources->getResource(filename->c_str()))); + auto resp = controller->createResponse(Status::CODE_200, + controller->m_resources->getResource(filename->c_str())); + resp->putHeader("Content-Type", controller->m_resources->getMimeType(filename)); + return _return(resp); } }; diff --git a/src/oatpp-swagger/Controller.hpp b/src/oatpp-swagger/Controller.hpp index b26c3d5..836cdb0 100644 --- a/src/oatpp-swagger/Controller.hpp +++ b/src/oatpp-swagger/Controller.hpp @@ -134,9 +134,13 @@ class Controller : public oatpp::web::server::api::ApiController { auto body = std::make_shared( m_resources->getResourceStream(filename->c_str()) ); - return OutgoingResponse::createShared(Status::CODE_200, body); + auto resp = OutgoingResponse::createShared(Status::CODE_200, body); + resp->putHeader("Content-Type", m_resources->getMimeType(filename)); + return resp; } - return createResponse(Status::CODE_200, m_resources->getResource(filename->c_str())); + auto resp = createResponse(Status::CODE_200, m_resources->getResource(filename->c_str())); + resp->putHeader("Content-Type", m_resources->getMimeType(filename)); + return resp; } #include OATPP_CODEGEN_END(ApiController) diff --git a/src/oatpp-swagger/Resources.cpp b/src/oatpp-swagger/Resources.cpp index 111a82c..56135f6 100644 --- a/src/oatpp-swagger/Resources.cpp +++ b/src/oatpp-swagger/Resources.cpp @@ -104,4 +104,26 @@ v_io_size Resources::ReadCallback::read(void *buffer, v_buff_size count, async:: return m_stream.read(buffer, count, action); } +bool Resources::hasEnding(std::string fullString, std::string const &ending) const { + std::transform(fullString.begin(), fullString.end(), fullString.begin(), + [](unsigned char c) { return std::tolower(c); }); + if (fullString.length() >= ending.length()) { + return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending)); + } else { + return false; + } +} + +std::string Resources::getMimeType(const std::string &filename) const { + if (hasEnding(filename, ".html")) return "text/html"; + if (hasEnding(filename, ".jpg")) return "image/jpeg"; + if (hasEnding(filename, ".jpeg")) return "image/jpeg"; + if (hasEnding(filename, ".png")) return "image/png"; + if (hasEnding(filename, ".gif")) return "image/gif"; + if (hasEnding(filename, ".css")) return "text/css"; + if (hasEnding(filename, ".js")) return "text/javascript"; + if (hasEnding(filename, ".xml")) return "text/xml"; + return "text/plain"; +} + }} diff --git a/src/oatpp-swagger/Resources.hpp b/src/oatpp-swagger/Resources.hpp index 32c79b1..11c12e3 100644 --- a/src/oatpp-swagger/Resources.hpp +++ b/src/oatpp-swagger/Resources.hpp @@ -45,6 +45,7 @@ class Resources { private: oatpp::String loadFromFile(const char* fileName); void cacheResource(const char* fileName); + bool hasEnding(std::string fullString, std::string const &ending) const; class ReadCallback : public oatpp::data::stream::ReadCallback { private: @@ -126,6 +127,13 @@ class Resources { bool isStreaming() { return m_streaming; } + + /** + * Returns the MIME type for a given filename + * @param filename to return the MIME type + * @return a MIME type + */ + std::string getMimeType(const std::string &filename) const; }; }}