From 022838a7d30cb14047c10c6d06287275bd2b536a Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Tue, 13 Nov 2018 17:59:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dhttp=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpClient.cpp | 28 +++++++++++++++------------- src/Http/HttpClient.h | 10 ++++++---- src/Http/HttpDownloader.cpp | 3 ++- src/Http/HttpDownloader.h | 2 +- src/Http/HttpRequester.cpp | 3 ++- src/Http/HttpRequester.h | 2 +- src/Http/HttpSession.cpp | 2 +- 7 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/Http/HttpClient.cpp b/src/Http/HttpClient.cpp index 3b010f89..272cc405 100644 --- a/src/Http/HttpClient.cpp +++ b/src/Http/HttpClient.cpp @@ -153,11 +153,10 @@ int64_t HttpClient::onRecvHeader(const char *data, uint64_t len) { if(_parser["Content-Length"].empty()){ //没有Content-Length字段 -// if(_parser.Content().empty()){ -// //content长度为0,本次http请求结束 -// onResponseCompleted_l(); -// return 0; -// } + auto ret = onResponseCompleted_l(); + if(ret){ + return 0; + } //如果http回复未声明Content-Length字段,但是却有content内容,那说明可能是个不限长度的content _totalBodySize = INT64_MAX; _recvedBodySize = 0; @@ -228,26 +227,29 @@ void HttpClient::onManager() { } } -void HttpClient::onResponseCompleted_l() { +bool HttpClient::onResponseCompleted_l() { _totalBodySize = 0; _recvedBodySize = 0; - HttpRequestSplitter::reset(); - onResponseCompleted(); + bool ret = onResponseCompleted(); + if(ret){ + HttpRequestSplitter::reset(); + } + return ret; } -void HttpClient::checkCookie(HttpClient::HttpHeader &headers) { +void HttpClient::checkCookie(const HttpClient::HttpHeader &headers) { //Set-Cookie: IPTV_SERVER=8E03927B-CC8C-4389-BC00-31DBA7EC7B49;expires=Sun, Sep 23 2018 15:07:31 GMT;path=/index/api/ - auto set_cookie = headers["Set-Cookie"]; - if(set_cookie.empty()){ + auto it_set_cookie = headers.find("Set-Cookie"); + if(it_set_cookie == headers.end()){ return; } - auto key_val = Parser::parseArgs(set_cookie,";","="); + auto key_val = Parser::parseArgs(it_set_cookie->second,";","="); HttpCookie::Ptr cookie = std::make_shared(); cookie->setHost(_lastHost); int index = 0; - auto arg_vec = split(set_cookie, ";"); + auto arg_vec = split(it_set_cookie->second, ";"); for (string &key_val : arg_vec) { auto key = FindField(key_val.data(),NULL,"="); auto val = FindField(key_val.data(),"=", NULL); diff --git a/src/Http/HttpClient.h b/src/Http/HttpClient.h index 9df8730e..00f9c062 100644 --- a/src/Http/HttpClient.h +++ b/src/Http/HttpClient.h @@ -264,10 +264,12 @@ protected: }; /** - * 接收http回复完毕 + * 接收http回复完毕, + * @return 是否真的结束数据接收 */ - virtual void onResponseCompleted(){ + virtual bool onResponseCompleted(){ DebugL; + return true; } /** @@ -293,8 +295,8 @@ protected: virtual void onSend() override; virtual void onManager() override; private: - void onResponseCompleted_l(); - void checkCookie(HttpHeader &headers ); + bool onResponseCompleted_l(); + void checkCookie(const HttpHeader &headers ); protected: bool _isHttps; private: diff --git a/src/Http/HttpDownloader.cpp b/src/Http/HttpDownloader.cpp index b939a9d8..a8724e31 100644 --- a/src/Http/HttpDownloader.cpp +++ b/src/Http/HttpDownloader.cpp @@ -83,7 +83,7 @@ void HttpDownloader::onResponseBody(const char* buf, size_t size, size_t recvedS } } -void HttpDownloader::onResponseCompleted() { +bool HttpDownloader::onResponseCompleted() { closeFile(); //InfoL << "md5Sum:" << getMd5Sum(_filePath); _bDownloadSuccess = true; @@ -91,6 +91,7 @@ void HttpDownloader::onResponseCompleted() { _onResult(Err_success,"success",_filePath.data()); _onResult = nullptr; } + return true; } void HttpDownloader::onDisconnect(const SockException &ex) { diff --git a/src/Http/HttpDownloader.h b/src/Http/HttpDownloader.h index 2c694c8a..591c1504 100644 --- a/src/Http/HttpDownloader.h +++ b/src/Http/HttpDownloader.h @@ -49,7 +49,7 @@ public: private: void onResponseHeader(const string &status,const HttpHeader &headers) override; void onResponseBody(const char *buf,size_t size,size_t recvedSize,size_t totalSize) override; - void onResponseCompleted() override; + bool onResponseCompleted() override; void onDisconnect(const SockException &ex) override; void closeFile(); private: diff --git a/src/Http/HttpRequester.cpp b/src/Http/HttpRequester.cpp index 68e4fa77..8b0f3e85 100644 --- a/src/Http/HttpRequester.cpp +++ b/src/Http/HttpRequester.cpp @@ -42,11 +42,12 @@ void HttpRequester::onResponseBody(const char *buf,size_t size,size_t recvedSize _strRecvBody.append(buf,size); } -void HttpRequester::onResponseCompleted() { +bool HttpRequester::onResponseCompleted() { if(_onResult){ _onResult(SockException(),responseStatus(),responseHeader(),_strRecvBody); _onResult = nullptr; } + return true; } void HttpRequester::onDisconnect(const SockException &ex){ diff --git a/src/Http/HttpRequester.h b/src/Http/HttpRequester.h index 36032379..1ce3ca59 100644 --- a/src/Http/HttpRequester.h +++ b/src/Http/HttpRequester.h @@ -42,7 +42,7 @@ public: private: void onResponseHeader(const string &status,const HttpHeader &headers) override; void onResponseBody(const char *buf,size_t size,size_t recvedSize,size_t totalSize) override; - void onResponseCompleted() override; + bool onResponseCompleted() override; void onDisconnect(const SockException &ex) override; private: string _strRecvBody; diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 88de812a..fa253693 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -515,7 +515,7 @@ inline bool HttpSession::makeMeun(const string &strFullPath,const string &vhost, } inline void HttpSession::sendResponse(const char* pcStatus, const KeyValue& header, const string& strContent) { _StrPrinter printer; - printer << "HTTP/1.1 " << pcStatus << " \r\n"; + printer << "HTTP/1.1 " << pcStatus << "\r\n"; for (auto &pr : header) { printer << pr.first << ": " << pr.second << "\r\n"; }