18 #ifdef HAVE_THRIFT_MESSAGE_LIMIT
25 #include <boost/algorithm/string.hpp>
26 #include <boost/core/ignore_unused.hpp>
27 #include <boost/filesystem.hpp>
29 #include <thrift/protocol/TBinaryProtocol.h>
30 #include <thrift/transport/THttpClient.h>
31 #include <thrift/transport/TSocket.h>
34 using namespace ::apache::thrift::transport;
35 using namespace ::apache::thrift::protocol;
39 if (ca_cert_file.empty()) {
40 static std::list<std::string> v_known_ca_paths({
41 "/etc/ssl/certs/ca-certificates.crt",
42 "/etc/pki/tls/certs/ca-bundle.crt",
43 "/usr/share/ssl/certs/ca-bundle.crt",
44 "/usr/local/share/certs/ca-root.crt",
46 "/etc/ssl/ca-bundle.pem",
48 for (
const auto& known_ca_path : v_known_ca_paths) {
49 if (boost::filesystem::exists(known_ca_path)) {
50 ca_cert_file = known_ca_path;
60 boost::ignore_unused(sa);
64 boost::ignore_unused(host);
65 boost::ignore_unused(
name);
66 boost::ignore_unused(size);
71 int size)
throw()
override {
72 boost::ignore_unused(sa);
73 boost::ignore_unused(data);
74 boost::ignore_unused(size);
95 #ifdef HAVE_THRIFT_MESSAGE_LIMIT
98 : THttpClient(transport, host, path) {
103 #ifdef HAVE_THRIFT_MESSAGE_LIMIT
106 : THttpClient(host, port, path) {
113 void parseHeader(
char*
header)
override {
115 if (boost::istarts_with(header,
"set-cookie:")) {
116 std::string tmp(header);
117 std::string cookie = tmp.substr(tmp.find(
":") + 1, std::string::npos);
118 cookies_.push_back(cookie);
120 THttpClient::parseHeader(header);
123 void flush()
override {
133 writeBuffer_.getBuffer(&buf, &len);
135 constexpr
static const char* CRLF =
"\r\n";
137 std::ostringstream h;
138 h <<
"POST " << path_ <<
" HTTP/1.1" << CRLF <<
"Host: " << host_ << CRLF
139 <<
"Content-Type: application/x-thrift" << CRLF <<
"Content-Length: " << len << CRLF
140 <<
"Accept: application/x-thrift" << CRLF <<
"User-Agent: Thrift/"
141 << THRIFT_PACKAGE_VERSION <<
" (C++/THttpClient)" << CRLF
142 <<
"Connection: keep-alive" << CRLF;
143 if (!cookies_.empty()) {
150 std::string header = h.str();
151 if (header.size() > (std::numeric_limits<uint32_t>::max)()) {
152 throw TTransportException(
154 "]. Max = " +
std::to_string((std::numeric_limits<uint32_t>::max)()));
157 transport_->write((
const uint8_t*)header.c_str(),
158 static_cast<uint32_t
>(header.size()));
159 transport_->write(buf, len);
163 writeBuffer_.resetBuffer();
167 std::vector<std::string> cookies_;
173 bool skip_host_verify,
174 std::shared_ptr<TSSLSocketFactory> factory)
175 : server_host_(server_host)
177 , conn_type_(conn_type)
178 , skip_host_verify_(skip_host_verify)
179 , trust_cert_file_(
"") {
184 factory_->ciphers(
"ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
193 std::shared_ptr<apache::thrift::transport::TTransport> mytransport;
208 }
catch (
const apache::thrift::TException& e) {
209 throw apache::thrift::TException(std::string(e.what()) +
": host " +
server_host_ +
214 return std::shared_ptr<TProtocol>(
new TJSONProtocol(mytransport));
216 return std::shared_ptr<TProtocol>(
new TBinaryProtocol(mytransport));
221 const std::string& server_host,
223 const std::string& ca_cert_name,
229 std::shared_ptr<TTransport> transport;
231 if (!
factory_ && !ca_cert_name.empty()) {
234 std::shared_ptr<TSSLSocketFactory>(
new TSSLSocketFactory(SSLProtocol::SSLTLS));
235 factory_->ciphers(
"ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
236 factory_->loadTrustedCertificates(ca_cert_name.c_str());
241 #ifdef HAVE_THRIFT_MESSAGE_LIMIT
245 const auto socket = std::make_shared<TSocket>(server_host, port);
248 socket->setKeepAlive(with_keepalive);
249 socket->setConnTimeout(connect_timeout);
250 socket->setRecvTimeout(recv_timeout);
251 socket->setSendTimeout(send_timeout);
253 socket->setLinger(
false, 0);
256 #ifdef HAVE_THRIFT_MESSAGE_LIMIT
259 transport = std::make_shared<TBufferedTransport>(socket);
262 std::shared_ptr<TSocket> secure_socket =
factory_->createSocket(server_host, port);
264 secure_socket->setKeepAlive(with_keepalive);
265 secure_socket->setConnTimeout(connect_timeout);
266 secure_socket->setRecvTimeout(recv_timeout);
267 secure_socket->setSendTimeout(send_timeout);
269 secure_socket->setLinger(
false, 0);
272 #ifdef HAVE_THRIFT_MESSAGE_LIMIT
273 transport = std::shared_ptr<TTransport>(
276 transport = std::shared_ptr<TTransport>(
new TBufferedTransport(secure_socket));
284 const std::string& server_host,
286 const std::string& trust_cert_fileX,
294 std::shared_ptr<TSSLSocketFactory>(
new TSSLSocketFactory(SSLProtocol::SSLTLS));
296 std::shared_ptr<TTransport> transport;
297 std::shared_ptr<TTransport> socket;
307 socket =
factory_->createSocket(server_host, port);
312 std::shared_ptr<TTransport>(
new ProxyTHttpClient(socket, server_host,
"/"));
314 transport = std::shared_ptr<TTransport>(
new ProxyTHttpClient(server_host, port,
"/"));
ThriftConnectionType conn_type_
void check_standard_ca(std::string &ca_cert_file)
std::shared_ptr< TTransport > open_http_client_transport(const std::string &server_host, const int port, const std::string &trust_cert_file_, bool use_https, bool skip_verify)
virtual ~ThriftClientConnection()
std::shared_ptr< apache::thrift::TConfiguration > default_tconfig()
Decision verify(const sockaddr_storage &sa) override
Decision verify(const std::string &host, const char *name, int size) override
std::shared_ptr< TTransport > open_buffered_client_transport(const std::string &server_host, const int port, const std::string &ca_cert_name, const bool with_timeout=false, const bool with_keepalive=true, const unsigned connect_timeout=0, const unsigned recv_timeount=0, const unsigned send_timeout=0)
ProxyTHttpClient(std::shared_ptr< TTransport > transport, std::string host, std::string path)
Decision verify(const sockaddr_storage &sa, const char *data, int size) override
std::string ca_cert_name_
std::string trust_cert_file_
std::shared_ptr< TProtocol > get_protocol()
std::shared_ptr< TSSLSocketFactory > factory_
AccessManager::Decision Decision