SMACC2
Loading...
Searching...
No Matches
cl_http_client.cpp
Go to the documentation of this file.
1// Copyright 2023 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Jaycee Lock
18 *
19 ******************************************************************************************************************/
20
22
23namespace cl_http
24{
25ClHttp::ClHttp(const std::string & server_name, const int & timeout)
26: initialized_{false},
27 timeout_{timeout},
28 server_{server_name},
29 worker_guard_{boost::asio::make_work_guard(io_context_.get_executor())},
30 ssl_context_{boost::asio::ssl::context::tlsv13_client}
31{
32 ssl_context_.set_default_verify_paths();
33 ssl_context_.set_verify_mode(boost::asio::ssl::verify_peer);
34}
35
37{
38 worker_guard_.reset();
40}
41
43{
44 if (!initialized_)
45 {
46 tcp_connection_runner_ = std::thread{[&]() { io_context_.run(); }};
47 this->initialized_ = true;
48 }
49}
50
52 const kHttpRequestMethod http_method, const std::string & path, const std::string & body,
53 const std::unordered_map<std::string, std::string> & headers)
54{
55 auto path_used = path;
56 if (path[0] != '/')
57 {
58 std::reverse(path_used.begin(), path_used.end());
59 path_used += '/';
60 std::reverse(path_used.begin(), path_used.end());
61 }
62
63 RCLCPP_INFO(this->getLogger(), "SSL? %d", server_.isSSL());
64 RCLCPP_INFO(this->getLogger(), "Server %s", server_.getServerName().c_str());
65 RCLCPP_INFO(this->getLogger(), "Path %s", path_used.c_str());
66 RCLCPP_INFO(this->getLogger(), "Port %s", server_.getPort().c_str());
67
68 std::shared_ptr<http_session_base> http_session_ptr;
69
70 if (server_.isSSL())
71 {
72 http_session_ptr = std::make_shared<ssl_http_session>(
73 boost::asio::make_strand(io_context_), ssl_context_, callbackHandler);
74 }
75 else
76 {
77 http_session_ptr =
78 std::make_shared<http_session>(boost::asio::make_strand(io_context_), callbackHandler);
79 }
80
81 http_session_ptr->setBody(body);
82 http_session_ptr->setHeaders(headers);
83 http_session_ptr->run(
84 server_.getServerName(), path_used, static_cast<boost::beast::http::verb>(http_method),
86}
87} // namespace cl_http
std::string getServerName() const
std::string getPort() const
std::thread tcp_connection_runner_
void makeRequest(const kHttpRequestMethod http_method, const std::string &path="/", const std::string &body="", const std::unordered_map< std::string, std::string > &headers={})
boost::asio::io_context io_context_
ClHttp(const std::string &server, const int &timeout=1500)
boost::asio::executor_work_guard< decltype(io_context_)::executor_type > worker_guard_
std::function< void(TResponse)> callbackHandler
void onInitialize() override
boost::asio::ssl::context ssl_context_
rclcpp::Logger getLogger()