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 req=HttpAppFramework::instance().newHttpRequest();
req->setMethod(drogon::HttpRequest::kGet);
int count=0;
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;
//auto headers=response.
count++;
std::cout<<response.getBody()<<std::endl;
std::cout<<"count="<<count<<std::endl;
});
HttpAppFramework::instance().run();
} else {

View File

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

View File

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

View File

@ -219,6 +219,19 @@ namespace drogon
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:
static const std::string web_content_type_to_string(uint8_t contenttype);
static const std::string web_response_code_to_string(int code);