Fix SSL client issue.
This commit is contained in:
+45
-65
@@ -10,21 +10,19 @@
|
|||||||
#include "boost/lambda/lambda.hpp"
|
#include "boost/lambda/lambda.hpp"
|
||||||
|
|
||||||
#include "webcc/logger.h"
|
#include "webcc/logger.h"
|
||||||
|
#include "webcc/utility.h"
|
||||||
|
|
||||||
using boost::asio::ip::tcp;
|
using boost::asio::ip::tcp;
|
||||||
namespace ssl = boost::asio::ssl;
|
namespace ssl = boost::asio::ssl;
|
||||||
|
|
||||||
namespace webcc {
|
namespace webcc {
|
||||||
|
|
||||||
extern void AdjustBufferSize(std::size_t content_length,
|
|
||||||
std::vector<char>* buffer);
|
|
||||||
|
|
||||||
HttpSslClient::HttpSslClient()
|
HttpSslClient::HttpSslClient()
|
||||||
: ssl_context_(ssl::context::sslv23),
|
: ssl_context_(ssl::context::sslv23),
|
||||||
ssl_socket_(io_context_, ssl_context_),
|
ssl_socket_(io_context_, ssl_context_),
|
||||||
buffer_(kBufferSize),
|
buffer_(kBufferSize),
|
||||||
deadline_(io_context_),
|
deadline_(io_context_),
|
||||||
timeout_seconds_(kMaxReceiveSeconds),
|
timeout_seconds_(kMaxReadSeconds),
|
||||||
stopped_(false),
|
stopped_(false),
|
||||||
timed_out_(false),
|
timed_out_(false),
|
||||||
error_(kNoError) {
|
error_(kNoError) {
|
||||||
@@ -32,16 +30,21 @@ HttpSslClient::HttpSslClient()
|
|||||||
ssl_context_.set_default_verify_paths();
|
ssl_context_.set_default_verify_paths();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HttpSslClient::SetTimeout(int seconds) {
|
||||||
|
if (seconds > 0) {
|
||||||
|
timeout_seconds_ = seconds;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool HttpSslClient::Request(const HttpRequest& request) {
|
bool HttpSslClient::Request(const HttpRequest& request) {
|
||||||
|
io_context_.restart();
|
||||||
|
|
||||||
response_.reset(new HttpResponse());
|
response_.reset(new HttpResponse());
|
||||||
response_parser_.reset(new HttpResponseParser(response_.get()));
|
response_parser_.reset(new HttpResponseParser(response_.get()));
|
||||||
|
|
||||||
stopped_ = false;
|
stopped_ = false;
|
||||||
timed_out_ = false;
|
timed_out_ = false;
|
||||||
|
error_ = kNoError;
|
||||||
// Start the persistent actor that checks for deadline expiry.
|
|
||||||
deadline_.expires_at(boost::posix_time::pos_infin);
|
|
||||||
CheckDeadline();
|
|
||||||
|
|
||||||
if ((error_ = Connect(request)) != kNoError) {
|
if ((error_ = Connect(request)) != kNoError) {
|
||||||
return false;
|
return false;
|
||||||
@@ -63,11 +66,9 @@ bool HttpSslClient::Request(const HttpRequest& request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Error HttpSslClient::Connect(const HttpRequest& request) {
|
Error HttpSslClient::Connect(const HttpRequest& request) {
|
||||||
using boost::asio::ip::tcp;
|
|
||||||
|
|
||||||
tcp::resolver resolver(io_context_);
|
tcp::resolver resolver(io_context_);
|
||||||
|
|
||||||
std::string port = request.port(kHttpsPort);
|
std::string port = request.port(kHttpSslPort);
|
||||||
|
|
||||||
boost::system::error_code ec;
|
boost::system::error_code ec;
|
||||||
auto endpoints = resolver.resolve(tcp::v4(), request.host(), port, ec);
|
auto endpoints = resolver.resolve(tcp::v4(), request.host(), port, ec);
|
||||||
@@ -80,22 +81,12 @@ Error HttpSslClient::Connect(const HttpRequest& request) {
|
|||||||
|
|
||||||
LOG_VERB("Connect to server...");
|
LOG_VERB("Connect to server...");
|
||||||
|
|
||||||
deadline_.expires_from_now(boost::posix_time::seconds(kMaxConnectSeconds));
|
// Use sync API directly since we don't need timeout control.
|
||||||
|
boost::asio::connect(ssl_socket_.lowest_layer(), endpoints, ec);
|
||||||
ec = boost::asio::error::would_block;
|
|
||||||
|
|
||||||
// ConnectHandler: void (boost::system::error_code, tcp::endpoint)
|
|
||||||
boost::asio::async_connect(ssl_socket_.lowest_layer(), endpoints,
|
|
||||||
boost::lambda::var(ec) = boost::lambda::_1);
|
|
||||||
|
|
||||||
// Block until the asynchronous operation has completed.
|
|
||||||
do {
|
|
||||||
io_context_.run_one();
|
|
||||||
} while (ec == boost::asio::error::would_block);
|
|
||||||
|
|
||||||
// Determine whether a connection was successfully established.
|
// Determine whether a connection was successfully established.
|
||||||
if (ec) {
|
if (ec) {
|
||||||
LOG_ERRO("Socket connect error: %s", ec.message().c_str());
|
LOG_ERRO("Socket connect error (%s).", ec.message().c_str());
|
||||||
Stop();
|
Stop();
|
||||||
return kEndpointConnectError;
|
return kEndpointConnectError;
|
||||||
}
|
}
|
||||||
@@ -115,22 +106,15 @@ Error HttpSslClient::Connect(const HttpRequest& request) {
|
|||||||
|
|
||||||
// NOTE: Don't check timeout. It doesn't make much sense.
|
// NOTE: Don't check timeout. It doesn't make much sense.
|
||||||
Error HttpSslClient::Handshake(const std::string& host) {
|
Error HttpSslClient::Handshake(const std::string& host) {
|
||||||
boost::system::error_code ec = boost::asio::error::would_block;
|
|
||||||
|
|
||||||
ssl_socket_.set_verify_mode(ssl::verify_peer);
|
ssl_socket_.set_verify_mode(ssl::verify_peer);
|
||||||
ssl_socket_.set_verify_callback(ssl::rfc2818_verification(host));
|
ssl_socket_.set_verify_callback(ssl::rfc2818_verification(host));
|
||||||
|
|
||||||
// HandshakeHandler: void (boost::system::error_code)
|
// Use sync API directly since we don't need timeout control.
|
||||||
ssl_socket_.async_handshake(ssl::stream_base::client,
|
boost::system::error_code ec;
|
||||||
boost::lambda::var(ec) = boost::lambda::_1);
|
ssl_socket_.handshake(ssl::stream_base::client, ec);
|
||||||
|
|
||||||
// Block until the asynchronous operation has completed.
|
|
||||||
do {
|
|
||||||
io_context_.run_one();
|
|
||||||
} while (ec == boost::asio::error::would_block);
|
|
||||||
|
|
||||||
if (ec) {
|
if (ec) {
|
||||||
LOG_ERRO("Handshake error: %s", ec.message().c_str());
|
LOG_ERRO("Handshake error (%s).", ec.message().c_str());
|
||||||
return kHandshakeError;
|
return kHandshakeError;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,37 +122,25 @@ Error HttpSslClient::Handshake(const std::string& host) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Error HttpSslClient::SendReqeust(const HttpRequest& request) {
|
Error HttpSslClient::SendReqeust(const HttpRequest& request) {
|
||||||
LOG_VERB("Send request (timeout: %ds)...", kMaxSendSeconds);
|
|
||||||
LOG_VERB("HTTP request:\n%s", request.Dump(4, "> ").c_str());
|
LOG_VERB("HTTP request:\n%s", request.Dump(4, "> ").c_str());
|
||||||
|
|
||||||
// NOTE:
|
// NOTE:
|
||||||
// It doesn't make much sense to set a timeout for socket write.
|
// It doesn't make much sense to set a timeout for socket write.
|
||||||
// I find that it's almost impossible to simulate a situation in the server
|
// I find that it's almost impossible to simulate a situation in the server
|
||||||
// side to test this timeout.
|
// side to test this timeout.
|
||||||
deadline_.expires_from_now(boost::posix_time::seconds(kMaxSendSeconds));
|
|
||||||
|
|
||||||
boost::system::error_code ec = boost::asio::error::would_block;
|
boost::system::error_code ec;
|
||||||
|
|
||||||
// WriteHandler: void (boost::system::error_code, std::size_t)
|
// Use sync API directly since we don't need timeout control.
|
||||||
boost::asio::async_write(ssl_socket_, request.ToBuffers(),
|
boost::asio::write(ssl_socket_, request.ToBuffers(), ec);
|
||||||
boost::lambda::var(ec) = boost::lambda::_1);
|
|
||||||
|
|
||||||
// Block until the asynchronous operation has completed.
|
|
||||||
do {
|
|
||||||
io_context_.run_one();
|
|
||||||
} while (ec == boost::asio::error::would_block);
|
|
||||||
|
|
||||||
if (ec) {
|
if (ec) {
|
||||||
LOG_ERRO("Socket write error: %s", ec.message().c_str());
|
LOG_ERRO("Socket write error (%s).", ec.message().c_str());
|
||||||
Stop();
|
Stop();
|
||||||
return kSocketWriteError;
|
return kSocketWriteError;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stopped_) {
|
LOG_INFO("Request sent.");
|
||||||
// |timed_out_| should be true in this case.
|
|
||||||
LOG_ERRO("Socket write timed out.");
|
|
||||||
return kSocketWriteError;
|
|
||||||
}
|
|
||||||
|
|
||||||
return kNoError;
|
return kNoError;
|
||||||
}
|
}
|
||||||
@@ -177,6 +149,7 @@ Error HttpSslClient::ReadResponse() {
|
|||||||
LOG_VERB("Read response (timeout: %ds)...", timeout_seconds_);
|
LOG_VERB("Read response (timeout: %ds)...", timeout_seconds_);
|
||||||
|
|
||||||
deadline_.expires_from_now(boost::posix_time::seconds(timeout_seconds_));
|
deadline_.expires_from_now(boost::posix_time::seconds(timeout_seconds_));
|
||||||
|
DoWaitDeadline();
|
||||||
|
|
||||||
Error error = kNoError;
|
Error error = kNoError;
|
||||||
DoReadResponse(&error);
|
DoReadResponse(&error);
|
||||||
@@ -200,14 +173,10 @@ void HttpSslClient::DoReadResponse(Error* error) {
|
|||||||
|
|
||||||
LOG_VERB("Socket async read handler.");
|
LOG_VERB("Socket async read handler.");
|
||||||
|
|
||||||
if (stopped_) {
|
if (ec || length == 0) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inner_ec || length == 0) {
|
|
||||||
Stop();
|
Stop();
|
||||||
*error = kSocketReadError;
|
*error = kSocketReadError;
|
||||||
LOG_ERRO("Socket read error.");
|
LOG_ERRO("Socket read error (%s).", ec.message().c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,7 +206,9 @@ void HttpSslClient::DoReadResponse(Error* error) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!stopped_) {
|
||||||
DoReadResponse(error);
|
DoReadResponse(error);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Block until the asynchronous operation has completed.
|
// Block until the asynchronous operation has completed.
|
||||||
@@ -246,12 +217,17 @@ void HttpSslClient::DoReadResponse(Error* error) {
|
|||||||
} while (ec == boost::asio::error::would_block);
|
} while (ec == boost::asio::error::would_block);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpSslClient::CheckDeadline() {
|
void HttpSslClient::DoWaitDeadline() {
|
||||||
|
deadline_.async_wait(std::bind(&HttpSslClient::OnDeadline, this,
|
||||||
|
std::placeholders::_1));
|
||||||
|
}
|
||||||
|
|
||||||
|
void HttpSslClient::OnDeadline(boost::system::error_code ec) {
|
||||||
if (stopped_) {
|
if (stopped_) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_VERB("Check deadline.");
|
LOG_VERB("OnDeadline.");
|
||||||
|
|
||||||
if (deadline_.expires_at() <=
|
if (deadline_.expires_at() <=
|
||||||
boost::asio::deadline_timer::traits_type::now()) {
|
boost::asio::deadline_timer::traits_type::now()) {
|
||||||
@@ -259,16 +235,20 @@ void HttpSslClient::CheckDeadline() {
|
|||||||
// The socket is closed so that any outstanding asynchronous operations
|
// The socket is closed so that any outstanding asynchronous operations
|
||||||
// are canceled.
|
// are canceled.
|
||||||
LOG_WARN("HTTP client timed out.");
|
LOG_WARN("HTTP client timed out.");
|
||||||
Stop();
|
|
||||||
timed_out_ = true;
|
timed_out_ = true;
|
||||||
|
Stop();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put the actor back to sleep.
|
// Put the actor back to sleep.
|
||||||
deadline_.async_wait(std::bind(&HttpSslClient::CheckDeadline, this));
|
DoWaitDeadline();
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpSslClient::Stop() {
|
void HttpSslClient::Stop() {
|
||||||
if (!stopped_) {
|
if (stopped_) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
stopped_ = true;
|
stopped_ = true;
|
||||||
|
|
||||||
LOG_INFO("Close socket...");
|
LOG_INFO("Close socket...");
|
||||||
@@ -276,11 +256,11 @@ void HttpSslClient::Stop() {
|
|||||||
boost::system::error_code ec;
|
boost::system::error_code ec;
|
||||||
ssl_socket_.lowest_layer().close(ec);
|
ssl_socket_.lowest_layer().close(ec);
|
||||||
if (ec) {
|
if (ec) {
|
||||||
LOG_ERRO("Failed to close socket.");
|
LOG_ERRO("Socket close error (%s).", ec.message().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOG_INFO("Cancel deadline timer...");
|
||||||
deadline_.cancel();
|
deadline_.cancel();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace webcc
|
} // namespace webcc
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "boost/asio/deadline_timer.hpp"
|
#include "boost/asio/deadline_timer.hpp"
|
||||||
@@ -23,12 +24,11 @@ class HttpSslClient {
|
|||||||
|
|
||||||
~HttpSslClient() = default;
|
~HttpSslClient() = default;
|
||||||
|
|
||||||
DELETE_COPY_AND_ASSIGN(HttpSslClient);
|
WEBCC_DELETE_COPY_ASSIGN(HttpSslClient);
|
||||||
|
|
||||||
void set_timeout_seconds(int timeout_seconds) {
|
// Set the timeout seconds for reading response.
|
||||||
assert(timeout_seconds > 0);
|
// The |seconds| is only effective when greater than 0.
|
||||||
timeout_seconds_ = timeout_seconds;
|
void SetTimeout(int seconds);
|
||||||
}
|
|
||||||
|
|
||||||
// Connect to server, send request, wait until response is received.
|
// Connect to server, send request, wait until response is received.
|
||||||
bool Request(const HttpRequest& request);
|
bool Request(const HttpRequest& request);
|
||||||
@@ -50,7 +50,8 @@ private:
|
|||||||
|
|
||||||
void DoReadResponse(Error* error);
|
void DoReadResponse(Error* error);
|
||||||
|
|
||||||
void CheckDeadline();
|
void DoWaitDeadline();
|
||||||
|
void OnDeadline(boost::system::error_code ec);
|
||||||
|
|
||||||
void Stop();
|
void Stop();
|
||||||
|
|
||||||
|
|||||||
@@ -190,12 +190,6 @@ UrlQuery::UrlQuery(const std::string& str) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
UrlQuery::UrlQuery(const std::map<std::string, std::string>& map) {
|
|
||||||
for (auto& pair : map) {
|
|
||||||
Add(pair.first, pair.second);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void UrlQuery::Add(std::string&& key, std::string&& value) {
|
void UrlQuery::Add(std::string&& key, std::string&& value) {
|
||||||
if (!Has(key)) {
|
if (!Has(key)) {
|
||||||
parameters_.push_back({ std::move(key), std::move(value) });
|
parameters_.push_back({ std::move(key), std::move(value) });
|
||||||
|
|||||||
@@ -7,7 +7,6 @@
|
|||||||
// Example:
|
// Example:
|
||||||
// /inventory-check.cgi?item=12731&color=blue&size=large
|
// /inventory-check.cgi?item=12731&color=blue&size=large
|
||||||
|
|
||||||
#include <map>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -27,9 +26,6 @@ class UrlQuery {
|
|||||||
// The query string should be key value pairs separated by '&'.
|
// The query string should be key value pairs separated by '&'.
|
||||||
explicit UrlQuery(const std::string& str);
|
explicit UrlQuery(const std::string& str);
|
||||||
|
|
||||||
// Construct from key-value pairs.
|
|
||||||
explicit UrlQuery(const std::map<std::string, std::string>& map);
|
|
||||||
|
|
||||||
void Add(const std::string& key, const std::string& value);
|
void Add(const std::string& key, const std::string& value);
|
||||||
|
|
||||||
void Add(std::string&& key, std::string&& value);
|
void Add(std::string&& key, std::string&& value);
|
||||||
|
|||||||
Reference in New Issue
Block a user