Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 3 | #include "async_resp.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 4 | #include "http_request.hpp" |
| 5 | #include "http_server.hpp" |
| 6 | #include "logging.hpp" |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 7 | #include "privileges.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 8 | #include "routing.hpp" |
| 9 | #include "utility.hpp" |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 10 | |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 11 | #include <systemd/sd-daemon.h> |
| 12 | |
Nan Zhou | cec58fe | 2022-06-14 20:45:45 +0000 | [diff] [blame] | 13 | #include <boost/asio/io_context.hpp> |
| 14 | #include <boost/asio/ip/tcp.hpp> |
Nan Zhou | cec58fe | 2022-06-14 20:45:45 +0000 | [diff] [blame] | 15 | #include <boost/asio/ssl/context.hpp> |
Ed Tanous | 003301a | 2024-04-16 09:59:19 -0700 | [diff] [blame] | 16 | #include <boost/asio/ssl/stream.hpp> |
Nan Zhou | cec58fe | 2022-06-14 20:45:45 +0000 | [diff] [blame] | 17 | |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 18 | #include <chrono> |
| 19 | #include <cstdint> |
| 20 | #include <functional> |
| 21 | #include <future> |
| 22 | #include <memory> |
| 23 | #include <string> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 24 | #include <utility> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 25 | |
Patrick Williams | a232343 | 2023-05-12 10:06:35 -0500 | [diff] [blame] | 26 | // NOLINTNEXTLINE(cppcoreguidelines-macro-usage, clang-diagnostic-unused-macros) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | #define BMCWEB_ROUTE(app, url) \ |
Ed Tanous | 47488a9 | 2023-06-26 18:19:33 -0700 | [diff] [blame] | 28 | app.template route<crow::utility::getParameterTag(url)>(url) |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 29 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 30 | namespace crow |
| 31 | { |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 32 | class App |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 33 | { |
| 34 | public: |
Ed Tanous | 003301a | 2024-04-16 09:59:19 -0700 | [diff] [blame] | 35 | using ssl_socket_t = boost::asio::ssl::stream<boost::asio::ip::tcp::socket>; |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 36 | using raw_socket_t = boost::asio::ip::tcp::socket; |
| 37 | |
Ed Tanous | 25b54db | 2024-04-17 15:40:31 -0700 | [diff] [blame] | 38 | using socket_type = std::conditional_t<BMCWEB_INSECURE_DISABLE_SSL, |
| 39 | raw_socket_t, ssl_socket_t>; |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 40 | using server_type = Server<App, socket_type>; |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 41 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 42 | explicit App(std::shared_ptr<boost::asio::io_context> ioIn = |
| 43 | std::make_shared<boost::asio::io_context>()) : |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 44 | io(std::move(ioIn)) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 45 | {} |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 46 | ~App() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 47 | { |
Ed Tanous | 21b4aba | 2023-06-05 11:42:43 -0700 | [diff] [blame] | 48 | stop(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 49 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 50 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 51 | App(const App&) = delete; |
| 52 | App(App&&) = delete; |
| 53 | App& operator=(const App&) = delete; |
| 54 | App& operator=(const App&&) = delete; |
| 55 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 56 | template <typename Adaptor> |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 57 | void handleUpgrade(const std::shared_ptr<Request>& req, |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame] | 58 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 59 | Adaptor&& adaptor) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 60 | { |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame] | 61 | router.handleUpgrade(req, asyncResp, std::forward<Adaptor>(adaptor)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 62 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 63 | |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 64 | void handle(const std::shared_ptr<Request>& req, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 65 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 66 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 67 | router.handle(req, asyncResp); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 68 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 69 | |
Ed Tanous | 8cb2c02 | 2024-03-27 16:31:46 -0700 | [diff] [blame] | 70 | DynamicRule& routeDynamic(const std::string& rule) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 71 | { |
| 72 | return router.newRuleDynamic(rule); |
| 73 | } |
| 74 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 75 | template <uint64_t Tag> |
| 76 | auto& route(std::string&& rule) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 77 | { |
| 78 | return router.newRuleTagged<Tag>(std::move(rule)); |
| 79 | } |
| 80 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 81 | void validate() |
| 82 | { |
| 83 | router.validate(); |
| 84 | } |
| 85 | |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 86 | void loadCertificate() |
| 87 | { |
| 88 | BMCWEB_LOG_DEBUG("Loading certificate"); |
| 89 | if (!server) |
| 90 | { |
| 91 | return; |
| 92 | } |
| 93 | server->loadCertificate(); |
| 94 | } |
| 95 | |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 96 | std::optional<boost::asio::ip::tcp::acceptor> setupSocket() |
| 97 | { |
| 98 | if (io == nullptr) |
| 99 | { |
| 100 | BMCWEB_LOG_CRITICAL("IO was nullptr?"); |
| 101 | return std::nullopt; |
| 102 | } |
| 103 | constexpr int defaultPort = 18080; |
| 104 | int listenFd = sd_listen_fds(0); |
| 105 | if (listenFd == 1) |
| 106 | { |
| 107 | BMCWEB_LOG_INFO("attempting systemd socket activation"); |
| 108 | if (sd_is_socket_inet(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_STREAM, |
| 109 | 1, 0) != 0) |
| 110 | { |
| 111 | BMCWEB_LOG_INFO("Starting webserver on socket handle {}", |
| 112 | SD_LISTEN_FDS_START); |
| 113 | return boost::asio::ip::tcp::acceptor( |
| 114 | *io, boost::asio::ip::tcp::v6(), SD_LISTEN_FDS_START); |
| 115 | } |
| 116 | BMCWEB_LOG_ERROR( |
| 117 | "bad incoming socket, starting webserver on port {}", |
| 118 | defaultPort); |
| 119 | } |
| 120 | BMCWEB_LOG_INFO("Starting webserver on port {}", defaultPort); |
| 121 | return boost::asio::ip::tcp::acceptor( |
| 122 | *io, boost::asio::ip::tcp::endpoint( |
| 123 | boost::asio::ip::make_address("0.0.0.0"), defaultPort)); |
| 124 | } |
| 125 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 126 | void run() |
| 127 | { |
| 128 | validate(); |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 129 | |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 130 | std::optional<boost::asio::ip::tcp::acceptor> acceptor = setupSocket(); |
| 131 | if (!acceptor) |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 132 | { |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 133 | BMCWEB_LOG_CRITICAL("Couldn't start server"); |
| 134 | return; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 135 | } |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 136 | server.emplace(this, std::move(*acceptor), sslContext, io); |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 137 | server->run(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void stop() |
| 141 | { |
| 142 | io->stop(); |
| 143 | } |
| 144 | |
| 145 | void debugPrint() |
| 146 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 147 | BMCWEB_LOG_DEBUG("Routing:"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 148 | router.debugPrint(); |
| 149 | } |
| 150 | |
| 151 | std::vector<const std::string*> getRoutes() |
| 152 | { |
Ed Tanous | e05aec5 | 2022-01-25 10:28:56 -0800 | [diff] [blame] | 153 | const std::string root; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 154 | return router.getRoutes(root); |
| 155 | } |
| 156 | std::vector<const std::string*> getRoutes(const std::string& parent) |
| 157 | { |
| 158 | return router.getRoutes(parent); |
| 159 | } |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 160 | |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame] | 161 | App& ssl(std::shared_ptr<boost::asio::ssl::context>&& ctx) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 162 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 163 | sslContext = std::move(ctx); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 164 | BMCWEB_LOG_INFO("app::ssl context use_count={}", |
| 165 | sslContext.use_count()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 166 | return *this; |
| 167 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 168 | |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 169 | std::shared_ptr<boost::asio::ssl::context> sslContext = nullptr; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 170 | |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 171 | boost::asio::io_context& ioContext() |
| 172 | { |
| 173 | return *io; |
| 174 | } |
| 175 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 176 | private: |
Ed Tanous | 23e6420 | 2020-09-15 19:21:30 -0700 | [diff] [blame] | 177 | std::shared_ptr<boost::asio::io_context> io; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 178 | |
Ed Tanous | 8db8374 | 2024-04-13 09:11:15 -0700 | [diff] [blame] | 179 | std::optional<server_type> server; |
| 180 | |
| 181 | Router router; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 182 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 183 | } // namespace crow |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 184 | using App = crow::App; |