mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 10:40:05 +08:00
精简HttpServer代码
This commit is contained in:
parent
fff53cf0e2
commit
52c7bc1d34
@ -18,7 +18,7 @@ using namespace toolkit;
|
|||||||
|
|
||||||
namespace mediakit{
|
namespace mediakit{
|
||||||
|
|
||||||
const char *getHttpStatusMessage(int status) {
|
const char *HttpConst::getHttpStatusMessage(int status) {
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case 100: return "Continue";
|
case 100: return "Continue";
|
||||||
case 101: return "Switching Protocol";
|
case 101: return "Switching Protocol";
|
||||||
@ -196,7 +196,7 @@ static const char *s_mime_src[][2] = {
|
|||||||
{"avi", "video/x-msvideo"},
|
{"avi", "video/x-msvideo"},
|
||||||
};
|
};
|
||||||
|
|
||||||
const string &getHttpContentType(const char *name) {
|
const string& HttpConst::getHttpContentType(const char *name) {
|
||||||
const char *dot;
|
const char *dot;
|
||||||
dot = strrchr(name, '.');
|
dot = strrchr(name, '.');
|
||||||
static StrCaseMap mapType;
|
static StrCaseMap mapType;
|
||||||
|
@ -15,19 +15,25 @@
|
|||||||
|
|
||||||
namespace mediakit{
|
namespace mediakit{
|
||||||
|
|
||||||
/**
|
class HttpConst {
|
||||||
* 根据http错误代码获取字符说明
|
public:
|
||||||
* @param status 譬如404
|
HttpConst() = delete;
|
||||||
* @return 错误代码字符说明,譬如Not Found
|
~HttpConst() = delete;
|
||||||
*/
|
|
||||||
const char *getHttpStatusMessage(int status);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据文件后缀返回http mime
|
* 根据http错误代码获取字符说明
|
||||||
* @param name 文件后缀,譬如html
|
* @param status 譬如404
|
||||||
* @return mime值,譬如text/html
|
* @return 错误代码字符说明,譬如Not Found
|
||||||
*/
|
*/
|
||||||
const std::string &getHttpContentType(const char *name);
|
static const char *getHttpStatusMessage(int status);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据文件后缀返回http mime
|
||||||
|
* @param name 文件后缀,譬如html
|
||||||
|
* @return mime值,譬如text/html
|
||||||
|
*/
|
||||||
|
static const std::string &getHttpContentType(const char *name);
|
||||||
|
};
|
||||||
|
|
||||||
}//mediakit
|
}//mediakit
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ struct HttpCookieAttachment {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const string &HttpFileManager::getContentType(const char *name) {
|
const string &HttpFileManager::getContentType(const char *name) {
|
||||||
return getHttpContentType(name);
|
return HttpConst::getHttpContentType(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static string searchIndexFile(const string &dir){
|
static string searchIndexFile(const string &dir){
|
||||||
|
@ -75,9 +75,6 @@ ssize_t HttpSession::onRecvHeader(const char *header, size_t len) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 跨域
|
|
||||||
_origin = _parser["Origin"];
|
|
||||||
|
|
||||||
//默认后面数据不是content而是header
|
//默认后面数据不是content而是header
|
||||||
ssize_t content_len = 0;
|
ssize_t content_len = 0;
|
||||||
(this->*(it->second))(content_len);
|
(this->*(it->second))(content_len);
|
||||||
@ -507,15 +504,6 @@ private:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
static const string kDate = "Date";
|
|
||||||
static const string kServer = "Server";
|
|
||||||
static const string kConnection = "Connection";
|
|
||||||
static const string kKeepAlive = "Keep-Alive";
|
|
||||||
static const string kContentType = "Content-Type";
|
|
||||||
static const string kContentLength = "Content-Length";
|
|
||||||
static const string kAccessControlAllowOrigin = "Access-Control-Allow-Origin";
|
|
||||||
static const string kAccessControlAllowCredentials = "Access-Control-Allow-Credentials";
|
|
||||||
|
|
||||||
void HttpSession::sendResponse(int code,
|
void HttpSession::sendResponse(int code,
|
||||||
bool bClose,
|
bool bClose,
|
||||||
const char *pcContentType,
|
const char *pcContentType,
|
||||||
@ -541,25 +529,19 @@ void HttpSession::sendResponse(int code,
|
|||||||
}
|
}
|
||||||
|
|
||||||
HttpSession::KeyValue &headerOut = const_cast<HttpSession::KeyValue &>(header);
|
HttpSession::KeyValue &headerOut = const_cast<HttpSession::KeyValue &>(header);
|
||||||
headerOut.emplace(kDate, dateStr());
|
headerOut.emplace("Date", dateStr());
|
||||||
headerOut.emplace(kServer, kServerName);
|
headerOut.emplace("Server", kServerName);
|
||||||
headerOut.emplace(kConnection, bClose ? "close" : "keep-alive");
|
headerOut.emplace("Connection", bClose ? "close" : "keep-alive");
|
||||||
if (!bClose) {
|
if (!bClose) {
|
||||||
string keepAliveString = "timeout=";
|
string keepAliveString = "timeout=";
|
||||||
keepAliveString += to_string(keepAliveSec);
|
keepAliveString += to_string(keepAliveSec);
|
||||||
keepAliveString += ", max=100";
|
keepAliveString += ", max=100";
|
||||||
headerOut.emplace(kKeepAlive, std::move(keepAliveString));
|
headerOut.emplace("Keep-Alive", std::move(keepAliveString));
|
||||||
}
|
|
||||||
|
|
||||||
if (!_origin.empty()) {
|
|
||||||
// 设置跨域
|
|
||||||
headerOut.emplace(kAccessControlAllowOrigin, _origin);
|
|
||||||
headerOut.emplace(kAccessControlAllowCredentials, "true");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!no_content_length && size >= 0 && (size_t)size < SIZE_MAX) {
|
if (!no_content_length && size >= 0 && (size_t)size < SIZE_MAX) {
|
||||||
// 文件长度为固定值,且不是http-flv强制设置Content-Length
|
// 文件长度为固定值,且不是http-flv强制设置Content-Length
|
||||||
headerOut[kContentLength] = to_string(size);
|
headerOut["Content-Length"] = to_string(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (size && !pcContentType) {
|
if (size && !pcContentType) {
|
||||||
@ -572,7 +554,7 @@ void HttpSession::sendResponse(int code,
|
|||||||
string strContentType = pcContentType;
|
string strContentType = pcContentType;
|
||||||
strContentType += "; charset=";
|
strContentType += "; charset=";
|
||||||
strContentType += charSet;
|
strContentType += charSet;
|
||||||
headerOut.emplace(kContentType, std::move(strContentType));
|
headerOut.emplace("Content-Type", std::move(strContentType));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送http头
|
// 发送http头
|
||||||
@ -581,7 +563,7 @@ void HttpSession::sendResponse(int code,
|
|||||||
str += "HTTP/1.1 ";
|
str += "HTTP/1.1 ";
|
||||||
str += to_string(code);
|
str += to_string(code);
|
||||||
str += ' ';
|
str += ' ';
|
||||||
str += getHttpStatusMessage(code);
|
str += HttpConst::getHttpStatusMessage(code);
|
||||||
str += "\r\n";
|
str += "\r\n";
|
||||||
for (auto &pr : header) {
|
for (auto &pr : header) {
|
||||||
str += pr.first;
|
str += pr.first;
|
||||||
|
@ -132,7 +132,6 @@ private:
|
|||||||
bool _live_over_websocket = false;
|
bool _live_over_websocket = false;
|
||||||
//消耗的总流量
|
//消耗的总流量
|
||||||
uint64_t _total_bytes_usage = 0;
|
uint64_t _total_bytes_usage = 0;
|
||||||
std::string _origin;
|
|
||||||
Parser _parser;
|
Parser _parser;
|
||||||
toolkit::Ticker _ticker;
|
toolkit::Ticker _ticker;
|
||||||
TSMediaSource::RingType::RingReader::Ptr _ts_reader;
|
TSMediaSource::RingType::RingReader::Ptr _ts_reader;
|
||||||
|
Loading…
Reference in New Issue
Block a user