SMACC2
Loading...
Searching...
No Matches
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/version.hpp>
30#include <iostream>
31#include <string>
32
33namespace cl_http
34{
35class http_session : public std::enable_shared_from_this<http_session>, public http_session_base
36{
37public:
38 // Objects are constructed with a strand to
39 // ensure that handlers do not execute concurrently.
41 boost::asio::any_io_executor ioc, const std::function<void(const TResponse &)> response);
42
43 virtual ~http_session() {}
44
45 // Start the asynchronous operation
46 void run(
47 const std::string & host, const std::string & target,
48 const boost::beast::http::verb http_method, const int & version) override;
49
50 std::string getPort() override { return kPort; }
51
52private:
53 const std::string kPort = "80";
54
55 void on_resolve(
56 boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results) override;
57 void fail(boost::beast::error_code ec, const char * what) override;
58 void on_connect(
59 boost::beast::error_code ec,
60 boost::asio::ip::tcp::resolver::results_type::endpoint_type) override;
61 void on_write(boost::beast::error_code ec, std::size_t bytes_transferred) override;
62 void on_read(boost::beast::error_code ec, std::size_t bytes_transferred) override;
63 void setBody(const std::string & body) override;
64 void setHeaders(const std::unordered_map<std::string, std::string> & headers) override {}
65
66 std::function<void(const TResponse &)> onResponse;
67
68 boost::asio::ip::tcp::resolver resolver_;
69 boost::beast::tcp_stream stream_;
70 boost::beast::flat_buffer buffer_; // (Must persist between reads)
71 boost::beast::http::request<boost::beast::http::string_body> req_;
73};
74} // namespace cl_http
boost::beast::http::response< boost::beast::http::string_body > TResponse
void setHeaders(const std::unordered_map< std::string, std::string > &headers) override
void on_resolve(boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results) override
void on_read(boost::beast::error_code ec, std::size_t bytes_transferred) override
boost::beast::flat_buffer buffer_
const std::string kPort
void setBody(const std::string &body) override
boost::beast::http::request< boost::beast::http::string_body > req_
void run(const std::string &host, const std::string &target, const boost::beast::http::verb http_method, const int &version) override
std::string getPort() override
boost::asio::ip::tcp::resolver resolver_
void on_connect(boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type::endpoint_type) override
boost::beast::tcp_stream stream_
std::function< void(const TResponse &)> onResponse
void fail(boost::beast::error_code ec, const char *what) override
void on_write(boost::beast::error_code ec, std::size_t bytes_transferred) override