From eb89a3e5048bac4285787a13a36eddf4bab3bd30 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Tue, 24 Dec 2019 10:25:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84http=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E5=AF=B9=E4=B8=8D=E5=9B=BA=E5=AE=9A=E9=95=BF=E5=BA=A6?= =?UTF-8?q?body=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpBody.h | 4 ++-- src/Http/HttpSession.cpp | 22 ++++++++++------------ src/Http/HttpSession.h | 2 +- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/Http/HttpBody.h b/src/Http/HttpBody.h index d119cbc5..a340c1b0 100644 --- a/src/Http/HttpBody.h +++ b/src/Http/HttpBody.h @@ -52,14 +52,14 @@ public: virtual ~HttpBody(){} /** - * 剩余数据大小 + * 剩余数据大小,如果返回>=INT64_MAX, 那么就不设置content-length */ virtual uint64_t remainSize() { return 0;}; /** * 读取一定字节数,返回大小可能小于size * @param size 请求大小 - * @return 字节对象 + * @return 字节对象,如果读完了,那么请返回nullptr */ virtual Buffer::Ptr readData(uint32_t size) { return nullptr;}; }; diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 88fa99e0..d02ac142 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -156,7 +156,7 @@ bool HttpSession::checkWebSocket(){ auto res_cb = [this,headerOut](){ _flv_over_websocket = true; - sendResponse("101 Switching Protocols",false,nullptr,headerOut,nullptr,false); + sendResponse("101 Switching Protocols",false,nullptr,headerOut,nullptr, true); }; //判断是否为websocket-flv @@ -219,7 +219,7 @@ bool HttpSession::checkLiveFlvStream(const function &cb){ if(!cb) { //找到rtmp源,发送http头,负载后续发送 - sendResponse("200 OK", false, "video/x-flv",KeyValue(),nullptr,false); + sendResponse("200 OK", false, "video/x-flv",KeyValue(),nullptr,true); }else{ cb(); } @@ -313,7 +313,7 @@ void HttpSession::sendResponse(const char *pcStatus, const char *pcContentType, const HttpSession::KeyValue &header, const HttpBody::Ptr &body, - bool set_content_len ){ + bool is_http_flv ){ GET_CONFIG(string,charSet,Http::kCharSet); GET_CONFIG(uint32_t,keepAliveSec,Http::kKeepAliveSecond); @@ -322,16 +322,14 @@ void HttpSession::sendResponse(const char *pcStatus, if (body && body->remainSize()) { //有body,获取body大小 size = body->remainSize(); - if (size >= INT64_MAX) { - //不固定长度的body,那么不设置content-length字段 - size = -1; - } } - if(!set_content_len || size == -1){ - //如果是不定长度body,或者不设置conten-length, - //那么一定是Keep-Alive类型 + if(is_http_flv){ + //http-flv直播是Keep-Alive类型 bClose = false; + }else if(size >= INT64_MAX){ + //不固定长度的body,那么发送完body后应该关闭socket,以便浏览器做下载完毕的判断 + bClose = true; } HttpSession::KeyValue &headerOut = const_cast(header); @@ -348,8 +346,8 @@ void HttpSession::sendResponse(const char *pcStatus, headerOut.emplace("Access-Control-Allow-Credentials", "true"); } - if(set_content_len && size >= 0){ - //文件长度为定值或者,且不是http-flv强制设置Content-Length + if(!is_http_flv && size >= 0 && size < INT64_MAX){ + //文件长度为固定值,且不是http-flv强制设置Content-Length headerOut["Content-Length"] = StrPrinter << size << endl; } diff --git a/src/Http/HttpSession.h b/src/Http/HttpSession.h index 207b061b..ec128c52 100644 --- a/src/Http/HttpSession.h +++ b/src/Http/HttpSession.h @@ -115,7 +115,7 @@ private: void sendNotFound(bool bClose); void sendResponse(const char *pcStatus, bool bClose, const char *pcContentType = nullptr, 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标志 void setSocketFlags();