Commit faab85e2 by xiongziliang

完善Http客户端复用机制

parent bdaeada3
......@@ -60,7 +60,7 @@ void HttpClient::sendRequest(const string &strUrl, float fTimeOutSec) {
if (_path.empty()) {
_path = "/";
}
auto port = atoi(FindField(host.data(), ":", NULL).data());
uint16_t port = atoi(FindField(host.data(), ":", NULL).data());
if (port <= 0) {
//默认端口
port = defaultPort;
......@@ -99,6 +99,7 @@ void HttpClient::sendRequest(const string &strUrl, float fTimeOutSec) {
if (!alive() || bChanged) {
//InfoL << "reconnet:" << _lastHost;
onBeforeConnect(host, port , fTimeOutSec);
startConnect(host, port, fTimeOutSec);
} else {
SockException ex;
......
......@@ -207,13 +207,21 @@ public:
HttpClient();
virtual ~HttpClient();
virtual void sendRequest(const string &url,float fTimeOutSec);
void clear(){
virtual void clear(){
_header.clear();
_body.reset();
_method.clear();
_path.clear();
_parser.Clear();
_recvedBodySize = 0;
_totalBodySize = 0;
_aliveTicker.resetTime();
_fTimeOutSec = 0;
_chunkedSplitter.reset();
HttpRequestSplitter::reset();
}
void setMethod(const string &method){
_method = method;
}
......@@ -291,6 +299,9 @@ protected:
//HttpRequestSplitter override
int64_t onRecvHeader(const char *data,uint64_t len) override ;
void onRecvContent(const char *data,uint64_t len) override;
//在连接服务器前调用一次
virtual void onBeforeConnect(string &strUrl, uint16_t &iPort,float &fTimeOutSec) {};
protected:
virtual void onConnect(const SockException &ex) override;
virtual void onRecv(const Buffer::Ptr &pBuf) override;
......@@ -303,7 +314,6 @@ private:
protected:
bool _isHttps;
private:
//send
HttpHeader _header;
HttpBody::Ptr _body;
string _method;
......
......@@ -28,47 +28,21 @@
namespace mediakit {
HttpClientImp::HttpClientImp() {
// TODO Auto-generated constructor stub
}
HttpClientImp::~HttpClientImp() {
}
void HttpClientImp::sendRequest(const string& url,float fTimeOutSec) {
HttpClient::sendRequest(url,fTimeOutSec);
#if defined(ENABLE_OPENSSL)
void HttpClientImp::onBeforeConnect(string &strUrl, uint16_t &iPort,float &fTimeOutSec) {
if(_isHttps){
#ifndef ENABLE_OPENSSL
shutdown();
throw std::invalid_argument("不支持HTTPS协议");
#else
_sslBox.reset(new SSL_Box(false));
_sslBox->setOnDecData([this](const char *data, uint32_t len){
#if defined(__GNUC__) && (__GNUC__ < 5)
public_onRecvBytes(data,len);
#else//defined(__GNUC__) && (__GNUC__ < 5)
HttpClient::onRecvBytes(data,len);
#endif//defined(__GNUC__) && (__GNUC__ < 5)
});
_sslBox->setOnEncData([this](const char *data, uint32_t len){
#if defined(__GNUC__) && (__GNUC__ < 5)
public_send(data,len);
#else//defined(__GNUC__) && (__GNUC__ < 5)
HttpClient::send(obtainBuffer(data,len));
#endif//defined(__GNUC__) && (__GNUC__ < 5)
});
#endif //ENABLE_OPENSSL
}else{
#ifdef ENABLE_OPENSSL
_sslBox.reset();
#endif //ENABLE_OPENSSL
}
}
#ifdef ENABLE_OPENSSL
void HttpClientImp::onRecvBytes(const char* data, int size) {
if(_sslBox){
_sslBox->onRecv(data,size);
......@@ -84,6 +58,7 @@ int HttpClientImp::send(const Buffer::Ptr &buf) {
}
return HttpClient::send(buf);
}
#endif //ENABLE_OPENSSL
#endif //defined(ENABLE_OPENSSL)
} /* namespace mediakit */
......@@ -36,29 +36,33 @@ using namespace toolkit;
namespace mediakit {
#if defined(ENABLE_OPENSSL)
class HttpClientImp: public HttpClient {
public:
typedef std::shared_ptr<HttpClientImp> Ptr;
HttpClientImp();
virtual ~HttpClientImp();
virtual void sendRequest(const string &url,float fTimeOutSec) override;
HttpClientImp() {}
virtual ~HttpClientImp() {}
#if defined(__GNUC__) && (__GNUC__ < 5)
void public_onRecvBytes(const char *data,int len){
inline void public_onRecvBytes(const char *data,int len){
HttpClient::onRecvBytes(data,len);
}
void public_send(const char *data, uint32_t len){
inline void public_send(const char *data, uint32_t len){
HttpClient::send(obtainBuffer(data,len));
}
#endif //defined(__GNUC__) && (__GNUC__ < 5)
private:
#ifdef ENABLE_OPENSSL
virtual void onRecvBytes(const char *data,int size) override;
virtual int send(const Buffer::Ptr &buf) override;
void onRecvBytes(const char *data,int size) override;
int send(const Buffer::Ptr &buf) override;
void onBeforeConnect(string &strUrl, uint16_t &iPort,float &fTimeOutSec) override;
private:
std::shared_ptr<SSL_Box> _sslBox;
#endif //ENABLE_OPENSSL
};
#else
typedef HttpClient HttpClientImp;
#endif // defined(ENABLE_OPENSSL)
} /* namespace mediakit */
#endif /* SRC_HTTP_HTTPCLIENTIMP_H_ */
......@@ -126,6 +126,7 @@ void HttpRequestSplitter::setContentLen(int64_t content_len) {
void HttpRequestSplitter::reset() {
_content_len = 0;
_remain_data_size = 0;
_remain_data.clear();
}
......
......@@ -63,5 +63,11 @@ void HttpRequester::startRequester(const string &url,const HttpRequesterResult &
sendRequest(url,timeOutSecond);
}
void HttpRequester::clear() {
HttpClientImp::clear();
_strRecvBody.clear();
_onResult = nullptr;
}
}//namespace mediakit
......@@ -39,6 +39,7 @@ public:
HttpRequester();
virtual ~HttpRequester();
void startRequester(const string &url,const HttpRequesterResult &onResult,float timeOutSecond = 10);
void clear() override ;
private:
int64_t onResponseHeader(const string &status,const HttpHeader &headers) override;
void onResponseBody(const char *buf,size_t size,size_t recvedSize,size_t totalSize) override;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论