From faab85e2ca87bfa25b2fdbb1611344d41bafcbb6 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Thu, 3 Jan 2019 15:05:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84Http=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E5=A4=8D=E7=94=A8=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpClient.cpp | 3 ++- src/Http/HttpClient.h | 14 ++++++++++++-- src/Http/HttpClientImp.cpp | 33 ++++---------------------------- src/Http/HttpClientImp.h | 26 ++++++++++++++----------- src/Http/HttpRequestSplitter.cpp | 1 + src/Http/HttpRequester.cpp | 6 ++++++ src/Http/HttpRequester.h | 1 + 7 files changed, 41 insertions(+), 43 deletions(-) diff --git a/src/Http/HttpClient.cpp b/src/Http/HttpClient.cpp index 6c5d13b9..be501fe5 100644 --- a/src/Http/HttpClient.cpp +++ b/src/Http/HttpClient.cpp @@ -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; diff --git a/src/Http/HttpClient.h b/src/Http/HttpClient.h index 68e76169..991920cd 100644 --- a/src/Http/HttpClient.h +++ b/src/Http/HttpClient.h @@ -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; diff --git a/src/Http/HttpClientImp.cpp b/src/Http/HttpClientImp.cpp index 31b04617..d71b05cf 100644 --- a/src/Http/HttpClientImp.cpp +++ b/src/Http/HttpClientImp.cpp @@ -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 */ diff --git a/src/Http/HttpClientImp.h b/src/Http/HttpClientImp.h index 300f8049..5fa6721b 100644 --- a/src/Http/HttpClientImp.h +++ b/src/Http/HttpClientImp.h @@ -36,29 +36,33 @@ using namespace toolkit; namespace mediakit { +#if defined(ENABLE_OPENSSL) class HttpClientImp: public HttpClient { public: typedef std::shared_ptr 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 _sslBox; -#endif //ENABLE_OPENSSL }; +#else + +typedef HttpClient HttpClientImp; + +#endif // defined(ENABLE_OPENSSL) + } /* namespace mediakit */ #endif /* SRC_HTTP_HTTPCLIENTIMP_H_ */ diff --git a/src/Http/HttpRequestSplitter.cpp b/src/Http/HttpRequestSplitter.cpp index 59acb05b..d0dcb86b 100644 --- a/src/Http/HttpRequestSplitter.cpp +++ b/src/Http/HttpRequestSplitter.cpp @@ -126,6 +126,7 @@ void HttpRequestSplitter::setContentLen(int64_t content_len) { void HttpRequestSplitter::reset() { _content_len = 0; + _remain_data_size = 0; _remain_data.clear(); } diff --git a/src/Http/HttpRequester.cpp b/src/Http/HttpRequester.cpp index 62ea4d96..c99c3631 100644 --- a/src/Http/HttpRequester.cpp +++ b/src/Http/HttpRequester.cpp @@ -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 diff --git a/src/Http/HttpRequester.h b/src/Http/HttpRequester.h index 3b43d96b..0bd777bb 100644 --- a/src/Http/HttpRequester.h +++ b/src/Http/HttpRequester.h @@ -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;