修复http相关的bug

This commit is contained in:
xiongziliang 2018-11-13 17:59:12 +08:00
parent 6d8bdefa31
commit 022838a7d3
7 changed files with 28 additions and 22 deletions

View File

@ -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;
bool ret = onResponseCompleted();
if(ret){
HttpRequestSplitter::reset();
onResponseCompleted();
}
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<HttpCookie>();
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);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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