mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 19:00:01 +08:00
优化http目录索引相关代码
优先使用ZLToolKit提供的api
This commit is contained in:
parent
fd52470fc9
commit
527d9b9e64
@ -8,19 +8,15 @@
|
|||||||
* may be found in the AUTHORS file in the root of the source tree.
|
* may be found in the AUTHORS file in the root of the source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#if !defined(_WIN32)
|
|
||||||
#include <dirent.h>
|
|
||||||
#endif //!defined(_WIN32)
|
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include "HttpFileManager.h"
|
|
||||||
#include "Util/File.h"
|
#include "Util/File.h"
|
||||||
#include "HttpConst.h"
|
|
||||||
#include "HttpSession.h"
|
|
||||||
#include "Record/HlsMediaSource.h"
|
|
||||||
#include "Common/Parser.h"
|
#include "Common/Parser.h"
|
||||||
#include "Common/config.h"
|
#include "Common/config.h"
|
||||||
#include "Common/strCoding.h"
|
#include "Common/strCoding.h"
|
||||||
|
#include "Record/HlsMediaSource.h"
|
||||||
|
#include "HttpConst.h"
|
||||||
|
#include "HttpSession.h"
|
||||||
|
#include "HttpFileManager.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace toolkit;
|
using namespace toolkit;
|
||||||
@ -158,23 +154,29 @@ bool HttpFileManager::isIPAllowed(const std::string &ip) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static string searchIndexFile(const string &dir){
|
static std::string fileName(const string &dir, const string &path) {
|
||||||
DIR *pDir;
|
auto ret = path.substr(dir.size());
|
||||||
dirent *pDirent;
|
if (ret.front() == '/') {
|
||||||
if ((pDir = opendir(dir.data())) == NULL) {
|
ret.erase(0, 1);
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
set<string> setFile;
|
return ret;
|
||||||
while ((pDirent = readdir(pDir)) != NULL) {
|
}
|
||||||
static set<const char *, StrCaseCompare> indexSet = {"index.html", "index.htm"};
|
|
||||||
if (indexSet.find(pDirent->d_name) != indexSet.end()) {
|
static string searchIndexFile(const string &dir) {
|
||||||
string ret = pDirent->d_name;
|
std::string ret;
|
||||||
closedir(pDir);
|
static set<std::string, StrCaseCompare> indexSet = { "index.html", "index.htm" };
|
||||||
return ret;
|
File::scanDir(dir, [&](const string &path, bool is_dir) {
|
||||||
|
if (is_dir) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
auto name = fileName(dir, path);
|
||||||
closedir(pDir);
|
if (indexSet.find(name) == indexSet.end()) {
|
||||||
return "";
|
return true;
|
||||||
|
}
|
||||||
|
ret = std::move(name);
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool makeFolderMenu(const string &httpPath, const string &strFullPath, string &strRet) {
|
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 << "</a></li>\r\n";
|
ss << "</a></li>\r\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
DIR *pDir;
|
|
||||||
dirent *pDirent;
|
|
||||||
if ((pDir = opendir(strPathPrefix.data())) == NULL) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
multimap<string/*url name*/, std::pair<string/*note name*/, string/*file path*/> > file_map;
|
multimap<string/*url name*/, std::pair<string/*note name*/, string/*file path*/> > file_map;
|
||||||
while ((pDirent = readdir(pDir)) != NULL) {
|
File::scanDir(strPathPrefix, [&](const std::string &path, bool isDir) {
|
||||||
if (File::is_special_dir(pDirent->d_name)) {
|
auto name = fileName(strPathPrefix, path);
|
||||||
continue;
|
file_map.emplace(strCoding::UrlEncode(name), std::make_pair(name, path));
|
||||||
}
|
return true;
|
||||||
if (pDirent->d_name[0] == '.') {
|
});
|
||||||
continue;
|
|
||||||
}
|
|
||||||
file_map.emplace(strCoding::UrlEncode(pDirent->d_name), std::make_pair(pDirent->d_name, strPathPrefix + "/" + pDirent->d_name));
|
|
||||||
}
|
|
||||||
//如果是root目录,添加虚拟目录
|
//如果是root目录,添加虚拟目录
|
||||||
if (httpPath == "/") {
|
if (httpPath == "/") {
|
||||||
GET_CONFIG_FUNC(StrCaseMap, virtualPathMap, Http::kVirtualPath, [](const string &str) {
|
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 << "</a></li>\r\n";
|
ss << "</a></li>\r\n";
|
||||||
}
|
}
|
||||||
closedir(pDir);
|
|
||||||
ss << "<ul>\r\n";
|
ss << "<ul>\r\n";
|
||||||
ss << "</ul>\r\n</body></html>";
|
ss << "</ul>\r\n</body></html>";
|
||||||
ss.str().swap(strRet);
|
ss.str().swap(strRet);
|
||||||
|
Loading…
Reference in New Issue
Block a user