diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index e03ee6d4..071f4e5f 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include "Common/config.h" #include "strCoding.h" @@ -296,9 +297,9 @@ inline bool HttpSession::Handle_Req_GET(int64_t &content_len) { bool bClose = (strcasecmp(_parser["Connection"].data(),"close") == 0) || ( ++_iReqCnt > reqCnt); //访问的是文件夹 - if (strFile.back() == '/') { + if (strFile.back() == '/' || File::is_dir(strFile.data())) { //生成文件夹菜单索引 - string strMeun; + string strMeun; if (!makeMeun(strFile,_mediaInfo._vhost, strMeun)) { //文件夹不存在 sendNotFound(bClose); @@ -431,11 +432,18 @@ inline bool HttpSession::Handle_Req_GET(int64_t &content_len) { inline bool HttpSession::makeMeun(const string &strFullPath,const string &vhost, string &strRet) { string strPathPrefix(strFullPath); - strPathPrefix = strPathPrefix.substr(0, strPathPrefix.length() - 1); + string last_dir_name; + if(strPathPrefix.back() == '/'){ + strPathPrefix.pop_back(); + }else{ + last_dir_name = split(strPathPrefix,"/").back(); + } + if (!File::is_dir(strPathPrefix.data())) { return false; } - strRet = "\r\n" + stringstream ss; + ss << "\r\n" "\r\n" "文件索引\r\n" "\r\n" @@ -444,20 +452,24 @@ inline bool HttpSession::makeMeun(const string &strFullPath,const string &vhost, string strPath = strFullPath; strPath = strPath.substr(_strPath.length() + vhost.length() + 1); - strRet += strPath; - strRet += "\r\n"; + ss << strPath; + ss << "\r\n"; if (strPath != "/") { - strRet += "
  • "; - strRet += "根目录"; - strRet += "
  • \r\n"; + ss << "
  • "; + ss << "根目录"; + ss << "
  • \r\n"; - strRet += "
  • "; - strRet += "上级目录"; - strRet += "
  • \r\n"; + ss << "
  • "; + ss << "上级目录"; + ss << "
  • \r\n"; } DIR *pDir; @@ -475,38 +487,47 @@ inline bool HttpSession::makeMeun(const string &strFullPath,const string &vhost, } setFile.emplace(pDirent->d_name); } + int i = 0; for(auto &strFile :setFile ){ string strAbsolutePath = strPathPrefix + "/" + strFile; - if (File::is_dir(strAbsolutePath.data())) { - strRet += "
  • "; - strRet += strFile; - strRet += "/
  • \r\n"; - } else { //是文件 - strRet += "
  • "; - strRet += strFile; - struct stat fileData; - if (0 == stat(strAbsolutePath.data(), &fileData)) { - auto &fileSize = fileData.st_size; - if (fileSize < 1024) { - strRet += StrPrinter << " (" << fileData.st_size << "B)" << endl; - } else if (fileSize < 1024 * 1024) { - strRet += StrPrinter << " (" << fileData.st_size / 1024 << "KB)" << endl; - } else if (fileSize < 1024 * 1024 * 1024) { - strRet += StrPrinter << " (" << fileData.st_size / 1024 / 1024 << "MB)" << endl; - } else { - strRet += StrPrinter << " (" << fileData.st_size / 1024 / 1024 / 1024 << "GB)" << endl; - } - } - strRet += "
  • \r\n"; + bool isDir = File::is_dir(strAbsolutePath.data()); + ss << "
  • " << i++ << "\t"; + ss << ""; + ss << strFile; + if (isDir) { + ss << "/
  • \r\n"; + continue; + } + //是文件 + struct stat fileData; + if (0 == stat(strAbsolutePath.data(), &fileData)) { + auto &fileSize = fileData.st_size; + if (fileSize < 1024) { + ss << " (" << fileData.st_size << "B)" << endl; + } else if (fileSize < 1024 * 1024) { + ss << fixed << setprecision(2) << " (" << fileData.st_size / 1024.0 << "KB)"; + } else if (fileSize < 1024 * 1024 * 1024) { + ss << fixed << setprecision(2) << " (" << fileData.st_size / 1024 / 1024.0 << "MB)"; + } else { + ss << fixed << setprecision(2) << " (" << fileData.st_size / 1024 / 1024 / 1024.0 << "GB)"; + } + } + ss << "\r\n"; } closedir(pDir); - strRet += "
      \r\n"; - strRet += "
    \r\n"; + ss << "
      \r\n"; + ss << "
    \r\n"; + ss.str().swap(strRet); return true; } inline void HttpSession::sendResponse(const char* pcStatus, const KeyValue& header, const string& strContent) {