完善http服务器对不固定长度body的支持

This commit is contained in:
xiongziliang 2019-12-24 10:25:28 +08:00
parent 40c2326950
commit eb89a3e504
3 changed files with 13 additions and 15 deletions

View File

@ -52,14 +52,14 @@ public:
virtual ~HttpBody(){} virtual ~HttpBody(){}
/** /**
* * >=INT64_MAX, content-length
*/ */
virtual uint64_t remainSize() { return 0;}; virtual uint64_t remainSize() { return 0;};
/** /**
* size * size
* @param size * @param size
* @return * @return ,nullptr
*/ */
virtual Buffer::Ptr readData(uint32_t size) { return nullptr;}; virtual Buffer::Ptr readData(uint32_t size) { return nullptr;};
}; };

View File

@ -156,7 +156,7 @@ bool HttpSession::checkWebSocket(){
auto res_cb = [this,headerOut](){ auto res_cb = [this,headerOut](){
_flv_over_websocket = true; _flv_over_websocket = true;
sendResponse("101 Switching Protocols",false,nullptr,headerOut,nullptr,false); sendResponse("101 Switching Protocols",false,nullptr,headerOut,nullptr, true);
}; };
//判断是否为websocket-flv //判断是否为websocket-flv
@ -219,7 +219,7 @@ bool HttpSession::checkLiveFlvStream(const function<void()> &cb){
if(!cb) { if(!cb) {
//找到rtmp源发送http头负载后续发送 //找到rtmp源发送http头负载后续发送
sendResponse("200 OK", false, "video/x-flv",KeyValue(),nullptr,false); sendResponse("200 OK", false, "video/x-flv",KeyValue(),nullptr,true);
}else{ }else{
cb(); cb();
} }
@ -313,7 +313,7 @@ void HttpSession::sendResponse(const char *pcStatus,
const char *pcContentType, const char *pcContentType,
const HttpSession::KeyValue &header, const HttpSession::KeyValue &header,
const HttpBody::Ptr &body, const HttpBody::Ptr &body,
bool set_content_len ){ bool is_http_flv ){
GET_CONFIG(string,charSet,Http::kCharSet); GET_CONFIG(string,charSet,Http::kCharSet);
GET_CONFIG(uint32_t,keepAliveSec,Http::kKeepAliveSecond); GET_CONFIG(uint32_t,keepAliveSec,Http::kKeepAliveSecond);
@ -322,16 +322,14 @@ void HttpSession::sendResponse(const char *pcStatus,
if (body && body->remainSize()) { if (body && body->remainSize()) {
//有body获取body大小 //有body获取body大小
size = body->remainSize(); size = body->remainSize();
if (size >= INT64_MAX) {
//不固定长度的body那么不设置content-length字段
size = -1;
}
} }
if(!set_content_len || size == -1){ if(is_http_flv){
//如果是不定长度body或者不设置conten-length, //http-flv直播是Keep-Alive类型
//那么一定是Keep-Alive类型
bClose = false; bClose = false;
}else if(size >= INT64_MAX){
//不固定长度的body那么发送完body后应该关闭socket以便浏览器做下载完毕的判断
bClose = true;
} }
HttpSession::KeyValue &headerOut = const_cast<HttpSession::KeyValue &>(header); HttpSession::KeyValue &headerOut = const_cast<HttpSession::KeyValue &>(header);
@ -348,8 +346,8 @@ void HttpSession::sendResponse(const char *pcStatus,
headerOut.emplace("Access-Control-Allow-Credentials", "true"); headerOut.emplace("Access-Control-Allow-Credentials", "true");
} }
if(set_content_len && size >= 0){ if(!is_http_flv && size >= 0 && size < INT64_MAX){
//文件长度为定值或者,且不是http-flv强制设置Content-Length //文件长度为定值,且不是http-flv强制设置Content-Length
headerOut["Content-Length"] = StrPrinter << size << endl; headerOut["Content-Length"] = StrPrinter << size << endl;
} }

View File

@ -115,7 +115,7 @@ private:
void sendNotFound(bool bClose); void sendNotFound(bool bClose);
void sendResponse(const char *pcStatus, bool bClose, const char *pcContentType = nullptr, void sendResponse(const char *pcStatus, bool bClose, const char *pcContentType = nullptr,
const HttpSession::KeyValue &header = HttpSession::KeyValue(), const HttpSession::KeyValue &header = HttpSession::KeyValue(),
const HttpBody::Ptr &body = nullptr,bool set_content_len = true); const HttpBody::Ptr &body = nullptr,bool is_http_flv = false);
//设置socket标志 //设置socket标志
void setSocketFlags(); void setSocketFlags();