blob: 29cb4de9ebef194e503ceaefe719bc1f814ca67a [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
2
zhanghch058d1b46d2021-04-01 11:18:24 +08003#include "async_resp.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -07004#include "http_request.hpp"
5#include "http_server.hpp"
6#include "logging.hpp"
Tanousf00032d2018-11-05 01:18:10 -03007#include "privileges.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -07008#include "routing.hpp"
9#include "utility.hpp"
Tanousf00032d2018-11-05 01:18:10 -030010
Ed Tanous8db83742024-04-13 09:11:15 -070011#include <systemd/sd-daemon.h>
12
Nan Zhoucec58fe2022-06-14 20:45:45 +000013#include <boost/asio/io_context.hpp>
14#include <boost/asio/ip/tcp.hpp>
Nan Zhoucec58fe2022-06-14 20:45:45 +000015#include <boost/asio/ssl/context.hpp>
Ed Tanous003301a2024-04-16 09:59:19 -070016#include <boost/asio/ssl/stream.hpp>
Nan Zhoucec58fe2022-06-14 20:45:45 +000017
Ed Tanous7045c8d2017-04-03 10:04:37 -070018#include <chrono>
19#include <cstdint>
20#include <functional>
21#include <future>
22#include <memory>
23#include <string>
Ed Tanous911ac312017-08-15 09:37:42 -070024#include <utility>
Ed Tanous1abe55e2018-09-05 08:30:59 -070025
Patrick Williamsa2323432023-05-12 10:06:35 -050026// NOLINTNEXTLINE(cppcoreguidelines-macro-usage, clang-diagnostic-unused-macros)
Ed Tanous1abe55e2018-09-05 08:30:59 -070027#define BMCWEB_ROUTE(app, url) \
Ed Tanous47488a92023-06-26 18:19:33 -070028 app.template route<crow::utility::getParameterTag(url)>(url)
Ed Tanous7045c8d2017-04-03 10:04:37 -070029
Ed Tanous1abe55e2018-09-05 08:30:59 -070030namespace crow
31{
Ed Tanous52cc1122020-07-18 13:51:21 -070032class App
Ed Tanous1abe55e2018-09-05 08:30:59 -070033{
34 public:
Ed Tanous003301a2024-04-16 09:59:19 -070035 using ssl_socket_t = boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
Ed Tanous8db83742024-04-13 09:11:15 -070036 using raw_socket_t = boost::asio::ip::tcp::socket;
37
Ed Tanous25b54db2024-04-17 15:40:31 -070038 using socket_type = std::conditional_t<BMCWEB_INSECURE_DISABLE_SSL,
39 raw_socket_t, ssl_socket_t>;
Ed Tanous8db83742024-04-13 09:11:15 -070040 using server_type = Server<App, socket_type>;
Ed Tanousceac6f72018-12-02 11:58:47 -080041
Ed Tanous52cc1122020-07-18 13:51:21 -070042 explicit App(std::shared_ptr<boost::asio::io_context> ioIn =
43 std::make_shared<boost::asio::io_context>()) :
Ed Tanous271584a2019-07-09 16:24:22 -070044 io(std::move(ioIn))
Gunnar Mills1214b7e2020-06-04 10:11:30 -050045 {}
Ed Tanous52cc1122020-07-18 13:51:21 -070046 ~App()
Ed Tanous1abe55e2018-09-05 08:30:59 -070047 {
Ed Tanous21b4aba2023-06-05 11:42:43 -070048 stop();
Ed Tanous1abe55e2018-09-05 08:30:59 -070049 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070050
Ed Tanousecd6a3a2022-01-07 09:18:40 -080051 App(const App&) = delete;
52 App(App&&) = delete;
53 App& operator=(const App&) = delete;
54 App& operator=(const App&&) = delete;
55
Ed Tanous1abe55e2018-09-05 08:30:59 -070056 template <typename Adaptor>
Jonathan Doman102a4cd2024-04-15 16:56:23 -070057 void handleUpgrade(const std::shared_ptr<Request>& req,
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +053058 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
59 Adaptor&& adaptor)
Ed Tanous1abe55e2018-09-05 08:30:59 -070060 {
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +053061 router.handleUpgrade(req, asyncResp, std::forward<Adaptor>(adaptor));
Ed Tanous1abe55e2018-09-05 08:30:59 -070062 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070063
Jonathan Doman102a4cd2024-04-15 16:56:23 -070064 void handle(const std::shared_ptr<Request>& req,
zhanghch058d1b46d2021-04-01 11:18:24 +080065 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -070066 {
zhanghch058d1b46d2021-04-01 11:18:24 +080067 router.handle(req, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -070068 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070069
Ed Tanous8cb2c022024-03-27 16:31:46 -070070 DynamicRule& routeDynamic(const std::string& rule)
Ed Tanous1abe55e2018-09-05 08:30:59 -070071 {
72 return router.newRuleDynamic(rule);
73 }
74
Gunnar Mills1214b7e2020-06-04 10:11:30 -050075 template <uint64_t Tag>
76 auto& route(std::string&& rule)
Ed Tanous1abe55e2018-09-05 08:30:59 -070077 {
78 return router.newRuleTagged<Tag>(std::move(rule));
79 }
80
Ed Tanous1abe55e2018-09-05 08:30:59 -070081 void validate()
82 {
83 router.validate();
84 }
85
Ed Tanous8db83742024-04-13 09:11:15 -070086 std::optional<boost::asio::ip::tcp::acceptor> setupSocket()
87 {
88 if (io == nullptr)
89 {
90 BMCWEB_LOG_CRITICAL("IO was nullptr?");
91 return std::nullopt;
92 }
93 constexpr int defaultPort = 18080;
94 int listenFd = sd_listen_fds(0);
95 if (listenFd == 1)
96 {
97 BMCWEB_LOG_INFO("attempting systemd socket activation");
98 if (sd_is_socket_inet(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_STREAM,
99 1, 0) != 0)
100 {
101 BMCWEB_LOG_INFO("Starting webserver on socket handle {}",
102 SD_LISTEN_FDS_START);
103 return boost::asio::ip::tcp::acceptor(
104 *io, boost::asio::ip::tcp::v6(), SD_LISTEN_FDS_START);
105 }
106 BMCWEB_LOG_ERROR(
107 "bad incoming socket, starting webserver on port {}",
108 defaultPort);
109 }
110 BMCWEB_LOG_INFO("Starting webserver on port {}", defaultPort);
111 return boost::asio::ip::tcp::acceptor(
112 *io, boost::asio::ip::tcp::endpoint(
113 boost::asio::ip::make_address("0.0.0.0"), defaultPort));
114 }
115
Ed Tanous1abe55e2018-09-05 08:30:59 -0700116 void run()
117 {
118 validate();
Ed Tanous789a6a32018-11-29 15:17:22 -0800119
Ed Tanous8db83742024-04-13 09:11:15 -0700120 std::optional<boost::asio::ip::tcp::acceptor> acceptor = setupSocket();
121 if (!acceptor)
Ed Tanous789a6a32018-11-29 15:17:22 -0800122 {
Ed Tanous8db83742024-04-13 09:11:15 -0700123 BMCWEB_LOG_CRITICAL("Couldn't start server");
124 return;
Ed Tanous789a6a32018-11-29 15:17:22 -0800125 }
Ed Tanous8db83742024-04-13 09:11:15 -0700126 server.emplace(this, std::move(*acceptor), sslContext, io);
Ed Tanous789a6a32018-11-29 15:17:22 -0800127 server->run();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700128 }
129
130 void stop()
131 {
132 io->stop();
133 }
134
135 void debugPrint()
136 {
Ed Tanous62598e32023-07-17 17:06:25 -0700137 BMCWEB_LOG_DEBUG("Routing:");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700138 router.debugPrint();
139 }
140
141 std::vector<const std::string*> getRoutes()
142 {
Ed Tanouse05aec52022-01-25 10:28:56 -0800143 const std::string root;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700144 return router.getRoutes(root);
145 }
146 std::vector<const std::string*> getRoutes(const std::string& parent)
147 {
148 return router.getRoutes(parent);
149 }
Ed Tanousb4a7bfa2017-04-04 17:23:00 -0700150
Ed Tanousb74e4402020-09-09 20:26:26 -0700151 App& ssl(std::shared_ptr<boost::asio::ssl::context>&& ctx)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700152 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700153 sslContext = std::move(ctx);
Ed Tanous62598e32023-07-17 17:06:25 -0700154 BMCWEB_LOG_INFO("app::ssl context use_count={}",
155 sslContext.use_count());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700156 return *this;
157 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700158
Ed Tanous4fa45df2023-09-01 14:20:50 -0700159 std::shared_ptr<boost::asio::ssl::context> sslContext = nullptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700160
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700161 boost::asio::io_context& ioContext()
162 {
163 return *io;
164 }
165
Ed Tanous1abe55e2018-09-05 08:30:59 -0700166 private:
Ed Tanous23e64202020-09-15 19:21:30 -0700167 std::shared_ptr<boost::asio::io_context> io;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700168
Ed Tanous8db83742024-04-13 09:11:15 -0700169 std::optional<server_type> server;
170
171 Router router;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700172};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700173} // namespace crow
Ed Tanous52cc1122020-07-18 13:51:21 -0700174using App = crow::App;