添加http服务器配置项:是否支持vhost

This commit is contained in:
xiongziliang 2019-05-16 17:09:29 +08:00
parent 36e8dcfac1
commit 4c39821691
5 changed files with 19 additions and 13 deletions

@ -1 +1 @@
Subproject commit b38a866916d166fba8a33275b9375f185fcf3671
Subproject commit 02c344efb0eba8bd4f1eec8fb5dce3db5bda284a

View File

@ -93,6 +93,10 @@ const char kKeepAliveSecond[] = HTTP_FIELD"keepAliveSecond";
#define HTTP_MAX_REQ_CNT 100
const char kMaxReqCount[] = HTTP_FIELD"maxReqCount";
//文件服务器是否启动虚拟主机
const char kEnableVhost[] = HTTP_FIELD"enableVhost";
//http 字符编码
#if defined(_WIN32)
#define HTTP_CHAR_SET "gb2312"
@ -126,6 +130,7 @@ onceToken token([](){
mINI::Instance()[kCharSet] = HTTP_CHAR_SET;
mINI::Instance()[kRootPath] = HTTP_ROOT_PATH;
mINI::Instance()[kNotFound] = HTTP_NOT_FOUND;
mINI::Instance()[kEnableVhost] = 1;
},nullptr);
}//namespace Http

View File

@ -165,6 +165,8 @@ extern const char kCharSet[];
extern const char kRootPath[];
//http 404错误提示内容
extern const char kNotFound[];
//文件服务器是否启动虚拟主机
extern const char kEnableVhost[];
}//namespace Http

View File

@ -105,9 +105,6 @@ HttpSession::HttpSession(const Socket::Ptr &pSock) : TcpSession(pSock) {
pSock->setSendTimeOutSecond(15);
//起始接收buffer缓存设置为4K节省内存
pSock->setReadBuffer(std::make_shared<BufferRaw>(4 * 1024));
GET_CONFIG_AND_REGISTER(string,rootPath,Http::kRootPath);
_strPath = rootPath;
}
HttpSession::~HttpSession() {
@ -266,6 +263,9 @@ inline bool HttpSession::checkLiveFlvStream(){
}
return true;
}
inline bool makeMeun(const string &httpPath,const string &strFullPath,const string &vhost, string &strRet) ;
inline bool HttpSession::Handle_Req_GET(int64_t &content_len) {
//先看看是否为WebSocket请求
if(checkWebSocket()){
@ -293,16 +293,19 @@ inline bool HttpSession::Handle_Req_GET(int64_t &content_len) {
auto fullUrl = string(HTTP_SCHEMA) + "://" + _parser["Host"] + _parser.FullUrl();
_mediaInfo.parse(fullUrl);
string strFile = _strPath + "/" + _mediaInfo._vhost + _parser.Url();
/////////////HTTP连接是否需要被关闭////////////////
GET_CONFIG_AND_REGISTER(uint32_t,reqCnt,Http::kMaxReqCount);
GET_CONFIG_AND_REGISTER(bool,enableVhost,Http::kEnableVhost);
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(strFile,_mediaInfo._vhost, strMeun)) {
if (!makeMeun(_parser.Url(),strFile,_mediaInfo._vhost, strMeun)) {
//文件夹不存在
sendNotFound(bClose);
return !bClose;
@ -432,7 +435,7 @@ inline bool HttpSession::Handle_Req_GET(int64_t &content_len) {
return true;
}
inline bool HttpSession::makeMeun(const string &strFullPath,const string &vhost, string &strRet) {
inline bool makeMeun(const string &httpPath,const string &strFullPath,const string &vhost, string &strRet) {
string strPathPrefix(strFullPath);
string last_dir_name;
if(strPathPrefix.back() == '/'){
@ -452,11 +455,9 @@ inline bool HttpSession::makeMeun(const string &strFullPath,const string &vhost,
"<body>\r\n"
"<h1>文件索引:";
string strPath = strFullPath;
strPath = strPath.substr(_strPath.length() + vhost.length() + 1);
ss << strPath;
ss << httpPath;
ss << "</h1>\r\n";
if (strPath != "/") {
if (httpPath != "/") {
ss << "<li><a href=\"";
ss << "/";
ss << "\">";

View File

@ -97,7 +97,6 @@ protected:
}
private:
Parser _parser;
string _strPath;
Ticker _ticker;
uint32_t _iReqCnt = 0;
//消耗的总流量
@ -113,7 +112,6 @@ private:
inline bool checkWebSocket();
inline bool emitHttpEvent(bool doInvoke);
inline void urlDecode(Parser &parser);
inline bool makeMeun(const string &strFullPath,const string &vhost, string &strRet);
inline void sendNotFound(bool bClose);
inline void sendResponse(const char *pcStatus,const KeyValue &header,const string &strContent);
inline static KeyValue makeHttpHeader(bool bClose=false,int64_t iContentSize=-1,const char *pcContentType="text/html");