blob: 08748a338105980766a72b1b4801cd35f600a212 [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 Tanousecd6a3a2022-01-07 09:18:40 -080046
Ed Tanous1abe55e2018-09-05 08:30:59 -070047 template <typename Adaptor>
Jonathan Doman102a4cd2024-04-15 16:56:23 -070048 void handleUpgrade(const std::shared_ptr<Request>& req,
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +053049 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
50 Adaptor&& adaptor)
Ed Tanous1abe55e2018-09-05 08:30:59 -070051 {
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +053052 router.handleUpgrade(req, asyncResp, std::forward<Adaptor>(adaptor));
Ed Tanous1abe55e2018-09-05 08:30:59 -070053 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070054
Jonathan Doman102a4cd2024-04-15 16:56:23 -070055 void handle(const std::shared_ptr<Request>& req,
zhanghch058d1b46d2021-04-01 11:18:24 +080056 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -070057 {
zhanghch058d1b46d2021-04-01 11:18:24 +080058 router.handle(req, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -070059 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070060
Ed Tanous8cb2c022024-03-27 16:31:46 -070061 DynamicRule& routeDynamic(const std::string& rule)
Ed Tanous1abe55e2018-09-05 08:30:59 -070062 {
63 return router.newRuleDynamic(rule);
64 }
65
Gunnar Mills1214b7e2020-06-04 10:11:30 -050066 template <uint64_t Tag>
67 auto& route(std::string&& rule)
Ed Tanous1abe55e2018-09-05 08:30:59 -070068 {
69 return router.newRuleTagged<Tag>(std::move(rule));
70 }
71
Ed Tanous1abe55e2018-09-05 08:30:59 -070072 void validate()
73 {
74 router.validate();
75 }
76
Ed Tanous3281bcf2024-06-25 16:02:05 -070077 void loadCertificate()
78 {
79 BMCWEB_LOG_DEBUG("Loading certificate");
80 if (!server)
81 {
82 return;
83 }
84 server->loadCertificate();
85 }
86
Ed Tanous8db83742024-04-13 09:11:15 -070087 std::optional<boost::asio::ip::tcp::acceptor> setupSocket()
88 {
89 if (io == nullptr)
90 {
91 BMCWEB_LOG_CRITICAL("IO was nullptr?");
92 return std::nullopt;
93 }
94 constexpr int defaultPort = 18080;
Ed Tanous38afdb92024-12-11 23:57:53 -080095 if (sd_listen_fds(0) == 1)
Ed Tanous8db83742024-04-13 09:11:15 -070096 {
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
Ed Tanous1abe55e2018-09-05 08:30:59 -0700130 void debugPrint()
131 {
Ed Tanous62598e32023-07-17 17:06:25 -0700132 BMCWEB_LOG_DEBUG("Routing:");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700133 router.debugPrint();
134 }
135
136 std::vector<const std::string*> getRoutes()
137 {
Ed Tanouse05aec52022-01-25 10:28:56 -0800138 const std::string root;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700139 return router.getRoutes(root);
140 }
141 std::vector<const std::string*> getRoutes(const std::string& parent)
142 {
143 return router.getRoutes(parent);
144 }
Ed Tanousb4a7bfa2017-04-04 17:23:00 -0700145
Ed Tanousb74e4402020-09-09 20:26:26 -0700146 App& ssl(std::shared_ptr<boost::asio::ssl::context>&& ctx)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700147 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700148 sslContext = std::move(ctx);
Ed Tanous62598e32023-07-17 17:06:25 -0700149 BMCWEB_LOG_INFO("app::ssl context use_count={}",
150 sslContext.use_count());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700151 return *this;
152 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700153
Ed Tanous4fa45df2023-09-01 14:20:50 -0700154 std::shared_ptr<boost::asio::ssl::context> sslContext = nullptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700155
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700156 boost::asio::io_context& ioContext()
157 {
158 return *io;
159 }
160
Ed Tanous1abe55e2018-09-05 08:30:59 -0700161 private:
Ed Tanous23e64202020-09-15 19:21:30 -0700162 std::shared_ptr<boost::asio::io_context> io;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700163
Ed Tanous8db83742024-04-13 09:11:15 -0700164 std::optional<server_type> server;
165
166 Router router;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700167};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700168} // namespace crow
Ed Tanous52cc1122020-07-18 13:51:21 -0700169using App = crow::App;