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 | 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 | |
RAJESWARAN THILLAIGOVINDAN | 61dbeef | 2019-12-13 04:26:54 -0600 | [diff] [blame] | 53 | void handle(Request& req, Response& res) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 54 | { |
| 55 | router.handle(req, res); |
| 56 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 57 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 58 | DynamicRule& routeDynamic(std::string&& rule) |
| 59 | { |
| 60 | return router.newRuleDynamic(rule); |
| 61 | } |
| 62 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 63 | template <uint64_t Tag> |
| 64 | auto& route(std::string&& rule) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 65 | { |
| 66 | return router.newRuleTagged<Tag>(std::move(rule)); |
| 67 | } |
| 68 | |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame^] | 69 | App& socket(int existing_socket) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 70 | { |
| 71 | socketFd = existing_socket; |
| 72 | return *this; |
| 73 | } |
| 74 | |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame^] | 75 | App& port(std::uint16_t port) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 76 | { |
| 77 | portUint = port; |
| 78 | return *this; |
| 79 | } |
| 80 | |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame^] | 81 | App& bindaddr(std::string bindaddr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 82 | { |
| 83 | bindaddrStr = bindaddr; |
| 84 | return *this; |
| 85 | } |
| 86 | |
| 87 | void validate() |
| 88 | { |
| 89 | router.validate(); |
| 90 | } |
| 91 | |
| 92 | void run() |
| 93 | { |
| 94 | validate(); |
| 95 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 96 | if (-1 == socketFd) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 97 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 98 | sslServer = std::make_unique<ssl_server_t>( |
| 99 | this, bindaddrStr, portUint, sslContext, io); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 100 | } |
| 101 | else |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 102 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 103 | sslServer = |
| 104 | std::make_unique<ssl_server_t>(this, socketFd, sslContext, io); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 105 | } |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 106 | sslServer->run(); |
| 107 | |
| 108 | #else |
| 109 | |
| 110 | if (-1 == socketFd) |
| 111 | { |
| 112 | server = std::move(std::make_unique<server_t>( |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 113 | this, bindaddrStr, portUint, nullptr, io)); |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 114 | } |
| 115 | else |
| 116 | { |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 117 | server = std::move( |
| 118 | std::make_unique<server_t>(this, socketFd, nullptr, io)); |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 119 | } |
Ed Tanous | 789a6a3 | 2018-11-29 15:17:22 -0800 | [diff] [blame] | 120 | server->run(); |
| 121 | |
| 122 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void stop() |
| 126 | { |
| 127 | io->stop(); |
| 128 | } |
| 129 | |
| 130 | void debugPrint() |
| 131 | { |
| 132 | BMCWEB_LOG_DEBUG << "Routing:"; |
| 133 | router.debugPrint(); |
| 134 | } |
| 135 | |
| 136 | std::vector<const std::string*> getRoutes() |
| 137 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 138 | const std::string root(""); |
| 139 | return router.getRoutes(root); |
| 140 | } |
| 141 | std::vector<const std::string*> getRoutes(const std::string& parent) |
| 142 | { |
| 143 | return router.getRoutes(parent); |
| 144 | } |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 145 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 146 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame^] | 147 | App& sslFile(const std::string& crt_filename, |
| 148 | const std::string& key_filename) |
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); |
| 153 | sslContext->use_certificate_file(crt_filename, ssl_context_t::pem); |
| 154 | sslContext->use_private_key_file(key_filename, ssl_context_t::pem); |
| 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 | b74e440 | 2020-09-09 20:26:26 -0700 | [diff] [blame^] | 163 | App& sslFile(const std::string& pem_filename) |
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); |
| 168 | sslContext->load_verify_file(pem_filename); |
| 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 | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 214 | std::shared_ptr<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 | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 220 | std::string bindaddrStr = "::"; |
| 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; |