From 28d20955e022087a5ba0d546e11cd736370263c5 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Thu, 16 May 2019 17:31:48 +0800 Subject: [PATCH] =?UTF-8?q?http=E6=9C=8D=E5=8A=A1=E5=99=A8=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=9F=A5=E6=89=BEindex.html=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpSession.cpp | 55 ++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 94b1ae99..59251ea6 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -266,6 +266,25 @@ inline bool HttpSession::checkLiveFlvStream(){ inline bool makeMeun(const string &httpPath,const string &strFullPath,const string &vhost, string &strRet) ; +inline static string findIndexFile(const string &dir){ + DIR *pDir; + dirent *pDirent; + if ((pDir = opendir(dir.data())) == NULL) { + return ""; + } + set setFile; + while ((pDirent = readdir(pDir)) != NULL) { + static set indexSet = {"index.html","index.htm","index"}; + if(indexSet.find(pDirent->d_name) != indexSet.end()){ + closedir(pDir); + return pDirent->d_name; + } + } + closedir(pDir); + return ""; +} + + inline bool HttpSession::Handle_Req_GET(int64_t &content_len) { //先看看是否为WebSocket请求 if(checkWebSocket()){ @@ -299,20 +318,30 @@ inline bool HttpSession::Handle_Req_GET(int64_t &content_len) { GET_CONFIG_AND_REGISTER(string,rootPath,Http::kRootPath); string strFile = enableVhost ? rootPath + "/" + _mediaInfo._vhost + _parser.Url() :rootPath + _parser.Url(); replace(strFile,"//","/"); - bool bClose = (strcasecmp(_parser["Connection"].data(),"close") == 0) || ( ++_iReqCnt > reqCnt); - //访问的是文件夹 - if (strFile.back() == '/' || File::is_dir(strFile.data())) { - //生成文件夹菜单索引 - string strMeun; - if (!makeMeun(_parser.Url(),strFile,_mediaInfo._vhost, strMeun)) { - //文件夹不存在 - sendNotFound(bClose); - return !bClose; - } - sendResponse("200 OK", makeHttpHeader(bClose,strMeun.size() ), strMeun); - return !bClose; - } + + do{ + //访问的是文件夹 + if (strFile.back() == '/' || File::is_dir(strFile.data())) { + auto indexFile = findIndexFile(strFile); + if(!indexFile.empty()){ + //发现该文件夹下有index文件 + strFile = strFile + "/" + indexFile; + replace(strFile,"//","/"); + break; + } + //生成文件夹菜单索引 + string strMeun; + if (!makeMeun(_parser.Url(),strFile,_mediaInfo._vhost, strMeun)) { + //文件夹不存在 + sendNotFound(bClose); + return !bClose; + } + sendResponse("200 OK", makeHttpHeader(bClose,strMeun.size() ), strMeun); + return !bClose; + } + }while(0); + //访问的是文件 struct stat tFileStat; if (0 != stat(strFile.data(), &tFileStat)) {