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 | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 11 | #include <chrono> |
| 12 | #include <cstdint> |
| 13 | #include <functional> |
| 14 | #include <future> |
| 15 | #include <memory> |
| 16 | #include <string> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 17 | #include <utility> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 18 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 19 | #define BMCWEB_ROUTE(app, url) \ |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 20 | app.template route<crow::black_magic::getParameterTag(url)>(url) |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 21 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 22 | namespace crow |
| 23 | { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 24 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 25 | using ssl_context_t = boost::asio::ssl::context; |
| 26 | #endif |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 27 | class App |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 28 | { |
| 29 | public: |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 30 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 31 | 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] | 32 | using ssl_server_t = Server<App, ssl_socket_t>; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 33 | #else |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 34 | using socket_t = boost::asio::ip::tcp::socket; |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 35 | using server_t = Server<App, socket_t>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 36 | #endif |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 37 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 38 | explicit App(std::shared_ptr<boost::asio::io_context> ioIn = |
| 39 | std::make_shared<boost::asio::io_context>()) : |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 40 | io(std::move(ioIn)) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 41 | {} |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 42 | ~App() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 43 | { |
| 44 | this->stop(); |
| 45 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 46 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 47 | template <typename Adaptor> |
| 48 | void handleUpgrade(const Request& req, Response& res, Adaptor&& adaptor) |
| 49 | { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 50 | router.handleUpgrade(req, res, std::move(adaptor)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 51 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 52 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 53 | void handle(Request& req, |
| 54 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 55 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 56 | router.handle(req, asyncResp); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 57 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 58 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 59 | DynamicRule& routeDynamic(std::string&& rule) |
| 60 | { |
| 61 | return router.newRuleDynamic(rule); |
| 62 | } |
| 63 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 64 | template <uint64_t Tag> |
| 65 | auto& route(std::string&& rule) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 66 | { |
| 67 | return router.newRuleTagged<Tag>(std::move(rule)); |
| 68 | } |
| 69 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 70 | App& socket(int existingSocket) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 71 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 72 | socketFd = existingSocket; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 73 | return *this; |
| 74 | } |
| 75 | |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame] | 76 | App& port(std::uint16_t port) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 77 | { |
| 78 | portUint = port; |
| 79 | return *this; |
| 80 | } |
| 81 | |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame] | 82 | App& bindaddr(std::string bindaddr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 83 | { |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 84 | bindaddrStr = std::move(bindaddr); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 85 | return *this; |
| 86 | } |
| 87 | |
| 88 | void validate() |
| 89 | { |
| 90 | router.validate(); |
| 91 | } |
| 92 | |
| 93 | void run() |
| 94 | { |
| 95 | validate(); |
| 96 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 97 | if (-1 == socketFd) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 98 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 99 | sslServer = std::make_unique<ssl_server_t>( |
| 100 | this, bindaddrStr, portUint, sslContext, io); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 101 | } |
| 102 | else |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 103 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 104 | sslServer = |
| 105 | std::make_unique<ssl_server_t>(this, socketFd, sslContext, io); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 106 | } |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 107 | sslServer->run(); |
| 108 | |
| 109 | #else |
| 110 | |
| 111 | if (-1 == socketFd) |
| 112 | { |
| 113 | server = std::move(std::make_unique<server_t>( |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 114 | this, bindaddrStr, portUint, nullptr, io)); |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 115 | } |
| 116 | else |
| 117 | { |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 118 | server = std::move( |
| 119 | std::make_unique<server_t>(this, socketFd, nullptr, io)); |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 120 | } |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 121 | server->run(); |
| 122 | |
| 123 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void stop() |
| 127 | { |
| 128 | io->stop(); |
| 129 | } |
| 130 | |
| 131 | void debugPrint() |
| 132 | { |
| 133 | BMCWEB_LOG_DEBUG << "Routing:"; |
| 134 | router.debugPrint(); |
| 135 | } |
| 136 | |
| 137 | std::vector<const std::string*> getRoutes() |
| 138 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 139 | const std::string root(""); |
| 140 | return router.getRoutes(root); |
| 141 | } |
| 142 | std::vector<const std::string*> getRoutes(const std::string& parent) |
| 143 | { |
| 144 | return router.getRoutes(parent); |
| 145 | } |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 146 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 147 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 148 | App& sslFile(const std::string& crtFilename, const std::string& keyFilename) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 149 | { |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 150 | sslContext = std::make_shared<ssl_context_t>( |
| 151 | boost::asio::ssl::context::tls_server); |
| 152 | sslContext->set_verify_mode(boost::asio::ssl::verify_peer); |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 153 | sslContext->use_certificate_file(crtFilename, ssl_context_t::pem); |
| 154 | sslContext->use_private_key_file(keyFilename, ssl_context_t::pem); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 155 | sslContext->set_options(boost::asio::ssl::context::default_workarounds | |
| 156 | boost::asio::ssl::context::no_sslv2 | |
| 157 | boost::asio::ssl::context::no_sslv3 | |
| 158 | boost::asio::ssl::context::no_tlsv1 | |
| 159 | boost::asio::ssl::context::no_tlsv1_1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 160 | return *this; |
| 161 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 162 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 163 | App& sslFile(const std::string& pemFilename) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 164 | { |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 165 | sslContext = std::make_shared<ssl_context_t>( |
| 166 | boost::asio::ssl::context::tls_server); |
| 167 | sslContext->set_verify_mode(boost::asio::ssl::verify_peer); |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 168 | sslContext->load_verify_file(pemFilename); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 169 | sslContext->set_options(boost::asio::ssl::context::default_workarounds | |
| 170 | boost::asio::ssl::context::no_sslv2 | |
| 171 | boost::asio::ssl::context::no_sslv3 | |
| 172 | boost::asio::ssl::context::no_tlsv1 | |
| 173 | boost::asio::ssl::context::no_tlsv1_1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 174 | return *this; |
| 175 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 176 | |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame] | 177 | App& ssl(std::shared_ptr<boost::asio::ssl::context>&& ctx) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 178 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 179 | sslContext = std::move(ctx); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 180 | BMCWEB_LOG_INFO << "app::ssl context use_count=" |
| 181 | << sslContext.use_count(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 182 | return *this; |
| 183 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 184 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 185 | std::shared_ptr<ssl_context_t> sslContext = nullptr; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 186 | |
| 187 | #else |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 188 | template <typename T, typename... Remain> |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame] | 189 | App& ssl_file(T&&, Remain&&...) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 190 | { |
| 191 | // We can't call .ssl() member function unless BMCWEB_ENABLE_SSL is |
| 192 | // defined. |
| 193 | static_assert( |
| 194 | // make static_assert dependent to T; always false |
| 195 | std::is_base_of<T, void>::value, |
| 196 | "Define BMCWEB_ENABLE_SSL to enable ssl support."); |
| 197 | return *this; |
| 198 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 199 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 200 | template <typename T> |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame] | 201 | App& ssl(T&&) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 202 | { |
| 203 | // We can't call .ssl() member function unless BMCWEB_ENABLE_SSL is |
| 204 | // defined. |
| 205 | static_assert( |
| 206 | // make static_assert dependent to T; always false |
| 207 | std::is_base_of<T, void>::value, |
| 208 | "Define BMCWEB_ENABLE_SSL to enable ssl support."); |
| 209 | return *this; |
| 210 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 211 | #endif |
| 212 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 213 | private: |
Ed Tanous | 23e6420 | 2020-09-15 19:21:30 -0700 | [diff] [blame] | 214 | std::shared_ptr<boost::asio::io_context> io; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 215 | #ifdef BMCWEB_ENABLE_SSL |
| 216 | uint16_t portUint = 443; |
| 217 | #else |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 218 | uint16_t portUint = 80; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 219 | #endif |
Ed Tanous | c7b9cb3 | 2021-02-11 13:28:35 -0800 | [diff] [blame] | 220 | std::string bindaddrStr = "0.0.0.0"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 221 | int socketFd = -1; |
| 222 | Router router; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 223 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 224 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 225 | std::unique_ptr<ssl_server_t> sslServer; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 226 | #else |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 227 | std::unique_ptr<server_t> server; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 228 | #endif |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 229 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 230 | } // namespace crow |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 231 | using App = crow::App; |