make Universal PIC.
This commit is contained in:
parent
edeb230c46
commit
e05d90f1b3
|
@ -96,7 +96,14 @@ std::vector<ParseItem> parseFormData(const std::string_view &buffer) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
Client::Client(boost::asio::io_context &ioContext, Type type, Version version) : m_ioContext(ioContext) {
|
class ClientPrivate {
|
||||||
|
public:
|
||||||
|
std::unique_ptr<boost::beast::ssl_stream<boost::beast::tcp_stream>> sslStream;
|
||||||
|
std::unique_ptr<boost::beast::tcp_stream> tcpStream;
|
||||||
|
};
|
||||||
|
|
||||||
|
Client::Client(boost::asio::io_context &ioContext, Type type, Version version)
|
||||||
|
: m_d(new ClientPrivate()), m_ioContext(ioContext) {
|
||||||
m_request.version(static_cast<unsigned>(version));
|
m_request.version(static_cast<unsigned>(version));
|
||||||
using namespace boost::asio;
|
using namespace boost::asio;
|
||||||
using namespace boost::beast;
|
using namespace boost::beast;
|
||||||
|
@ -107,6 +114,15 @@ Client::Client(boost::asio::io_context &ioContext, Type type, Version version) :
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Client::~Client() {
|
||||||
|
boost::system::error_code error;
|
||||||
|
shutdown(error);
|
||||||
|
if (error) {
|
||||||
|
LOG(error) << error.message();
|
||||||
|
}
|
||||||
|
if (m_d != nullptr) delete m_d;
|
||||||
|
}
|
||||||
|
|
||||||
void Client::loadRootCertificates(boost::system::error_code &error) {
|
void Client::loadRootCertificates(boost::system::error_code &error) {
|
||||||
if (m_sslContext) {
|
if (m_sslContext) {
|
||||||
load_root_certificates(*m_sslContext, error);
|
load_root_certificates(*m_sslContext, error);
|
||||||
|
@ -153,57 +169,81 @@ void Client::addRequestField(boost::beast::http::field name, const std::string &
|
||||||
|
|
||||||
std::string Client::execute(const std::string &host, const std::string &port, const Request &request,
|
std::string Client::execute(const std::string &host, const std::string &port, const Request &request,
|
||||||
boost::system::error_code &error) {
|
boost::system::error_code &error) {
|
||||||
using namespace boost::asio::ip;
|
|
||||||
using namespace boost::beast;
|
using namespace boost::beast;
|
||||||
|
|
||||||
tcp::resolver resolver(m_ioContext);
|
if ((m_host != host) || (m_port != port) || (!m_d->sslStream && !m_d->tcpStream)) {
|
||||||
auto const results = resolver.resolve(host, port, error);
|
if (m_d->sslStream || m_d->tcpStream) {
|
||||||
if (error) {
|
boost::system::error_code errorCode;
|
||||||
return std::string();
|
shutdown(errorCode);
|
||||||
}
|
if (errorCode) {
|
||||||
|
LOG(error) << errorCode.message();
|
||||||
std::unique_ptr<ssl_stream<tcp_stream>> sslStream;
|
}
|
||||||
std::unique_ptr<tcp_stream> tcpStream;
|
|
||||||
if (m_sslContext) {
|
|
||||||
if (m_certificateLocation == CertificateLocation::None) {
|
|
||||||
m_sslContext->set_default_verify_paths(error);
|
|
||||||
if (!error) m_certificateLocation = CertificateLocation::SystemDependent;
|
|
||||||
}
|
}
|
||||||
sslStream = std::make_unique<ssl_stream<tcp_stream>>(m_ioContext, *m_sslContext);
|
connect(host, port, error);
|
||||||
get_lowest_layer(*sslStream).connect(results, error);
|
|
||||||
if (error) {
|
if (error) {
|
||||||
return std::string();
|
return std::string();
|
||||||
}
|
}
|
||||||
sslStream->handshake(ssl::stream_base::client, error);
|
|
||||||
} else {
|
|
||||||
tcpStream = std::make_unique<tcp_stream>(m_ioContext);
|
|
||||||
tcpStream->connect(results, error);
|
|
||||||
}
|
|
||||||
if (error) {
|
|
||||||
return std::string();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sslStream) {
|
if (m_d->sslStream) {
|
||||||
http::write(*sslStream, request, error);
|
http::write(*m_d->sslStream, request, error);
|
||||||
} else if (tcpStream) {
|
} else if (m_d->tcpStream) {
|
||||||
http::write(*tcpStream, request, error);
|
http::write(*m_d->tcpStream, request, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
http::response<http::dynamic_body> response;
|
http::response<http::dynamic_body> response;
|
||||||
if (!error) {
|
if (!error) {
|
||||||
flat_buffer buffer;
|
flat_buffer buffer;
|
||||||
if (sslStream) {
|
if (m_d->sslStream) {
|
||||||
http::read(*sslStream, buffer, response, error);
|
http::read(*m_d->sslStream, buffer, response, error);
|
||||||
error_code ec;
|
} else if (m_d->tcpStream) {
|
||||||
sslStream->shutdown(ec);
|
http::read(*m_d->tcpStream, buffer, response, error);
|
||||||
} else if (tcpStream) {
|
|
||||||
http::read(*tcpStream, buffer, response, error);
|
|
||||||
error_code ec;
|
|
||||||
tcpStream->socket().shutdown(tcp::socket::shutdown_both, ec);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return boost::beast::buffers_to_string(response.body().data());
|
return boost::beast::buffers_to_string(response.body().data());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Client::connect(const std::string &host, const std::string &port, boost::system::error_code &error) {
|
||||||
|
using namespace boost::asio::ip;
|
||||||
|
using namespace boost::beast;
|
||||||
|
tcp::resolver resolver(m_ioContext);
|
||||||
|
auto const results = resolver.resolve(host, port, error);
|
||||||
|
if (error) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_sslContext) {
|
||||||
|
if (m_certificateLocation == CertificateLocation::None) {
|
||||||
|
m_sslContext->set_default_verify_paths(error);
|
||||||
|
if (!error) m_certificateLocation = CertificateLocation::SystemDependent;
|
||||||
|
}
|
||||||
|
m_d->sslStream = std::make_unique<ssl_stream<tcp_stream>>(m_ioContext, *m_sslContext);
|
||||||
|
get_lowest_layer(*m_d->sslStream).connect(results, error);
|
||||||
|
if (error) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
m_d->sslStream->handshake(ssl::stream_base::client, error);
|
||||||
|
} else {
|
||||||
|
m_d->tcpStream = std::make_unique<tcp_stream>(m_ioContext);
|
||||||
|
m_d->tcpStream->connect(results, error);
|
||||||
|
}
|
||||||
|
if (error) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
m_host = host;
|
||||||
|
m_port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::shutdown(boost::system::error_code &error) {
|
||||||
|
using namespace boost::asio::ip;
|
||||||
|
if (m_d->sslStream) {
|
||||||
|
m_d->sslStream->shutdown(error);
|
||||||
|
m_d->sslStream.reset();
|
||||||
|
} else if (m_d->tcpStream) {
|
||||||
|
m_d->tcpStream->socket().shutdown(tcp::socket::shutdown_both, error);
|
||||||
|
m_d->tcpStream.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Http
|
} // namespace Http
|
||||||
|
|
|
@ -52,47 +52,54 @@ struct ParseItem {
|
||||||
size_t end;
|
size_t end;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief
|
* @brief
|
||||||
*
|
*
|
||||||
* @param buffer
|
* @param buffer
|
||||||
* @return std::vector<ParseItem>
|
* @return std::vector<ParseItem>
|
||||||
* @example auto items = parseFormData(request.body());
|
* @example auto items = parseFormData(request.body());
|
||||||
* for (auto &item : items) {
|
* for (auto &item : items) {
|
||||||
* std::string filename("E:/");
|
* std::string filename("E:/");
|
||||||
* filename += item.filename;
|
* filename += item.filename;
|
||||||
* std::ofstream ofs(filename.c_str(), std::ios::binary);
|
* std::ofstream ofs(filename.c_str(), std::ios::binary);
|
||||||
* ofs.write(buffer.data() + item.begin, item.end - item.begin);
|
* ofs.write(buffer.data() + item.begin, item.end - item.begin);
|
||||||
* ofs.close();
|
* ofs.close();
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
static std::vector<ParseItem> parseFormData(const std::string_view &buffer);
|
static std::vector<ParseItem> parseFormData(const std::string_view &buffer);
|
||||||
|
|
||||||
class Client {
|
class ClientPrivate;
|
||||||
public:
|
class Client {
|
||||||
Client(boost::asio::io_context &ioContext, Type type, Version version = Version_1_1);
|
public:
|
||||||
void loadRootCertificates(boost::system::error_code &error);
|
Client(boost::asio::io_context &ioContext, Type type, Version version = Version_1_1);
|
||||||
std::string get(const std::string &host, const std::string &port, const std::string &url,
|
~Client();
|
||||||
|
void loadRootCertificates(boost::system::error_code &error);
|
||||||
|
std::string get(const std::string &host, const std::string &port, const std::string &url,
|
||||||
|
boost::system::error_code &error);
|
||||||
|
std::string post(const std::string &host, const std::string &port, const std::string &url, const std::string &body,
|
||||||
|
boost::system::error_code &error);
|
||||||
|
std::string put(const std::string &host, const std::string &port, const std::string &url, const std::string &body,
|
||||||
|
boost::system::error_code &error);
|
||||||
|
|
||||||
|
void addRequestField(boost::beast::http::field name, const std::string &value);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::string execute(const std::string &host, const std::string &port, const Request &request,
|
||||||
boost::system::error_code &error);
|
boost::system::error_code &error);
|
||||||
std::string post(const std::string &host, const std::string &port, const std::string &url,
|
void connect(const std::string &host, const std::string &port, boost::system::error_code &error);
|
||||||
const std::string &body, boost::system::error_code &error);
|
void shutdown(boost::system::error_code &error);
|
||||||
std::string put(const std::string &host, const std::string &port, const std::string &url,
|
|
||||||
const std::string &body, boost::system::error_code &error);
|
|
||||||
|
|
||||||
void addRequestField(boost::beast::http::field name, const std::string &value);
|
private:
|
||||||
|
ClientPrivate *m_d = nullptr;
|
||||||
|
boost::asio::io_context &m_ioContext;
|
||||||
|
std::shared_ptr<boost::asio::ssl::context> m_sslContext;
|
||||||
|
CertificateLocation m_certificateLocation = CertificateLocation::None;
|
||||||
|
std::string m_host;
|
||||||
|
std::string m_port;
|
||||||
|
Request m_request;
|
||||||
|
};
|
||||||
|
|
||||||
protected:
|
} // namespace Http
|
||||||
std::string execute(const std::string &host, const std::string &port, const Request &request,
|
|
||||||
boost::system::error_code &error);
|
|
||||||
|
|
||||||
private:
|
|
||||||
boost::asio::io_context &m_ioContext;
|
|
||||||
std::shared_ptr<boost::asio::ssl::context> m_sslContext;
|
|
||||||
CertificateLocation m_certificateLocation = CertificateLocation::None;
|
|
||||||
Request m_request;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Http
|
|
||||||
|
|
||||||
class Https {
|
class Https {
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -15,6 +15,10 @@ add_library(Universal
|
||||||
StringUtility.h StringUtility.cpp
|
StringUtility.h StringUtility.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
set_property(TARGET Universal
|
||||||
|
PROPERTY POSITION_INDEPENDENT_CODE ON
|
||||||
|
)
|
||||||
|
|
||||||
if(CMAKE_VERSION VERSION_LESS 3.20)
|
if(CMAKE_VERSION VERSION_LESS 3.20)
|
||||||
get_filename_component(KYLIN_CORE_INCLUDE_PATH ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
|
get_filename_component(KYLIN_CORE_INCLUDE_PATH ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
|
||||||
if(NOT Boost_USE_STATIC_LIBS)
|
if(NOT Boost_USE_STATIC_LIBS)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user