精简HttpServer代码

This commit is contained in:
xia-chu 2023-06-10 10:16:45 +08:00 committed by 夏楚
parent fff53cf0e2
commit 52c7bc1d34
5 changed files with 28 additions and 41 deletions

View File

@ -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;

View File

@ -15,19 +15,25 @@
namespace mediakit{
class HttpConst {
public:
HttpConst() = delete;
~HttpConst() = delete;
/**
* http错误代码获取字符说明
* @param status 404
* @return Not Found
*/
const char *getHttpStatusMessage(int status);
static const char *getHttpStatusMessage(int status);
/**
* http mime
* @param name html
* @return mime值text/html
*/
const std::string &getHttpContentType(const char *name);
static const std::string &getHttpContentType(const char *name);
};
}//mediakit

View File

@ -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){

View File

@ -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;

View File

@ -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;