SMACC2
Loading...
Searching...
No Matches
cp_http_session_manager.cpp
Go to the documentation of this file.
1// Copyright 2024 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: Claude (Anthropic AI)
18 *
19 ******************************************************************************************************************/
20
22
23#include <algorithm>
24
25namespace cl_http
26{
27
29: server_name_(server_name), ssl_(true)
30{
31 if (!server_name_.substr(0, 7).compare("http://"))
32 {
33 ssl_ = false;
34 server_name_.erase(0, 7);
35 }
36 else if (!server_name_.substr(0, 8).compare("https://"))
37 {
38 server_name_.erase(0, 8);
39 ssl_ = true;
40 }
41}
42
44: ssl_context_(boost::asio::ssl::context::tlsv13_client), initialized_(false)
45{
46 ssl_context_.set_default_verify_paths();
47 ssl_context_.set_verify_mode(boost::asio::ssl::verify_peer);
48}
49
51
53
54void CpHttpSessionManager::setServerUrl(const std::string & server_url)
55{
56 server_config_ = std::make_unique<ServerConfig>(server_url);
57}
58
59std::shared_ptr<http_session_base> CpHttpSessionManager::createSession(
60 boost::asio::any_io_executor executor,
61 std::function<void(const http_session_base::TResponse &)> callback)
62{
63 if (!server_config_)
64 {
65 throw std::runtime_error("Server URL not configured");
66 }
67
68 if (server_config_->isSSL())
69 {
70 return std::make_shared<ssl_http_session>(executor, ssl_context_, callback);
71 }
72 else
73 {
74 return std::make_shared<http_session>(executor, callback);
75 }
76}
77
79{
80 return server_config_ ? server_config_->isSSL() : false;
81}
82
84{
85 return server_config_ ? server_config_->getServerName() : "";
86}
87
89{
90 return server_config_ ? server_config_->getPort() : "";
91}
92
93} // namespace cl_http
boost::asio::ssl::context ssl_context_
std::shared_ptr< http_session_base > createSession(boost::asio::any_io_executor executor, std::function< void(const http_session_base::TResponse &)> callback)
void setServerUrl(const std::string &server_url)
std::unique_ptr< ServerConfig > server_config_
boost::beast::http::response< boost::beast::http::string_body > TResponse