Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 3 | #include "http_request.h" |
| 4 | #include "http_server.h" |
| 5 | #include "logging.h" |
| 6 | #include "middleware_context.h" |
| 7 | #include "routing.h" |
| 8 | #include "utility.h" |
| 9 | |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 10 | #include "privileges.hpp" |
| 11 | |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 12 | #include <chrono> |
| 13 | #include <cstdint> |
| 14 | #include <functional> |
| 15 | #include <future> |
| 16 | #include <memory> |
| 17 | #include <string> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 18 | #include <utility> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 19 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 20 | #define BMCWEB_ROUTE(app, url) \ |
| 21 | app.template route<crow::black_magic::get_parameter_tag(url)>(url) |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 22 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 23 | namespace crow |
| 24 | { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 25 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 26 | using ssl_context_t = boost::asio::ssl::context; |
| 27 | #endif |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 28 | template <typename... Middlewares> |
| 29 | class Crow |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 30 | { |
| 31 | public: |
| 32 | using self_t = Crow; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 33 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 34 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 35 | using ssl_socket_t = boost::beast::ssl_stream<boost::asio::ip::tcp::socket>; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 36 | using ssl_server_t = Server<Crow, ssl_socket_t, Middlewares...>; |
| 37 | #else |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 38 | using socket_t = boost::asio::ip::tcp::socket; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 39 | using server_t = Server<Crow, socket_t, Middlewares...>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 40 | #endif |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 41 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 42 | explicit Crow(std::shared_ptr<boost::asio::io_context> ioIn = |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 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 | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 46 | ~Crow() |
| 47 | { |
| 48 | this->stop(); |
| 49 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 50 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 51 | template <typename Adaptor> |
| 52 | void handleUpgrade(const Request& req, Response& res, Adaptor&& adaptor) |
| 53 | { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 54 | router.handleUpgrade(req, res, std::move(adaptor)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 55 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 56 | |
RAJESWARAN THILLAIGOVINDAN | 61dbeef | 2019-12-13 04:26:54 -0600 | [diff] [blame] | 57 | void handle(Request& req, Response& res) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 58 | { |
| 59 | router.handle(req, res); |
| 60 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 61 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 62 | DynamicRule& routeDynamic(std::string&& rule) |
| 63 | { |
| 64 | return router.newRuleDynamic(rule); |
| 65 | } |
| 66 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 67 | template <uint64_t Tag> |
| 68 | auto& route(std::string&& rule) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 69 | { |
| 70 | return router.newRuleTagged<Tag>(std::move(rule)); |
| 71 | } |
| 72 | |
| 73 | self_t& socket(int existing_socket) |
| 74 | { |
| 75 | socketFd = existing_socket; |
| 76 | return *this; |
| 77 | } |
| 78 | |
| 79 | self_t& port(std::uint16_t port) |
| 80 | { |
| 81 | portUint = port; |
| 82 | return *this; |
| 83 | } |
| 84 | |
| 85 | self_t& bindaddr(std::string bindaddr) |
| 86 | { |
| 87 | bindaddrStr = bindaddr; |
| 88 | return *this; |
| 89 | } |
| 90 | |
| 91 | void validate() |
| 92 | { |
| 93 | router.validate(); |
| 94 | } |
| 95 | |
| 96 | void run() |
| 97 | { |
| 98 | validate(); |
| 99 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 100 | if (-1 == socketFd) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 101 | { |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 102 | sslServer = std::move(std::make_unique<ssl_server_t>( |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 103 | this, bindaddrStr, portUint, sslContext, &middlewares, io)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 104 | } |
| 105 | else |
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 = std::move(std::make_unique<ssl_server_t>( |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 108 | this, socketFd, sslContext, &middlewares, io)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 109 | } |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 110 | sslServer->setTickFunction(tickInterval, tickFunction); |
| 111 | sslServer->run(); |
| 112 | |
| 113 | #else |
| 114 | |
| 115 | if (-1 == socketFd) |
| 116 | { |
| 117 | server = std::move(std::make_unique<server_t>( |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 118 | this, bindaddrStr, portUint, nullptr, &middlewares, io)); |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 119 | } |
| 120 | else |
| 121 | { |
| 122 | server = std::move(std::make_unique<server_t>( |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 123 | this, socketFd, nullptr, &middlewares, io)); |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 124 | } |
| 125 | server->setTickFunction(tickInterval, tickFunction); |
| 126 | server->run(); |
| 127 | |
| 128 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void stop() |
| 132 | { |
| 133 | io->stop(); |
| 134 | } |
| 135 | |
| 136 | void debugPrint() |
| 137 | { |
| 138 | BMCWEB_LOG_DEBUG << "Routing:"; |
| 139 | router.debugPrint(); |
| 140 | } |
| 141 | |
| 142 | std::vector<const std::string*> getRoutes() |
| 143 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 144 | const std::string root(""); |
| 145 | return router.getRoutes(root); |
| 146 | } |
| 147 | std::vector<const std::string*> getRoutes(const std::string& parent) |
| 148 | { |
| 149 | return router.getRoutes(parent); |
| 150 | } |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 151 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 152 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 153 | self_t& sslFile(const std::string& crt_filename, |
| 154 | const std::string& key_filename) |
| 155 | { |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 156 | sslContext = std::make_shared<ssl_context_t>( |
| 157 | boost::asio::ssl::context::tls_server); |
| 158 | sslContext->set_verify_mode(boost::asio::ssl::verify_peer); |
| 159 | sslContext->use_certificate_file(crt_filename, ssl_context_t::pem); |
| 160 | sslContext->use_private_key_file(key_filename, ssl_context_t::pem); |
| 161 | sslContext->set_options(boost::asio::ssl::context::default_workarounds | |
| 162 | boost::asio::ssl::context::no_sslv2 | |
| 163 | boost::asio::ssl::context::no_sslv3 | |
| 164 | boost::asio::ssl::context::no_tlsv1 | |
| 165 | boost::asio::ssl::context::no_tlsv1_1); |
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 | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 169 | self_t& sslFile(const std::string& pem_filename) |
| 170 | { |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 171 | sslContext = std::make_shared<ssl_context_t>( |
| 172 | boost::asio::ssl::context::tls_server); |
| 173 | sslContext->set_verify_mode(boost::asio::ssl::verify_peer); |
| 174 | sslContext->load_verify_file(pem_filename); |
| 175 | sslContext->set_options(boost::asio::ssl::context::default_workarounds | |
| 176 | boost::asio::ssl::context::no_sslv2 | |
| 177 | boost::asio::ssl::context::no_sslv3 | |
| 178 | boost::asio::ssl::context::no_tlsv1 | |
| 179 | boost::asio::ssl::context::no_tlsv1_1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 180 | return *this; |
| 181 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 182 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 183 | self_t& ssl(std::shared_ptr<boost::asio::ssl::context>&& ctx) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 184 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 185 | sslContext = std::move(ctx); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 186 | BMCWEB_LOG_INFO << "app::ssl context use_count=" |
| 187 | << sslContext.use_count(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 188 | return *this; |
| 189 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 190 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 191 | std::shared_ptr<ssl_context_t> sslContext = nullptr; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 192 | |
| 193 | #else |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 194 | template <typename T, typename... Remain> |
| 195 | self_t& ssl_file(T&&, Remain&&...) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 196 | { |
| 197 | // We can't call .ssl() member function unless BMCWEB_ENABLE_SSL is |
| 198 | // defined. |
| 199 | static_assert( |
| 200 | // make static_assert dependent to T; always false |
| 201 | std::is_base_of<T, void>::value, |
| 202 | "Define BMCWEB_ENABLE_SSL to enable ssl support."); |
| 203 | return *this; |
| 204 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 205 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 206 | template <typename T> |
| 207 | self_t& ssl(T&&) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 208 | { |
| 209 | // We can't call .ssl() member function unless BMCWEB_ENABLE_SSL is |
| 210 | // defined. |
| 211 | static_assert( |
| 212 | // make static_assert dependent to T; always false |
| 213 | std::is_base_of<T, void>::value, |
| 214 | "Define BMCWEB_ENABLE_SSL to enable ssl support."); |
| 215 | return *this; |
| 216 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 217 | #endif |
| 218 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 219 | // middleware |
| 220 | using context_t = detail::Context<Middlewares...>; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 221 | template <typename T> |
| 222 | typename T::Context& getContext(const Request& req) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 223 | { |
| 224 | static_assert(black_magic::Contains<T, Middlewares...>::value, |
| 225 | "App doesn't have the specified middleware type."); |
| 226 | auto& ctx = *reinterpret_cast<context_t*>(req.middlewareContext); |
| 227 | return ctx.template get<T>(); |
| 228 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 229 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 230 | template <typename T> |
| 231 | T& getMiddleware() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 232 | { |
| 233 | return utility::getElementByType<T, Middlewares...>(middlewares); |
| 234 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 235 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 236 | template <typename Duration, typename Func> |
| 237 | self_t& tick(Duration d, Func f) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 238 | { |
| 239 | tickInterval = std::chrono::duration_cast<std::chrono::milliseconds>(d); |
| 240 | tickFunction = f; |
| 241 | return *this; |
| 242 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 243 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 244 | private: |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 245 | std::shared_ptr<asio::io_context> io; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 246 | #ifdef BMCWEB_ENABLE_SSL |
| 247 | uint16_t portUint = 443; |
| 248 | #else |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 249 | uint16_t portUint = 80; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 250 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 251 | std::string bindaddrStr = "::"; |
| 252 | int socketFd = -1; |
| 253 | Router router; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 254 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 255 | std::chrono::milliseconds tickInterval{}; |
| 256 | std::function<void()> tickFunction; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 257 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 258 | std::tuple<Middlewares...> middlewares; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 259 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 260 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 261 | std::unique_ptr<ssl_server_t> sslServer; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 262 | #else |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 263 | std::unique_ptr<server_t> server; |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 264 | #endif |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 265 | }; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 266 | template <typename... Middlewares> |
| 267 | using App = Crow<Middlewares...>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 268 | using SimpleApp = Crow<>; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 269 | } // namespace crow |