From 527d9b9e646469c8337838a3dc328f67dc95eb7d Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sat, 2 Dec 2023 20:56:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96http=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E7=B4=A2=E5=BC=95=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优先使用ZLToolKit提供的api --- src/Http/HttpFileManager.cpp | 68 ++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 38 deletions(-) diff --git a/src/Http/HttpFileManager.cpp b/src/Http/HttpFileManager.cpp index 9c95cef6..dfb499a0 100644 --- a/src/Http/HttpFileManager.cpp +++ b/src/Http/HttpFileManager.cpp @@ -8,19 +8,15 @@ * may be found in the AUTHORS file in the root of the source tree. */ -#include -#if !defined(_WIN32) -#include -#endif //!defined(_WIN32) #include -#include "HttpFileManager.h" #include "Util/File.h" -#include "HttpConst.h" -#include "HttpSession.h" -#include "Record/HlsMediaSource.h" #include "Common/Parser.h" #include "Common/config.h" #include "Common/strCoding.h" +#include "Record/HlsMediaSource.h" +#include "HttpConst.h" +#include "HttpSession.h" +#include "HttpFileManager.h" using namespace std; using namespace toolkit; @@ -158,23 +154,29 @@ bool HttpFileManager::isIPAllowed(const std::string &ip) { return false; } -static string searchIndexFile(const string &dir){ - DIR *pDir; - dirent *pDirent; - if ((pDir = opendir(dir.data())) == NULL) { - return ""; +static std::string fileName(const string &dir, const string &path) { + auto ret = path.substr(dir.size()); + if (ret.front() == '/') { + ret.erase(0, 1); } - set setFile; - while ((pDirent = readdir(pDir)) != NULL) { - static set indexSet = {"index.html", "index.htm"}; - if (indexSet.find(pDirent->d_name) != indexSet.end()) { - string ret = pDirent->d_name; - closedir(pDir); - return ret; + return ret; +} + +static string searchIndexFile(const string &dir) { + std::string ret; + static set indexSet = { "index.html", "index.htm" }; + File::scanDir(dir, [&](const string &path, bool is_dir) { + if (is_dir) { + return true; } - } - closedir(pDir); - return ""; + auto name = fileName(dir, path); + if (indexSet.find(name) == indexSet.end()) { + return true; + } + ret = std::move(name); + return false; + }); + return ret; } static bool makeFolderMenu(const string &httpPath, const string &strFullPath, string &strRet) { @@ -223,21 +225,12 @@ static bool makeFolderMenu(const string &httpPath, const string &strFullPath, st ss << "\r\n"; } - DIR *pDir; - dirent *pDirent; - if ((pDir = opendir(strPathPrefix.data())) == NULL) { - return false; - } multimap > file_map; - while ((pDirent = readdir(pDir)) != NULL) { - if (File::is_special_dir(pDirent->d_name)) { - continue; - } - if (pDirent->d_name[0] == '.') { - continue; - } - file_map.emplace(strCoding::UrlEncode(pDirent->d_name), std::make_pair(pDirent->d_name, strPathPrefix + "/" + pDirent->d_name)); - } + File::scanDir(strPathPrefix, [&](const std::string &path, bool isDir) { + auto name = fileName(strPathPrefix, path); + file_map.emplace(strCoding::UrlEncode(name), std::make_pair(name, path)); + return true; + }); //如果是root目录,添加虚拟目录 if (httpPath == "/") { GET_CONFIG_FUNC(StrCaseMap, virtualPathMap, Http::kVirtualPath, [](const string &str) { @@ -283,7 +276,6 @@ static bool makeFolderMenu(const string &httpPath, const string &strFullPath, st } ss << "\r\n"; } - closedir(pDir); ss << "
    \r\n"; ss << "
\r\n"; ss.str().swap(strRet);