blob: 8dcec48e93d60fb05ec3430d614ae04a44bbcc83 [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
2
zhanghch058d1b46d2021-04-01 11:18:24 +08003#include "async_resp.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -07004#include "http_request.hpp"
5#include "http_server.hpp"
6#include "logging.hpp"
Tanousf00032d2018-11-05 01:18:10 -03007#include "privileges.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -07008#include "routing.hpp"
9#include "utility.hpp"
Tanousf00032d2018-11-05 01:18:10 -030010
Ed Tanous7045c8d2017-04-03 10:04:37 -070011#include <chrono>
12#include <cstdint>
13#include <functional>
14#include <future>
15#include <memory>
16#include <string>
Ed Tanous911ac312017-08-15 09:37:42 -070017#include <utility>
Ed Tanous1abe55e2018-09-05 08:30:59 -070018
Ed Tanous1abe55e2018-09-05 08:30:59 -070019#define BMCWEB_ROUTE(app, url) \
Ed Tanous988403c2020-08-24 11:29:49 -070020 app.template route<crow::black_magic::getParameterTag(url)>(url)
Ed Tanous7045c8d2017-04-03 10:04:37 -070021
Ed Tanous1abe55e2018-09-05 08:30:59 -070022namespace crow
23{
Ed Tanous55c7b7a2018-05-22 15:27:24 -070024#ifdef BMCWEB_ENABLE_SSL
Ed Tanous7045c8d2017-04-03 10:04:37 -070025using ssl_context_t = boost::asio::ssl::context;
26#endif
Ed Tanous52cc1122020-07-18 13:51:21 -070027class App
Ed Tanous1abe55e2018-09-05 08:30:59 -070028{
29 public:
Ed Tanous55c7b7a2018-05-22 15:27:24 -070030#ifdef BMCWEB_ENABLE_SSL
Ed Tanousceac6f72018-12-02 11:58:47 -080031 using ssl_socket_t = boost::beast::ssl_stream<boost::asio::ip::tcp::socket>;
Ed Tanous52cc1122020-07-18 13:51:21 -070032 using ssl_server_t = Server<App, ssl_socket_t>;
Ed Tanous789a6a32018-11-29 15:17:22 -080033#else
Ed Tanousceac6f72018-12-02 11:58:47 -080034 using socket_t = boost::asio::ip::tcp::socket;
Ed Tanous52cc1122020-07-18 13:51:21 -070035 using server_t = Server<App, socket_t>;
Ed Tanous7045c8d2017-04-03 10:04:37 -070036#endif
Ed Tanousceac6f72018-12-02 11:58:47 -080037
Ed Tanous52cc1122020-07-18 13:51:21 -070038 explicit App(std::shared_ptr<boost::asio::io_context> ioIn =
39 std::make_shared<boost::asio::io_context>()) :
Ed Tanous271584a2019-07-09 16:24:22 -070040 io(std::move(ioIn))
Gunnar Mills1214b7e2020-06-04 10:11:30 -050041 {}
Ed Tanous52cc1122020-07-18 13:51:21 -070042 ~App()
Ed Tanous1abe55e2018-09-05 08:30:59 -070043 {
44 this->stop();
45 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070046
Ed Tanousecd6a3a2022-01-07 09:18:40 -080047 App(const App&) = delete;
48 App(App&&) = delete;
49 App& operator=(const App&) = delete;
50 App& operator=(const App&&) = delete;
51
Ed Tanous1abe55e2018-09-05 08:30:59 -070052 template <typename Adaptor>
53 void handleUpgrade(const Request& req, Response& res, Adaptor&& adaptor)
54 {
Ed Tanousf94c4ec2022-01-06 12:44:41 -080055 router.handleUpgrade(req, res, std::forward<Adaptor>(adaptor));
Ed Tanous1abe55e2018-09-05 08:30:59 -070056 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070057
zhanghch058d1b46d2021-04-01 11:18:24 +080058 void handle(Request& req,
59 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -070060 {
zhanghch058d1b46d2021-04-01 11:18:24 +080061 router.handle(req, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -070062 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070063
Ed Tanous1abe55e2018-09-05 08:30:59 -070064 DynamicRule& routeDynamic(std::string&& rule)
65 {
66 return router.newRuleDynamic(rule);
67 }
68
Gunnar Mills1214b7e2020-06-04 10:11:30 -050069 template <uint64_t Tag>
70 auto& route(std::string&& rule)
Ed Tanous1abe55e2018-09-05 08:30:59 -070071 {
72 return router.newRuleTagged<Tag>(std::move(rule));
73 }
74
Ed Tanous81ce6092020-12-17 16:54:55 +000075 App& socket(int existingSocket)
Ed Tanous1abe55e2018-09-05 08:30:59 -070076 {
Ed Tanous81ce6092020-12-17 16:54:55 +000077 socketFd = existingSocket;
Ed Tanous1abe55e2018-09-05 08:30:59 -070078 return *this;
79 }
80
Ed Tanousb74e4402020-09-09 20:26:26 -070081 App& port(std::uint16_t port)
Ed Tanous1abe55e2018-09-05 08:30:59 -070082 {
83 portUint = port;
84 return *this;
85 }
86
Ed Tanousb74e4402020-09-09 20:26:26 -070087 App& bindaddr(std::string bindaddr)
Ed Tanous1abe55e2018-09-05 08:30:59 -070088 {
Ed Tanousb5a76932020-09-29 16:16:58 -070089 bindaddrStr = std::move(bindaddr);
Ed Tanous1abe55e2018-09-05 08:30:59 -070090 return *this;
91 }
92
93 void validate()
94 {
95 router.validate();
96 }
97
98 void run()
99 {
100 validate();
101#ifdef BMCWEB_ENABLE_SSL
Ed Tanous789a6a32018-11-29 15:17:22 -0800102 if (-1 == socketFd)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103 {
Ed Tanous23a21a12020-07-25 04:45:05 +0000104 sslServer = std::make_unique<ssl_server_t>(
105 this, bindaddrStr, portUint, sslContext, io);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700106 }
107 else
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108 {
Ed Tanous23a21a12020-07-25 04:45:05 +0000109 sslServer =
110 std::make_unique<ssl_server_t>(this, socketFd, sslContext, io);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111 }
Ed Tanous789a6a32018-11-29 15:17:22 -0800112 sslServer->run();
113
114#else
115
116 if (-1 == socketFd)
117 {
118 server = std::move(std::make_unique<server_t>(
Ed Tanous52cc1122020-07-18 13:51:21 -0700119 this, bindaddrStr, portUint, nullptr, io));
Ed Tanous789a6a32018-11-29 15:17:22 -0800120 }
121 else
122 {
Ed Tanous52cc1122020-07-18 13:51:21 -0700123 server = std::move(
124 std::make_unique<server_t>(this, socketFd, nullptr, io));
Ed Tanous789a6a32018-11-29 15:17:22 -0800125 }
Ed Tanous789a6a32018-11-29 15:17:22 -0800126 server->run();
127
128#endif
Ed Tanous1abe55e2018-09-05 08:30:59 -0700129 }
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 Tanous1abe55e2018-09-05 08:30:59 -0700144 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 Tanousb4a7bfa2017-04-04 17:23:00 -0700151
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700152#ifdef BMCWEB_ENABLE_SSL
Ed Tanous81ce6092020-12-17 16:54:55 +0000153 App& sslFile(const std::string& crtFilename, const std::string& keyFilename)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700154 {
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600155 sslContext = std::make_shared<ssl_context_t>(
156 boost::asio::ssl::context::tls_server);
157 sslContext->set_verify_mode(boost::asio::ssl::verify_peer);
Ed Tanous81ce6092020-12-17 16:54:55 +0000158 sslContext->use_certificate_file(crtFilename, ssl_context_t::pem);
159 sslContext->use_private_key_file(keyFilename, ssl_context_t::pem);
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600160 sslContext->set_options(boost::asio::ssl::context::default_workarounds |
161 boost::asio::ssl::context::no_sslv2 |
162 boost::asio::ssl::context::no_sslv3 |
163 boost::asio::ssl::context::no_tlsv1 |
164 boost::asio::ssl::context::no_tlsv1_1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700165 return *this;
166 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700167
Ed Tanous81ce6092020-12-17 16:54:55 +0000168 App& sslFile(const std::string& pemFilename)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700169 {
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600170 sslContext = std::make_shared<ssl_context_t>(
171 boost::asio::ssl::context::tls_server);
172 sslContext->set_verify_mode(boost::asio::ssl::verify_peer);
Ed Tanous81ce6092020-12-17 16:54:55 +0000173 sslContext->load_verify_file(pemFilename);
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600174 sslContext->set_options(boost::asio::ssl::context::default_workarounds |
175 boost::asio::ssl::context::no_sslv2 |
176 boost::asio::ssl::context::no_sslv3 |
177 boost::asio::ssl::context::no_tlsv1 |
178 boost::asio::ssl::context::no_tlsv1_1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700179 return *this;
180 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700181
Ed Tanousb74e4402020-09-09 20:26:26 -0700182 App& ssl(std::shared_ptr<boost::asio::ssl::context>&& ctx)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700183 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700184 sslContext = std::move(ctx);
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600185 BMCWEB_LOG_INFO << "app::ssl context use_count="
186 << sslContext.use_count();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700187 return *this;
188 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700189
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600190 std::shared_ptr<ssl_context_t> sslContext = nullptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700191
192#else
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500193 template <typename T, typename... Remain>
Ed Tanousb74e4402020-09-09 20:26:26 -0700194 App& ssl_file(T&&, Remain&&...)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700195 {
196 // We can't call .ssl() member function unless BMCWEB_ENABLE_SSL is
197 // defined.
198 static_assert(
199 // make static_assert dependent to T; always false
200 std::is_base_of<T, void>::value,
201 "Define BMCWEB_ENABLE_SSL to enable ssl support.");
202 return *this;
203 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700204
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500205 template <typename T>
Ed Tanousb74e4402020-09-09 20:26:26 -0700206 App& ssl(T&&)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700207 {
208 // We can't call .ssl() member function unless BMCWEB_ENABLE_SSL is
209 // defined.
210 static_assert(
211 // make static_assert dependent to T; always false
212 std::is_base_of<T, void>::value,
213 "Define BMCWEB_ENABLE_SSL to enable ssl support.");
214 return *this;
215 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700216#endif
217
Ed Tanous1abe55e2018-09-05 08:30:59 -0700218 private:
Ed Tanous23e64202020-09-15 19:21:30 -0700219 std::shared_ptr<boost::asio::io_context> io;
Ed Tanous789a6a32018-11-29 15:17:22 -0800220#ifdef BMCWEB_ENABLE_SSL
221 uint16_t portUint = 443;
222#else
Ed Tanous1abe55e2018-09-05 08:30:59 -0700223 uint16_t portUint = 80;
Ed Tanous789a6a32018-11-29 15:17:22 -0800224#endif
Ed Tanousc7b9cb32021-02-11 13:28:35 -0800225 std::string bindaddrStr = "0.0.0.0";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700226 int socketFd = -1;
227 Router router;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700228
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700229#ifdef BMCWEB_ENABLE_SSL
Ed Tanous1abe55e2018-09-05 08:30:59 -0700230 std::unique_ptr<ssl_server_t> sslServer;
Ed Tanous789a6a32018-11-29 15:17:22 -0800231#else
Ed Tanous1abe55e2018-09-05 08:30:59 -0700232 std::unique_ptr<server_t> server;
Ed Tanous789a6a32018-11-29 15:17:22 -0800233#endif
Ed Tanous7045c8d2017-04-03 10:04:37 -0700234};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700235} // namespace crow
Ed Tanous52cc1122020-07-18 13:51:21 -0700236using App = crow::App;