Fix bug in HttpClient

This commit is contained in:
antao 2018-07-24 18:50:25 +08:00
parent 89764e864a
commit 278f7abae1
4 changed files with 22 additions and 3 deletions

View File

@ -19,11 +19,14 @@ int main()
auto client=HttpAppFramework::instance().newHttpClient(netaddr.toIp(),80); auto client=HttpAppFramework::instance().newHttpClient(netaddr.toIp(),80);
auto req=HttpAppFramework::instance().newHttpRequest(); auto req=HttpAppFramework::instance().newHttpRequest();
req->setMethod(drogon::HttpRequest::kGet); req->setMethod(drogon::HttpRequest::kGet);
int count=0;
for(int i=0;i<10;i++) for(int i=0;i<10;i++)
client->sendRequest(req,[](ReqResult result,const HttpResponse &response){ client->sendRequest(req,[&](ReqResult result,const HttpResponse &response){
std::cout<<"receive response!"<<std::endl; std::cout<<"receive response!"<<std::endl;
//auto headers=response. //auto headers=response.
count++;
std::cout<<response.getBody()<<std::endl; std::cout<<response.getBody()<<std::endl;
std::cout<<"count="<<count<<std::endl;
}); });
HttpAppFramework::instance().run(); HttpAppFramework::instance().run();
} else { } else {

View File

@ -109,7 +109,7 @@ void HttpClientImpl::onRecvMessage(const trantor::TcpConnectionPtr &connPtr,tran
auto cb=_reqAndCallbacks.front().second; auto cb=_reqAndCallbacks.front().second;
cb(ReqResult::Ok,resp); cb(ReqResult::Ok,resp);
_reqAndCallbacks.pop(); _reqAndCallbacks.pop();
context->reset();
LOG_TRACE<<"req buffer size="<<_reqAndCallbacks.size(); LOG_TRACE<<"req buffer size="<<_reqAndCallbacks.size();
if(!_reqAndCallbacks.empty()) if(!_reqAndCallbacks.empty())
{ {
@ -123,6 +123,6 @@ void HttpClientImpl::onRecvMessage(const trantor::TcpConnectionPtr &connPtr,tran
_tcpClient.reset(); _tcpClient.reset();
} }
} }
context->reset();
} }
} }

View File

@ -86,8 +86,11 @@ namespace drogon
void reset() void reset()
{ {
state_ = kExpectRequestLine; state_ = kExpectRequestLine;
res_state_ = HttpResponseParseState::kExpectResponseLine;
HttpRequestImpl dummy; HttpRequestImpl dummy;
request_.swap(dummy); request_.swap(dummy);
HttpResponseImpl dummy_res;
response_.swap(dummy_res);
} }
void resetRes() void resetRes()

View File

@ -219,6 +219,19 @@ namespace drogon
return _body; return _body;
} }
void swap(HttpResponseImpl &that)
{
_headers.swap(that._headers);
_cookies.swap(that._cookies);
std::swap(_statusCode,that._statusCode);
std::swap(_v,that._v);
_statusMessage.swap(that._statusMessage);
std::swap(_closeConnection,that._closeConnection);
_body.swap(that._body);
std::swap(_left_body_length,that._left_body_length);
std::swap(_current_chunk_length,that._current_chunk_length);
std::swap(_contentType,that._contentType);
}
protected: protected:
static const std::string web_content_type_to_string(uint8_t contenttype); static const std::string web_content_type_to_string(uint8_t contenttype);
static const std::string web_response_code_to_string(int code); static const std::string web_response_code_to_string(int code);