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