From d482dc1c05b319efa3167d05e6d99b6aadaab7b9 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Thu, 2 Jan 2020 18:24:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84http=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8mime=E7=B1=BB=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpFileManager.cpp | 146 +++++++++++++++++++++++++++-------- src/Http/HttpFileManager.h | 7 ++ 2 files changed, 121 insertions(+), 32 deletions(-) diff --git a/src/Http/HttpFileManager.cpp b/src/Http/HttpFileManager.cpp index 6bb1895f..2ffc0819 100644 --- a/src/Http/HttpFileManager.cpp +++ b/src/Http/HttpFileManager.cpp @@ -63,46 +63,128 @@ public: bool _have_find_media_source = false; }; -static const string &getContentType(const char *name) { +static const char *s_mime_src[][2] = { + {"html", "text/html"}, + {"htm", "text/html"}, + {"shtml", "text/html"}, + {"css", "text/css"}, + {"xml", "text/xml"}, + {"gif", "image/gif"}, + {"jpeg", "image/jpeg"}, + {"jpg", "image/jpeg"}, + {"js", "application/javascript"}, + {"map", "application/javascript" }, + {"atom", "application/atom+xml"}, + {"rss", "application/rss+xml"}, + {"mml", "text/mathml"}, + {"txt", "text/plain"}, + {"jad", "text/vnd.sun.j2me.app-descriptor"}, + {"wml", "text/vnd.wap.wml"}, + {"htc", "text/x-component"}, + {"png", "image/png"}, + {"tif", "image/tiff"}, + {"tiff", "image/tiff"}, + {"wbmp", "image/vnd.wap.wbmp"}, + {"ico", "image/x-icon"}, + {"jng", "image/x-jng"}, + {"bmp", "image/x-ms-bmp"}, + {"svg", "image/svg+xml"}, + {"svgz", "image/svg+xml"}, + {"webp", "image/webp"}, + {"woff", "application/font-woff"}, + {"woff2","application/font-woff" }, + {"jar", "application/java-archive"}, + {"war", "application/java-archive"}, + {"ear", "application/java-archive"}, + {"json", "application/json"}, + {"hqx", "application/mac-binhex40"}, + {"doc", "application/msword"}, + {"pdf", "application/pdf"}, + {"ps", "application/postscript"}, + {"eps", "application/postscript"}, + {"ai", "application/postscript"}, + {"rtf", "application/rtf"}, + {"m3u8", "application/vnd.apple.mpegurl"}, + {"xls", "application/vnd.ms-excel"}, + {"eot", "application/vnd.ms-fontobject"}, + {"ppt", "application/vnd.ms-powerpoint"}, + {"wmlc", "application/vnd.wap.wmlc"}, + {"kml", "application/vnd.google-earth.kml+xml"}, + {"kmz", "application/vnd.google-earth.kmz"}, + {"7z", "application/x-7z-compressed"}, + {"cco", "application/x-cocoa"}, + {"jardiff", "application/x-java-archive-diff"}, + {"jnlp", "application/x-java-jnlp-file"}, + {"run", "application/x-makeself"}, + {"pl", "application/x-perl"}, + {"pm", "application/x-perl"}, + {"prc", "application/x-pilot"}, + {"pdb", "application/x-pilot"}, + {"rar", "application/x-rar-compressed"}, + {"rpm", "application/x-redhat-package-manager"}, + {"sea", "application/x-sea"}, + {"swf", "application/x-shockwave-flash"}, + {"sit", "application/x-stuffit"}, + {"tcl", "application/x-tcl"}, + {"tk", "application/x-tcl"}, + {"der", "application/x-x509-ca-cert"}, + {"pem", "application/x-x509-ca-cert"}, + {"crt", "application/x-x509-ca-cert"}, + {"xpi", "application/x-xpinstall"}, + {"xhtml", "application/xhtml+xml"}, + {"xspf", "application/xspf+xml"}, + {"zip", "application/zip"}, + {"bin", "application/octet-stream"}, + {"exe", "application/octet-stream"}, + {"dll", "application/octet-stream"}, + {"deb", "application/octet-stream"}, + {"dmg", "application/octet-stream"}, + {"iso", "application/octet-stream"}, + {"img", "application/octet-stream"}, + {"msi", "application/octet-stream"}, + {"msp", "application/octet-stream"}, + {"msm", "application/octet-stream"}, + {"docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}, + {"xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, + {"pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"}, + {"mid", "audio/midi"}, + {"midi", "audio/midi"}, + {"kar", "audio/midi"}, + {"mp3", "audio/mpeg"}, + {"ogg", "audio/ogg"}, + {"m4a", "audio/x-m4a"}, + {"ra", "audio/x-realaudio"}, + {"3gpp", "video/3gpp"}, + {"3gp", "video/3gpp"}, + {"ts", "video/mp2t"}, + {"mp4", "video/mp4"}, + {"mpeg", "video/mpeg"}, + {"mpg", "video/mpeg"}, + {"mov", "video/quicktime"}, + {"webm", "video/webm"}, + {"flv", "video/x-flv"}, + {"m4v", "video/x-m4v"}, + {"mng", "video/x-mng"}, + {"asx", "video/x-ms-asf"}, + {"asf", "video/x-ms-asf"}, + {"wmv", "video/x-ms-wmv"}, + {"avi", "video/x-msvideo"}, +}; + +const string &HttpFileManager::getContentType(const char *name) { const char *dot; dot = strrchr(name, '.'); static StrCaseMap mapType; static onceToken token([&]() { - mapType.emplace(".html", "text/html"); - mapType.emplace(".htm", "text/html"); - mapType.emplace(".mp4", "video/mp4"); - mapType.emplace(".mkv", "video/x-matroska"); - mapType.emplace(".rmvb", "application/vnd.rn-realmedia"); - mapType.emplace(".rm", "application/vnd.rn-realmedia"); - mapType.emplace(".m3u8", "application/vnd.apple.mpegurl"); - mapType.emplace(".jpg", "image/jpeg"); - mapType.emplace(".jpeg", "image/jpeg"); - mapType.emplace(".gif", "image/gif"); - mapType.emplace(".png", "image/png"); - mapType.emplace(".ico", "image/x-icon"); - mapType.emplace(".css", "text/css"); - mapType.emplace(".js", "application/javascript"); - mapType.emplace(".au", "audio/basic"); - mapType.emplace(".wav", "audio/wav"); - mapType.emplace(".avi", "video/x-msvideo"); - mapType.emplace(".mov", "video/quicktime"); - mapType.emplace(".qt", "video/quicktime"); - mapType.emplace(".mpeg", "video/mpeg"); - mapType.emplace(".mpe", "video/mpeg"); - mapType.emplace(".vrml", "model/vrml"); - mapType.emplace(".wrl", "model/vrml"); - mapType.emplace(".midi", "audio/midi"); - mapType.emplace(".mid", "audio/midi"); - mapType.emplace(".mp3", "audio/mpeg"); - mapType.emplace(".ogg", "application/ogg"); - mapType.emplace(".pac", "application/x-ns-proxy-autoconfig"); - mapType.emplace(".flv", "video/x-flv"); + for (unsigned int i = 0; i < sizeof (s_mime_src) / sizeof (s_mime_src[0]); ++i) { + mapType.emplace(s_mime_src[i][0], s_mime_src[i][1]); + } }); static string defaultType = "text/plain"; if (!dot) { return defaultType; } - auto it = mapType.find(dot); + auto it = mapType.find(dot + 1); if (it == mapType.end()) { return defaultType; } @@ -416,7 +498,7 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo (*cookie)[kCookieName].get()._hls_data->addByteUsage(body->remainSize()); } } - cb(codeOut.data(), getContentType(strFile.data()), headerOut, body); + cb(codeOut.data(), HttpFileManager::getContentType(strFile.data()), headerOut, body); }; invoker.responseFile(parser.getValues(), httpHeader, strFile); }; diff --git a/src/Http/HttpFileManager.h b/src/Http/HttpFileManager.h index 4421431a..dc3a613e 100644 --- a/src/Http/HttpFileManager.h +++ b/src/Http/HttpFileManager.h @@ -69,6 +69,13 @@ public: * @param cb 回调对象 */ static void onAccessPath(TcpSession &sender, Parser &parser, const invoker &cb); + + /** + * 获取mime值 + * @param name 文件后缀 + * @return mime值 + */ + static const string &getContentType(const char *name); private: HttpFileManager() = delete; ~HttpFileManager() = delete;