blob: 9a4eb20a0477b025cce8d0fb91ba99bc5900797d [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
2
Ed Tanous04e438c2020-10-03 08:06:26 -07003#include "http_request.hpp"
4#include "http_server.hpp"
5#include "logging.hpp"
Tanousf00032d2018-11-05 01:18:10 -03006#include "privileges.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -07007#include "routing.hpp"
8#include "utility.hpp"
Tanousf00032d2018-11-05 01:18:10 -03009
Ed Tanous7045c8d2017-04-03 10:04:37 -070010#include <chrono>
11#include <cstdint>
12#include <functional>
13#include <future>
14#include <memory>
15#include <string>
Ed Tanous911ac312017-08-15 09:37:42 -070016#include <utility>
Ed Tanous1abe55e2018-09-05 08:30:59 -070017
Ed Tanous1abe55e2018-09-05 08:30:59 -070018#define BMCWEB_ROUTE(app, url) \
Ed Tanous988403c2020-08-24 11:29:49 -070019 app.template route<crow::black_magic::getParameterTag(url)>(url)
Ed Tanous7045c8d2017-04-03 10:04:37 -070020
Ed Tanous1abe55e2018-09-05 08:30:59 -070021namespace crow
22{
Ed Tanous55c7b7a2018-05-22 15:27:24 -070023#ifdef BMCWEB_ENABLE_SSL
Ed Tanous7045c8d2017-04-03 10:04:37 -070024using ssl_context_t = boost::asio::ssl::context;
25#endif
Ed Tanous52cc1122020-07-18 13:51:21 -070026class App
Ed Tanous1abe55e2018-09-05 08:30:59 -070027{
28 public:
Ed Tanous55c7b7a2018-05-22 15:27:24 -070029#ifdef BMCWEB_ENABLE_SSL
Ed Tanousceac6f72018-12-02 11:58:47 -080030 using ssl_socket_t = boost::beast::ssl_stream<boost::asio::ip::tcp::socket>;
Ed Tanous52cc1122020-07-18 13:51:21 -070031 using ssl_server_t = Server<App, ssl_socket_t>;
Ed Tanous789a6a32018-11-29 15:17:22 -080032#else
Ed Tanousceac6f72018-12-02 11:58:47 -080033 using socket_t = boost::asio::ip::tcp::socket;
Ed Tanous52cc1122020-07-18 13:51:21 -070034 using server_t = Server<App, socket_t>;
Ed Tanous7045c8d2017-04-03 10:04:37 -070035#endif
Ed Tanousceac6f72018-12-02 11:58:47 -080036
Ed Tanous52cc1122020-07-18 13:51:21 -070037 explicit App(std::shared_ptr<boost::asio::io_context> ioIn =
38 std::make_shared<boost::asio::io_context>()) :
Ed Tanous271584a2019-07-09 16:24:22 -070039 io(std::move(ioIn))
Gunnar Mills1214b7e2020-06-04 10:11:30 -050040 {}
Ed Tanous52cc1122020-07-18 13:51:21 -070041 ~App()
Ed Tanous1abe55e2018-09-05 08:30:59 -070042 {
43 this->stop();
44 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070045
Ed Tanous1abe55e2018-09-05 08:30:59 -070046 template <typename Adaptor>
47 void handleUpgrade(const Request& req, Response& res, Adaptor&& adaptor)
48 {
Ed Tanousceac6f72018-12-02 11:58:47 -080049 router.handleUpgrade(req, res, std::move(adaptor));
Ed Tanous1abe55e2018-09-05 08:30:59 -070050 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070051
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -060052 void handle(Request& req, Response& res)
Ed Tanous1abe55e2018-09-05 08:30:59 -070053 {
54 router.handle(req, res);
55 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070056
Ed Tanous1abe55e2018-09-05 08:30:59 -070057 DynamicRule& routeDynamic(std::string&& rule)
58 {
59 return router.newRuleDynamic(rule);
60 }
61
Gunnar Mills1214b7e2020-06-04 10:11:30 -050062 template <uint64_t Tag>
63 auto& route(std::string&& rule)
Ed Tanous1abe55e2018-09-05 08:30:59 -070064 {
65 return router.newRuleTagged<Tag>(std::move(rule));
66 }
67
Ed Tanousb74e4402020-09-09 20:26:26 -070068 App& socket(int existing_socket)
Ed Tanous1abe55e2018-09-05 08:30:59 -070069 {
70 socketFd = existing_socket;
71 return *this;
72 }
73
Ed Tanousb74e4402020-09-09 20:26:26 -070074 App& port(std::uint16_t port)
Ed Tanous1abe55e2018-09-05 08:30:59 -070075 {
76 portUint = port;
77 return *this;
78 }
79
Ed Tanousb74e4402020-09-09 20:26:26 -070080 App& bindaddr(std::string bindaddr)
Ed Tanous1abe55e2018-09-05 08:30:59 -070081 {
Ed Tanousb5a76932020-09-29 16:16:58 -070082 bindaddrStr = std::move(bindaddr);
Ed Tanous1abe55e2018-09-05 08:30:59 -070083 return *this;
84 }
85
86 void validate()
87 {
88 router.validate();
89 }
90
91 void run()
92 {
93 validate();
94#ifdef BMCWEB_ENABLE_SSL
Ed Tanous789a6a32018-11-29 15:17:22 -080095 if (-1 == socketFd)
Ed Tanous1abe55e2018-09-05 08:30:59 -070096 {
Ed Tanous23a21a12020-07-25 04:45:05 +000097 sslServer = std::make_unique<ssl_server_t>(
98 this, bindaddrStr, portUint, sslContext, io);
Ed Tanous1abe55e2018-09-05 08:30:59 -070099 }
100 else
Ed Tanous1abe55e2018-09-05 08:30:59 -0700101 {
Ed Tanous23a21a12020-07-25 04:45:05 +0000102 sslServer =
103 std::make_unique<ssl_server_t>(this, socketFd, sslContext, io);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104 }
Ed Tanous789a6a32018-11-29 15:17:22 -0800105 sslServer->run();
106
107#else
108
109 if (-1 == socketFd)
110 {
111 server = std::move(std::make_unique<server_t>(
Ed Tanous52cc1122020-07-18 13:51:21 -0700112 this, bindaddrStr, portUint, nullptr, io));
Ed Tanous789a6a32018-11-29 15:17:22 -0800113 }
114 else
115 {
Ed Tanous52cc1122020-07-18 13:51:21 -0700116 server = std::move(
117 std::make_unique<server_t>(this, socketFd, nullptr, io));
Ed Tanous789a6a32018-11-29 15:17:22 -0800118 }
Ed Tanous789a6a32018-11-29 15:17:22 -0800119 server->run();
120
121#endif
Ed Tanous1abe55e2018-09-05 08:30:59 -0700122 }
123
124 void stop()
125 {
126 io->stop();
127 }
128
129 void debugPrint()
130 {
131 BMCWEB_LOG_DEBUG << "Routing:";
132 router.debugPrint();
133 }
134
135 std::vector<const std::string*> getRoutes()
136 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700137 const std::string root("");
138 return router.getRoutes(root);
139 }
140 std::vector<const std::string*> getRoutes(const std::string& parent)
141 {
142 return router.getRoutes(parent);
143 }
Ed Tanousb4a7bfa2017-04-04 17:23:00 -0700144
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700145#ifdef BMCWEB_ENABLE_SSL
Ed Tanousb74e4402020-09-09 20:26:26 -0700146 App& sslFile(const std::string& crt_filename,
147 const std::string& key_filename)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700148 {
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600149 sslContext = std::make_shared<ssl_context_t>(
150 boost::asio::ssl::context::tls_server);
151 sslContext->set_verify_mode(boost::asio::ssl::verify_peer);
152 sslContext->use_certificate_file(crt_filename, ssl_context_t::pem);
153 sslContext->use_private_key_file(key_filename, ssl_context_t::pem);
154 sslContext->set_options(boost::asio::ssl::context::default_workarounds |
155 boost::asio::ssl::context::no_sslv2 |
156 boost::asio::ssl::context::no_sslv3 |
157 boost::asio::ssl::context::no_tlsv1 |
158 boost::asio::ssl::context::no_tlsv1_1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700159 return *this;
160 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700161
Ed Tanousb74e4402020-09-09 20:26:26 -0700162 App& sslFile(const std::string& pem_filename)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700163 {
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600164 sslContext = std::make_shared<ssl_context_t>(
165 boost::asio::ssl::context::tls_server);
166 sslContext->set_verify_mode(boost::asio::ssl::verify_peer);
167 sslContext->load_verify_file(pem_filename);
168 sslContext->set_options(boost::asio::ssl::context::default_workarounds |
169 boost::asio::ssl::context::no_sslv2 |
170 boost::asio::ssl::context::no_sslv3 |
171 boost::asio::ssl::context::no_tlsv1 |
172 boost::asio::ssl::context::no_tlsv1_1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700173 return *this;
174 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700175
Ed Tanousb74e4402020-09-09 20:26:26 -0700176 App& ssl(std::shared_ptr<boost::asio::ssl::context>&& ctx)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700177 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700178 sslContext = std::move(ctx);
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600179 BMCWEB_LOG_INFO << "app::ssl context use_count="
180 << sslContext.use_count();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700181 return *this;
182 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700183
Marri Devender Rao5968cae2019-01-21 10:27:12 -0600184 std::shared_ptr<ssl_context_t> sslContext = nullptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700185
186#else
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500187 template <typename T, typename... Remain>
Ed Tanousb74e4402020-09-09 20:26:26 -0700188 App& ssl_file(T&&, Remain&&...)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700189 {
190 // We can't call .ssl() member function unless BMCWEB_ENABLE_SSL is
191 // defined.
192 static_assert(
193 // make static_assert dependent to T; always false
194 std::is_base_of<T, void>::value,
195 "Define BMCWEB_ENABLE_SSL to enable ssl support.");
196 return *this;
197 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700198
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500199 template <typename T>
Ed Tanousb74e4402020-09-09 20:26:26 -0700200 App& ssl(T&&)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700201 {
202 // We can't call .ssl() member function unless BMCWEB_ENABLE_SSL is
203 // defined.
204 static_assert(
205 // make static_assert dependent to T; always false
206 std::is_base_of<T, void>::value,
207 "Define BMCWEB_ENABLE_SSL to enable ssl support.");
208 return *this;
209 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700210#endif
211
Ed Tanous1abe55e2018-09-05 08:30:59 -0700212 private:
Ed Tanous23e64202020-09-15 19:21:30 -0700213 std::shared_ptr<boost::asio::io_context> io;
Ed Tanous789a6a32018-11-29 15:17:22 -0800214#ifdef BMCWEB_ENABLE_SSL
215 uint16_t portUint = 443;
216#else
Ed Tanous1abe55e2018-09-05 08:30:59 -0700217 uint16_t portUint = 80;
Ed Tanous789a6a32018-11-29 15:17:22 -0800218#endif
Ed Tanous1abe55e2018-09-05 08:30:59 -0700219 std::string bindaddrStr = "::";
220 int socketFd = -1;
221 Router router;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700222
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700223#ifdef BMCWEB_ENABLE_SSL
Ed Tanous1abe55e2018-09-05 08:30:59 -0700224 std::unique_ptr<ssl_server_t> sslServer;
Ed Tanous789a6a32018-11-29 15:17:22 -0800225#else
Ed Tanous1abe55e2018-09-05 08:30:59 -0700226 std::unique_ptr<server_t> server;
Ed Tanous789a6a32018-11-29 15:17:22 -0800227#endif
Ed Tanous7045c8d2017-04-03 10:04:37 -0700228};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700229} // namespace crow
Ed Tanous52cc1122020-07-18 13:51:21 -0700230using App = crow::App;