合并并完善http虚拟目录相关代码

This commit is contained in:
ziyue 2021-08-26 19:36:38 +08:00
parent 03e9c09c42
commit 848774271f
4 changed files with 40 additions and 30 deletions

View File

@ -158,6 +158,12 @@ sendBufSize=65536
sslport=443 sslport=443
#是否显示文件夹菜单,开启后可以浏览文件夹 #是否显示文件夹菜单,开启后可以浏览文件夹
dirMenu=1 dirMenu=1
#虚拟目录, 虚拟目录名和文件路径使用","隔开,多个配置路径间用";"隔开
#例如赋值为 app_a,/path/to/a;app_b,/path/to/b 那么
#访问 http://127.0.0.1/app_a/file_a 对应的文件路径为 /path/to/a/file_a
#访问 http://127.0.0.1/app_b/file_b 对应的文件路径为 /path/to/b/file_b
#访问其他http路径,对应的文件路径还是在rootPath内
kVirtualPath=
[multicast] [multicast]
#rtp组播截止组播ip地址 #rtp组播截止组播ip地址

View File

@ -124,6 +124,7 @@ onceToken token([](){
mINI::Instance()[kMaxReqSize] = 4 * 10240; mINI::Instance()[kMaxReqSize] = 4 * 10240;
mINI::Instance()[kKeepAliveSecond] = 15; mINI::Instance()[kKeepAliveSecond] = 15;
mINI::Instance()[kDirMenu] = true; mINI::Instance()[kDirMenu] = true;
mINI::Instance()[kVirtualPath] = "";
#if defined(_WIN32) #if defined(_WIN32)
mINI::Instance()[kCharSet] = "gb2312"; mINI::Instance()[kCharSet] = "gb2312";

View File

@ -183,7 +183,7 @@ extern const string kKeepAliveSecond;
extern const string kCharSet; extern const string kCharSet;
//http 服务器根目录 //http 服务器根目录
extern const string kRootPath; extern const string kRootPath;
//http 服务器虚拟目录 虚拟目录名和磁盘物理路径使用“,”隔开,多个配置路径间用 "|"隔开,例如 path1,d:/record|path2,e:/record //http 服务器虚拟目录 虚拟目录名和文件路径使用","隔开,多个配置路径间用";"隔开,例如 path_d,d:/record;path_e,e:/record
extern const string kVirtualPath; extern const string kVirtualPath;
//http 404错误提示内容 //http 404错误提示内容
extern const string kNotFound; extern const string kNotFound;

View File

@ -79,6 +79,7 @@ static bool makeFolderMenu(const string &httpPath, const string &strFullPath, st
return false; return false;
} }
string strPathPrefix(strFullPath); string strPathPrefix(strFullPath);
//url后缀有没有'/'访问文件夹,处理逻辑不一致
string last_dir_name; string last_dir_name;
if (strPathPrefix.back() == '/') { if (strPathPrefix.back() == '/') {
strPathPrefix.pop_back(); strPathPrefix.pop_back();
@ -90,12 +91,12 @@ static bool makeFolderMenu(const string &httpPath, const string &strFullPath, st
return false; return false;
} }
stringstream ss; stringstream ss;
ss << "<html>\r\n" ss << "<html>\r\n"
"<head>\r\n" "<head>\r\n"
"<title>文件索引</title>\r\n" "<title>文件索引</title>\r\n"
"</head>\r\n" "</head>\r\n"
"<body>\r\n" "<body>\r\n"
"<h1>文件索引:"; "<h1>文件索引:";
ss << httpPath; ss << httpPath;
ss << "</h1>\r\n"; ss << "</h1>\r\n";
@ -107,9 +108,9 @@ static bool makeFolderMenu(const string &httpPath, const string &strFullPath, st
ss << "</a></li>\r\n"; ss << "</a></li>\r\n";
ss << "<li><a href=\""; ss << "<li><a href=\"";
if(!last_dir_name.empty()){ if (!last_dir_name.empty()) {
ss << "./"; ss << "./";
}else{ } else {
ss << "../"; ss << "../";
} }
ss << "\">"; ss << "\">";
@ -122,7 +123,7 @@ static bool makeFolderMenu(const string &httpPath, const string &strFullPath, st
if ((pDir = opendir(strPathPrefix.data())) == NULL) { if ((pDir = opendir(strPathPrefix.data())) == NULL) {
return false; return false;
} }
set<string> setFile; multimap<string/*url name*/, std::pair<string/*note name*/, string/*file path*/> > file_map;
while ((pDirent = readdir(pDir)) != NULL) { while ((pDirent = readdir(pDir)) != NULL) {
if (File::is_special_dir(pDirent->d_name)) { if (File::is_special_dir(pDirent->d_name)) {
continue; continue;
@ -130,34 +131,35 @@ static bool makeFolderMenu(const string &httpPath, const string &strFullPath, st
if (pDirent->d_name[0] == '.') { if (pDirent->d_name[0] == '.') {
continue; continue;
} }
setFile.emplace(pDirent->d_name); file_map.emplace(pDirent->d_name, std::make_pair(pDirent->d_name, strPathPrefix + "/" + pDirent->d_name));
} }
//如果是root目录添加虚拟目录 //如果是root目录添加虚拟目录
if (httpPath == "/") { if (httpPath == "/") {
GET_CONFIG(string, virtualPath, Http::kVirtualPath); GET_CONFIG(string, virtualPath, Http::kVirtualPath);
mediakit::Parser pathParser; StrCaseMap args = Parser::parseArgs(virtualPath, ";", ",");
StrCaseMap args = pathParser.parseArgs(virtualPath, "|", ","); for (auto &pr : args) {
for (auto arg : args) { file_map.emplace(pr.first, std::make_pair(string("虚拟目录:") + pr.first, File::absolutePath("", pr.second)));
setFile.emplace(arg.first);
} }
} }
int i = 0; int i = 0;
for (auto &strFile :setFile) { for (auto &pr :file_map) {
string strAbsolutePath = strPathPrefix + "/" + strFile; auto &strAbsolutePath = pr.second.second;
bool isDir = File::is_dir(strAbsolutePath.data()); bool isDir = File::is_dir(strAbsolutePath.data());
ss << "<li><span>" << i++ << "</span>\t"; ss << "<li><span>" << i++ << "</span>\t";
ss << "<a href=\""; ss << "<a href=\"";
//路径链接地址
if (!last_dir_name.empty()) { if (!last_dir_name.empty()) {
ss << httpPath << "/" << strFile; ss << last_dir_name << "/" << pr.first;
} else { } else {
ss << strFile; ss << pr.first;
} }
if (isDir) { if (isDir) {
ss << "/"; ss << "/";
} }
ss << "\">"; ss << "\">";
ss << strFile; //路径名称
ss << pr.second.first;
if (isDir) { if (isDir) {
ss << "/</a></li>\r\n"; ss << "/</a></li>\r\n";
continue; continue;
@ -470,19 +472,20 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo
static string getFilePath(const Parser &parser,const MediaInfo &mediaInfo, TcpSession &sender){ static string getFilePath(const Parser &parser,const MediaInfo &mediaInfo, TcpSession &sender){
GET_CONFIG(bool, enableVhost, General::kEnableVhost); GET_CONFIG(bool, enableVhost, General::kEnableVhost);
GET_CONFIG(string, rootPath, Http::kRootPath);
GET_CONFIG(string, virtualPath, Http::kVirtualPath); GET_CONFIG(string, virtualPath, Http::kVirtualPath);
mediakit::Parser pathParser; StrCaseMap args = Parser::parseArgs(virtualPath, ";", ",");
StrCaseMap args = pathParser.parseArgs(virtualPath, "|", ",");
auto path = args[mediaInfo._app]; auto path = args[mediaInfo._app];
string ret; string url;
if (path == "") { if (path.empty()) {
ret = File::absolutePath( //访问的是根路径
enableVhost ? mediaInfo._vhost + parser.Url() : parser.Url(), rootPath); GET_CONFIG(string, rootPath, Http::kRootPath);
}else { path = rootPath;
ret = File::absolutePath( url = parser.Url();
enableVhost ? mediaInfo._vhost + "/" + mediaInfo._streamid : mediaInfo._streamid, path); } else {
//访问的是虚拟路径
url = parser.Url().substr(1 + mediaInfo._app.size());
} }
auto ret = File::absolutePath(enableVhost ? mediaInfo._vhost + url : url, path);
NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastHttpBeforeAccess, parser, ret, static_cast<SockInfo &>(sender)); NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastHttpBeforeAccess, parser, ret, static_cast<SockInfo &>(sender));
return ret; return ret;
} }