Forbit to create http async clients on stack since they are derived from std::shared_from_this.

This commit is contained in:
Chunting Gu
2019-02-25 10:31:28 +08:00
parent 43eaf90621
commit d42556ce5a
10 changed files with 58 additions and 46 deletions
+15 -5
View File
@@ -5,15 +5,27 @@
namespace webcc {
class HttpAsyncClient;
typedef std::shared_ptr<HttpAsyncClient> HttpAsyncClientPtr;
// HTTP asynchronous client.
class HttpAsyncClient : public HttpAsyncClientBase {
public:
~HttpAsyncClient() = default;
// Forbid to create HttpAsyncClient in stack since it's derived from
// std::shared_from_this.
static HttpAsyncClientPtr New(boost::asio::io_context& io_context,
std::size_t buffer_size = 0) {
return HttpAsyncClientPtr{
new HttpAsyncClient(io_context, buffer_size)
};
}
private:
explicit HttpAsyncClient(boost::asio::io_context& io_context,
std::size_t buffer_size = 0);
~HttpAsyncClient() = default;
private:
void Resolve() final {
DoResolve(kHttpPort);
}
@@ -34,8 +46,6 @@ class HttpAsyncClient : public HttpAsyncClientBase {
tcp::socket socket_;
};
typedef std::shared_ptr<HttpAsyncClient> HttpAsyncClientPtr;
} // namespace webcc
#endif // WEBCC_HTTP_ASYNC_CLIENT_H_
+5 -5
View File
@@ -33,11 +33,11 @@ void HttpRequest::Prepare() {
}
// static
HttpRequestPtr HttpRequest::Make(const std::string& method,
const std::string& url,
const std::string& host,
const std::string& port,
bool prepare) {
HttpRequestPtr HttpRequest::New(const std::string& method,
const std::string& url,
const std::string& host,
const std::string& port,
bool prepare) {
HttpRequestPtr request{
new HttpRequest{ method, url, host, port }
};
+5 -5
View File
@@ -43,11 +43,11 @@ class HttpRequest : public HttpMessage {
// Compose start line, set Host header, etc.
void Prepare() override;
static HttpRequestPtr Make(const std::string& method,
const std::string& url,
const std::string& host,
const std::string& port = "",
bool prepare = true);
static HttpRequestPtr New(const std::string& method,
const std::string& url,
const std::string& host,
const std::string& port = "",
bool prepare = true);
private:
friend class HttpRequestParser;
+20 -9
View File
@@ -7,24 +7,37 @@
namespace webcc {
class HttpSslAsyncClient;
typedef std::shared_ptr<HttpSslAsyncClient> HttpSslAsyncClientPtr;
// HTTP SSL (a.k.a., HTTPS) asynchronous client.
class HttpSslAsyncClient : public HttpAsyncClientBase {
public:
// SSL verification (|ssl_verify|) needs CA certificates to be found
// in the default verify paths of OpenSSL. On Windows, it means you need to
// set environment variable SSL_CERT_FILE properly.
explicit HttpSslAsyncClient(boost::asio::io_context& io_context,
std::size_t buffer_size = 0,
bool ssl_verify = true);
~HttpSslAsyncClient() = default;
// Forbid to create HttpSslAsyncClient in stack since it's derived from
// std::shared_from_this.
static HttpSslAsyncClientPtr New(boost::asio::io_context& io_context,
std::size_t buffer_size = 0,
bool ssl_verify = true) {
return HttpSslAsyncClientPtr{
new HttpSslAsyncClient(io_context, buffer_size, ssl_verify)
};
}
// See https://stackoverflow.com/q/657155/6825348
std::shared_ptr<HttpSslAsyncClient> shared_from_this() {
return shared_from_base<HttpSslAsyncClient>();
}
private:
// SSL verification (|ssl_verify|) needs CA certificates to be found
// in the default verify paths of OpenSSL. On Windows, it means you need to
// set environment variable SSL_CERT_FILE properly.
explicit HttpSslAsyncClient(boost::asio::io_context& io_context,
std::size_t buffer_size = 0,
bool ssl_verify = true);
void Resolve() final;
// Override to do handshake after connected.
@@ -51,8 +64,6 @@ class HttpSslAsyncClient : public HttpAsyncClientBase {
bool ssl_verify_;
};
typedef std::shared_ptr<HttpSslAsyncClient> HttpSslAsyncClientPtr;
} // namespace webcc
#endif // WEBCC_HTTP_SSL_ASYNC_CLIENT_H_
+1 -3
View File
@@ -26,9 +26,7 @@ void RestAsyncClient::Request(const std::string& method,
http_request->Prepare();
HttpAsyncClientPtr http_async_client{
new HttpAsyncClient(io_context_, buffer_size_)
};
auto http_async_client = HttpAsyncClient::New(io_context_, buffer_size_);
if (timeout_seconds_ > 0) {
http_async_client->SetTimeout(timeout_seconds_);
+1 -3
View File
@@ -65,9 +65,7 @@ void SoapAsyncClient::Request(const std::string& operation,
http_request->SetHeader(kSoapAction, operation);
http_request->Prepare();
HttpAsyncClientPtr http_async_client{
new HttpAsyncClient(io_context_, buffer_size_)
};
auto http_async_client = HttpAsyncClient::New(io_context_, buffer_size_);
if (timeout_seconds_ > 0) {
http_async_client->SetTimeout(timeout_seconds_);