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 | |
Nan Zhou | cec58fe | 2022-06-14 20:45:45 +0000 | [diff] [blame] | 11 | #include <boost/asio/io_context.hpp> |
| 12 | #include <boost/asio/ip/tcp.hpp> |
| 13 | #ifdef BMCWEB_ENABLE_SSL |
| 14 | #include <boost/asio/ssl/context.hpp> |
| 15 | #include <boost/beast/ssl/ssl_stream.hpp> |
| 16 | #endif |
| 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 | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 28 | app.template route<crow::black_magic::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 | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 32 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 33 | using ssl_context_t = boost::asio::ssl::context; |
| 34 | #endif |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 35 | class App |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 36 | { |
| 37 | public: |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 38 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 39 | using ssl_socket_t = boost::beast::ssl_stream<boost::asio::ip::tcp::socket>; |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 40 | using ssl_server_t = Server<App, ssl_socket_t>; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 41 | #else |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 42 | using socket_t = boost::asio::ip::tcp::socket; |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 43 | using server_t = Server<App, socket_t>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 44 | #endif |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 45 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 46 | explicit App(std::shared_ptr<boost::asio::io_context> ioIn = |
| 47 | std::make_shared<boost::asio::io_context>()) : |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 48 | io(std::move(ioIn)) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 49 | {} |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 50 | ~App() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 51 | { |
Ed Tanous | 21b4aba | 2023-06-05 11:42:43 -0700 | [diff] [blame^] | 52 | stop(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 53 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 54 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 55 | App(const App&) = delete; |
| 56 | App(App&&) = delete; |
| 57 | App& operator=(const App&) = delete; |
| 58 | App& operator=(const App&&) = delete; |
| 59 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 60 | template <typename Adaptor> |
P Dheeraj Srujan Kumar | 7e9093e | 2021-09-18 01:19:04 +0530 | [diff] [blame] | 61 | void handleUpgrade(Request& req, |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame] | 62 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 63 | Adaptor&& adaptor) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | { |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame] | 65 | router.handleUpgrade(req, asyncResp, std::forward<Adaptor>(adaptor)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 66 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 67 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 68 | void handle(Request& req, |
| 69 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 70 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 71 | router.handle(req, asyncResp); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 72 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 73 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 74 | DynamicRule& routeDynamic(std::string&& rule) |
| 75 | { |
| 76 | return router.newRuleDynamic(rule); |
| 77 | } |
| 78 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 79 | template <uint64_t Tag> |
| 80 | auto& route(std::string&& rule) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 81 | { |
| 82 | return router.newRuleTagged<Tag>(std::move(rule)); |
| 83 | } |
| 84 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 85 | App& socket(int existingSocket) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 86 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 87 | socketFd = existingSocket; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 88 | return *this; |
| 89 | } |
| 90 | |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame] | 91 | App& port(std::uint16_t port) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 92 | { |
| 93 | portUint = port; |
| 94 | return *this; |
| 95 | } |
| 96 | |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame] | 97 | App& bindaddr(std::string bindaddr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 98 | { |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 99 | bindaddrStr = std::move(bindaddr); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 100 | return *this; |
| 101 | } |
| 102 | |
| 103 | void validate() |
| 104 | { |
| 105 | router.validate(); |
| 106 | } |
| 107 | |
| 108 | void run() |
| 109 | { |
| 110 | validate(); |
| 111 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 112 | if (-1 == socketFd) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 113 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 114 | sslServer = std::make_unique<ssl_server_t>( |
| 115 | this, bindaddrStr, portUint, sslContext, io); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 116 | } |
| 117 | else |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 118 | { |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 119 | sslServer = std::make_unique<ssl_server_t>(this, socketFd, |
| 120 | sslContext, io); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 121 | } |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 122 | sslServer->run(); |
| 123 | |
| 124 | #else |
| 125 | |
| 126 | if (-1 == socketFd) |
| 127 | { |
Nan Zhou | d9049df | 2022-08-02 18:38:18 +0000 | [diff] [blame] | 128 | server = std::make_unique<server_t>(this, bindaddrStr, portUint, |
| 129 | nullptr, io); |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 130 | } |
| 131 | else |
| 132 | { |
Nan Zhou | d9049df | 2022-08-02 18:38:18 +0000 | [diff] [blame] | 133 | server = std::make_unique<server_t>(this, socketFd, nullptr, io); |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 134 | } |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 135 | server->run(); |
| 136 | |
| 137 | #endif |
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 | { |
| 147 | BMCWEB_LOG_DEBUG << "Routing:"; |
| 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 | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 161 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame] | 162 | App& ssl(std::shared_ptr<boost::asio::ssl::context>&& ctx) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 163 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 164 | sslContext = std::move(ctx); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 165 | BMCWEB_LOG_INFO << "app::ssl context use_count=" |
| 166 | << sslContext.use_count(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 167 | return *this; |
| 168 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 169 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 170 | std::shared_ptr<ssl_context_t> sslContext = nullptr; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 171 | |
| 172 | #else |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 173 | template <typename T> |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame] | 174 | App& ssl(T&&) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 175 | { |
| 176 | // We can't call .ssl() member function unless BMCWEB_ENABLE_SSL is |
| 177 | // defined. |
| 178 | static_assert( |
| 179 | // make static_assert dependent to T; always false |
| 180 | std::is_base_of<T, void>::value, |
| 181 | "Define BMCWEB_ENABLE_SSL to enable ssl support."); |
| 182 | return *this; |
| 183 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 184 | #endif |
| 185 | |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 186 | boost::asio::io_context& ioContext() |
| 187 | { |
| 188 | return *io; |
| 189 | } |
| 190 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 191 | private: |
Ed Tanous | 23e6420 | 2020-09-15 19:21:30 -0700 | [diff] [blame] | 192 | std::shared_ptr<boost::asio::io_context> io; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 193 | #ifdef BMCWEB_ENABLE_SSL |
| 194 | uint16_t portUint = 443; |
| 195 | #else |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 196 | uint16_t portUint = 80; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 197 | #endif |
Ed Tanous | c7b9cb3 | 2021-02-11 13:28:35 -0800 | [diff] [blame] | 198 | std::string bindaddrStr = "0.0.0.0"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 199 | int socketFd = -1; |
| 200 | Router router; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 201 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 202 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 203 | std::unique_ptr<ssl_server_t> sslServer; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 204 | #else |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 205 | std::unique_ptr<server_t> server; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 206 | #endif |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 207 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 208 | } // namespace crow |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 209 | using App = crow::App; |