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 | |
Ed Tanous | 600d239 | 2022-01-07 09:32:03 -0800 | [diff] [blame] | 26 | // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) |
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 | { |
| 52 | this->stop(); |
| 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> |
| 61 | void handleUpgrade(const Request& req, Response& res, Adaptor&& adaptor) |
| 62 | { |
Ed Tanous | f94c4ec | 2022-01-06 12:44:41 -0800 | [diff] [blame] | 63 | router.handleUpgrade(req, res, std::forward<Adaptor>(adaptor)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 65 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 66 | void handle(Request& req, |
| 67 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 68 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 69 | router.handle(req, asyncResp); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 70 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 71 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 72 | DynamicRule& routeDynamic(std::string&& rule) |
| 73 | { |
| 74 | return router.newRuleDynamic(rule); |
| 75 | } |
| 76 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 77 | template <uint64_t Tag> |
| 78 | auto& route(std::string&& rule) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 79 | { |
| 80 | return router.newRuleTagged<Tag>(std::move(rule)); |
| 81 | } |
| 82 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 83 | App& socket(int existingSocket) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 84 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 85 | socketFd = existingSocket; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 86 | return *this; |
| 87 | } |
| 88 | |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame] | 89 | App& port(std::uint16_t port) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 90 | { |
| 91 | portUint = port; |
| 92 | return *this; |
| 93 | } |
| 94 | |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame] | 95 | App& bindaddr(std::string bindaddr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 96 | { |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 97 | bindaddrStr = std::move(bindaddr); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 98 | return *this; |
| 99 | } |
| 100 | |
| 101 | void validate() |
| 102 | { |
| 103 | router.validate(); |
| 104 | } |
| 105 | |
| 106 | void run() |
| 107 | { |
| 108 | validate(); |
| 109 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 110 | if (-1 == socketFd) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 111 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 112 | sslServer = std::make_unique<ssl_server_t>( |
| 113 | this, bindaddrStr, portUint, sslContext, io); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 114 | } |
| 115 | else |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 116 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 117 | sslServer = |
| 118 | std::make_unique<ssl_server_t>(this, socketFd, sslContext, io); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 119 | } |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 120 | sslServer->run(); |
| 121 | |
| 122 | #else |
| 123 | |
| 124 | if (-1 == socketFd) |
| 125 | { |
Nan Zhou | d9049df | 2022-08-02 18:38:18 +0000 | [diff] [blame] | 126 | server = std::make_unique<server_t>(this, bindaddrStr, portUint, |
| 127 | nullptr, io); |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 128 | } |
| 129 | else |
| 130 | { |
Nan Zhou | d9049df | 2022-08-02 18:38:18 +0000 | [diff] [blame] | 131 | server = std::make_unique<server_t>(this, socketFd, nullptr, io); |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 132 | } |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 133 | server->run(); |
| 134 | |
| 135 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void stop() |
| 139 | { |
| 140 | io->stop(); |
| 141 | } |
| 142 | |
| 143 | void debugPrint() |
| 144 | { |
| 145 | BMCWEB_LOG_DEBUG << "Routing:"; |
| 146 | router.debugPrint(); |
| 147 | } |
| 148 | |
| 149 | std::vector<const std::string*> getRoutes() |
| 150 | { |
Ed Tanous | e05aec5 | 2022-01-25 10:28:56 -0800 | [diff] [blame] | 151 | const std::string root; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 152 | return router.getRoutes(root); |
| 153 | } |
| 154 | std::vector<const std::string*> getRoutes(const std::string& parent) |
| 155 | { |
| 156 | return router.getRoutes(parent); |
| 157 | } |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 158 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 159 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame] | 160 | App& ssl(std::shared_ptr<boost::asio::ssl::context>&& ctx) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 161 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 162 | sslContext = std::move(ctx); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 163 | BMCWEB_LOG_INFO << "app::ssl context use_count=" |
| 164 | << sslContext.use_count(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 165 | return *this; |
| 166 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 167 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 168 | std::shared_ptr<ssl_context_t> sslContext = nullptr; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 169 | |
| 170 | #else |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 171 | template <typename T> |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame] | 172 | App& ssl(T&&) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 173 | { |
| 174 | // We can't call .ssl() member function unless BMCWEB_ENABLE_SSL is |
| 175 | // defined. |
| 176 | static_assert( |
| 177 | // make static_assert dependent to T; always false |
| 178 | std::is_base_of<T, void>::value, |
| 179 | "Define BMCWEB_ENABLE_SSL to enable ssl support."); |
| 180 | return *this; |
| 181 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 182 | #endif |
| 183 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 184 | private: |
Ed Tanous | 23e6420 | 2020-09-15 19:21:30 -0700 | [diff] [blame] | 185 | std::shared_ptr<boost::asio::io_context> io; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 186 | #ifdef BMCWEB_ENABLE_SSL |
| 187 | uint16_t portUint = 443; |
| 188 | #else |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 189 | uint16_t portUint = 80; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 190 | #endif |
Ed Tanous | c7b9cb3 | 2021-02-11 13:28:35 -0800 | [diff] [blame] | 191 | std::string bindaddrStr = "0.0.0.0"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 192 | int socketFd = -1; |
| 193 | Router router; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 194 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 195 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 196 | std::unique_ptr<ssl_server_t> sslServer; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 197 | #else |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 198 | std::unique_ptr<server_t> server; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 199 | #endif |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 200 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 201 | } // namespace crow |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 202 | using App = crow::App; |