SMACC2
Loading...
Searching...
No Matches
ssl_http_session.hpp
Go to the documentation of this file.
1// Copyright 2021 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
18Author: Jacobus Lock
19
20******************************************************************************************************************/
21
22#pragma once
23
24#include <boost/asio/executor_work_guard.hpp>
25#include <boost/asio/strand.hpp>
26#include <boost/beast/core.hpp>
27#include <boost/beast/http.hpp>
28#include <boost/beast/ssl.hpp>
29#include <boost/beast/version.hpp>
31#include <iostream>
32#include <string>
33#include <unordered_map>
34
35namespace cl_http
36{
37class ssl_http_session : public std::enable_shared_from_this<ssl_http_session>,
39{
40public:
41 // Objects are constructed with a strand to
42 // ensure that handlers do not execute concurrently.
44 boost::asio::any_io_executor ioc, boost::asio::ssl::context & ssl_context,
45 const std::function<void(const TResponse &)> response);
46
47 virtual ~ssl_http_session() {}
48
49 // Start the asynchronous operation
50 void run(
51 const std::string & host, const std::string & target,
52 const boost::beast::http::verb http_method, const int & version) override;
53
54 std::string getPort() override { return kPort; }
55
56private:
57 const std::string kPort = "443";
58
59 void on_resolve(
60 boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results) override;
61 void fail(boost::beast::error_code ec, const char * what) override;
62 void on_connect(
63 boost::beast::error_code ec,
64 boost::asio::ip::tcp::resolver::results_type::endpoint_type) override;
65 void on_handshake(boost::beast::error_code ec) override;
66 void on_write(boost::beast::error_code ec, std::size_t bytes_transferred) override;
67 void on_read(boost::beast::error_code ec, std::size_t bytes_transferred) override;
68 void on_shutdown(boost::beast::error_code ec) override;
69 void setBody(const std::string & body) override;
70 void setHeaders(const std::unordered_map<std::string, std::string> & headers) override;
71 void appendToHeader(const std::string & key, const std::string & val);
72
73 std::function<void(const TResponse &)> onResponse;
74
75 boost::asio::ip::tcp::resolver resolver_;
76 // boost::beast::tcp_stream stream_;
77 boost::beast::ssl_stream<boost::beast::tcp_stream> stream_;
78 boost::beast::flat_buffer buffer_; // (Must persist between reads)
79 boost::beast::http::request<boost::beast::http::string_body> req_;
81};
82} // namespace cl_http
boost::beast::http::response< boost::beast::http::string_body > TResponse
void fail(boost::beast::error_code ec, const char *what) override
void appendToHeader(const std::string &key, const std::string &val)
void run(const std::string &host, const std::string &target, const boost::beast::http::verb http_method, const int &version) override
boost::beast::ssl_stream< boost::beast::tcp_stream > stream_
boost::beast::flat_buffer buffer_
void on_write(boost::beast::error_code ec, std::size_t bytes_transferred) override
void on_handshake(boost::beast::error_code ec) override
boost::beast::http::request< boost::beast::http::string_body > req_
void on_resolve(boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results) override
std::string getPort() override
void setHeaders(const std::unordered_map< std::string, std::string > &headers) override
std::function< void(const TResponse &)> onResponse
void setBody(const std::string &body) override
void on_read(boost::beast::error_code ec, std::size_t bytes_transferred) override
void on_shutdown(boost::beast::error_code ec) override
void on_connect(boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type::endpoint_type) override
boost::asio::ip::tcp::resolver resolver_