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