From 221e35022898db913859c2a04713f744e4ded105 Mon Sep 17 00:00:00 2001 From: EinMByte Date: Thu, 17 Sep 2015 11:59:04 +0200 Subject: [PATCH] Move MIME type detection to util/HTTP.cpp --- client/HTTPServer.cpp | 8 +------- core/util/HTTP.cpp | 13 +++++++++++++ core/util/HTTP.h | 5 +++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/client/HTTPServer.cpp b/client/HTTPServer.cpp index f4e5efff..1a73272c 100644 --- a/client/HTTPServer.cpp +++ b/client/HTTPServer.cpp @@ -152,13 +152,7 @@ void HTTPConnection::HandleRequest() str = i2p::util::http::preprocessContent(str, address.parent_path().string()); m_Reply = i2p::util::http::Response(200, str); - // TODO: get rid of this hack, actually determine the MIME type - if(address_str.substr(address_str.find_last_of(".")) == ".css") - m_Reply.setHeader("Content-Type", "text/css"); - else if(address_str.substr(address_str.find_last_of(".")) == ".js") - m_Reply.setHeader("Content-Type", "text/javascript"); - else - m_Reply.setHeader("Content-Type", "text/html"); + m_Reply.setHeader("Content-Type", i2p::util::http::getMimeType(address_str)); SendReply(); } diff --git a/core/util/HTTP.cpp b/core/util/HTTP.cpp index 931e0b6a..be8014e1 100644 --- a/core/util/HTTP.cpp +++ b/core/util/HTTP.cpp @@ -227,6 +227,19 @@ std::string preprocessContent(const std::string& content, const std::string& pat return result; } +std::string getMimeType(const std::string& filename) +{ + const std::string ext = filename.substr(filename.find_last_of(".")); + if(ext == ".css") + return "text/css"; + else if(ext == ".css") + return "text/javascript"; + else if(ext == ".html" || ext == ".htm") + return "text/html"; + else + return "application/octet-stream"; +} + } } } diff --git a/core/util/HTTP.h b/core/util/HTTP.h index 41e4d2af..3f6415dc 100644 --- a/core/util/HTTP.h +++ b/core/util/HTTP.h @@ -92,6 +92,11 @@ private: */ std::string preprocessContent(const std::string& content, const std::string& path); +/** + * @return the MIME type based on the extension of the given filename + */ +std::string getMimeType(const std::string& filename); + } } }