diff --git a/libresapi/src/api/ApiServerMHD.cpp b/libresapi/src/api/ApiServerMHD.cpp index 36d7a2c1b..18afe449e 100644 --- a/libresapi/src/api/ApiServerMHD.cpp +++ b/libresapi/src/api/ApiServerMHD.cpp @@ -7,6 +7,7 @@ #include #include +#include "util/ContentTypes.h" // for filestreamer #include @@ -302,8 +303,13 @@ public: struct MHD_Response* resp = MHD_create_response_from_callback( mSize, 1024*1024, &contentReadercallback, this, NULL); - // only mp3 at the moment - MHD_add_response_header(resp, "Content-Type", "audio/mpeg3"); + // get content-type from extension + std::string ext = ""; + unsigned int i = info.fname.rfind('.'); + if(i != std::string::npos) + ext = info.fname.substr(i+1); + MHD_add_response_header(resp, "Content-Type", ContentTypes::ContentTypes::cTypeFromExt(ext).c_str()); + secure_queue_response(connection, MHD_HTTP_OK, resp); MHD_destroy_response(resp); return MHD_YES; diff --git a/libresapi/src/libresapi.pro b/libresapi/src/libresapi.pro index d814219db..708515439 100644 --- a/libresapi/src/libresapi.pro +++ b/libresapi/src/libresapi.pro @@ -59,7 +59,8 @@ SOURCES += \ api/GetPluginInterfaces.cpp \ api/ChatHandler.cpp \ api/LivereloadHandler.cpp \ - api/TmpBlobStore.cpp + api/TmpBlobStore.cpp \ + util/ContentTypes.cpp HEADERS += \ api/ApiServer.h \ @@ -82,4 +83,5 @@ HEADERS += \ api/GetPluginInterfaces.h \ api/ChatHandler.h \ api/LivereloadHandler.h \ - api/TmpBlobStore.h + api/TmpBlobStore.h \ + util/ContentTypes.h diff --git a/libresapi/src/util/ContentTypes.cpp b/libresapi/src/util/ContentTypes.cpp new file mode 100644 index 000000000..c8915e482 --- /dev/null +++ b/libresapi/src/util/ContentTypes.cpp @@ -0,0 +1,63 @@ +#include "ContentTypes.h" +#include + +RsMutex ContentTypes::ctmtx = RsMutex("CTMTX"); +std::map ContentTypes::cache; + +#ifdef WINDOWS_SYS + //Next to the executable + const char* ContentTypes::filename = ".\\mime.types"; +#else + const char* ContentTypes::filename = "/etc/mime.types"; +#endif + +std::string ContentTypes::cTypeFromExt(const std::string &extension) +{ + if(extension.empty()) + return DEFAULTCT; + + RsStackMutex mtx(ctmtx); + + //looking into the cache + std::map::iterator it; + it = cache.find(extension); + if (it != cache.end()) + { + std::cout << "Mime " + it->second + " for extension ." + extension + " was found in cache" << std::endl; + return it->second; + } + + //looking into mime.types + std::string line; + std::string ext; + std::ifstream file(filename); + while(getline(file, line)) + { + if(line.empty() || line[0] == '#') continue; + unsigned int i = line.find_first_of("\t "); + unsigned int j; + while(i != std::string::npos) //tokenize + { + j = i; + i = line.find_first_of("\t ", i+1); + if(i == std::string::npos) + ext = line.substr(j+1); + else + ext = line.substr(j+1, i-j-1); + + if(extension == ext) + { + std::string mime = line.substr(0, line.find_first_of("\t ")); + cache[extension] = mime; + std::cout << "Mime " + mime + " for extension ." + extension + " was found in mime.types" << std::endl; + return mime; + } + } + } + + //nothing found + std::cout << "Mime for " + extension + " was not found in " + filename + " falling back to " << DEFAULTCT << std::endl; + cache[extension] = DEFAULTCT; + return DEFAULTCT; +} + diff --git a/libresapi/src/util/ContentTypes.h b/libresapi/src/util/ContentTypes.h new file mode 100644 index 000000000..b0c5d0e65 --- /dev/null +++ b/libresapi/src/util/ContentTypes.h @@ -0,0 +1,21 @@ +#ifndef CONTENTTYPES_H +#define CONTENTTYPES_H + +#include +#include +#include + +#define DEFAULTCT "application/octet-stream" + +class ContentTypes +{ +public: + static std::string cTypeFromExt(const std::string& extension); + +private: + static std::map cache; + static RsMutex ctmtx; + static const char* filename; +}; + +#endif // CONTENTTYPES_H