blob: f9826189bc653243bda60fa2a9a9e7dd184aca54 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous7045c8d2017-04-03 10:04:37 -07003#pragma once
4
Ed Tanousd7857202025-01-28 15:32:26 -08005#include "bmcweb_config.h"
6
zhanghch058d1b46d2021-04-01 11:18:24 +08007#include "async_resp.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -07008#include "http_request.hpp"
9#include "http_server.hpp"
10#include "logging.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -070011#include "routing.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080012#include "routing/dynamicrule.hpp"
Tanousf00032d2018-11-05 01:18:10 -030013
Ed Tanousd7857202025-01-28 15:32:26 -080014#include <sys/socket.h>
Ed Tanous8db83742024-04-13 09:11:15 -070015#include <systemd/sd-daemon.h>
16
Nan Zhoucec58fe2022-06-14 20:45:45 +000017#include <boost/asio/io_context.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080018#include <boost/asio/ip/address.hpp>
Nan Zhoucec58fe2022-06-14 20:45:45 +000019#include <boost/asio/ip/tcp.hpp>
Nan Zhoucec58fe2022-06-14 20:45:45 +000020#include <boost/asio/ssl/context.hpp>
Ed Tanous003301a2024-04-16 09:59:19 -070021#include <boost/asio/ssl/stream.hpp>
Nan Zhoucec58fe2022-06-14 20:45:45 +000022
Ed Tanous7045c8d2017-04-03 10:04:37 -070023#include <cstdint>
Ed Tanous7045c8d2017-04-03 10:04:37 -070024#include <memory>
Ed Tanousd7857202025-01-28 15:32:26 -080025#include <optional>
Ed Tanous7045c8d2017-04-03 10:04:37 -070026#include <string>
Ed Tanousd7857202025-01-28 15:32:26 -080027#include <type_traits>
Ed Tanous911ac312017-08-15 09:37:42 -070028#include <utility>
Ed Tanousd7857202025-01-28 15:32:26 -080029#include <vector>
Ed Tanous1abe55e2018-09-05 08:30:59 -070030
Patrick Williamsa2323432023-05-12 10:06:35 -050031// NOLINTNEXTLINE(cppcoreguidelines-macro-usage, clang-diagnostic-unused-macros)
Ed Tanous1abe55e2018-09-05 08:30:59 -070032#define BMCWEB_ROUTE(app, url) \
Ed Tanous47488a92023-06-26 18:19:33 -070033 app.template route<crow::utility::getParameterTag(url)>(url)
Ed Tanous7045c8d2017-04-03 10:04:37 -070034
Ed Tanous1abe55e2018-09-05 08:30:59 -070035namespace crow
36{
Ed Tanous52cc1122020-07-18 13:51:21 -070037class App
Ed Tanous1abe55e2018-09-05 08:30:59 -070038{
39 public:
Ed Tanous003301a2024-04-16 09:59:19 -070040 using ssl_socket_t = boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
Ed Tanous8db83742024-04-13 09:11:15 -070041 using raw_socket_t = boost::asio::ip::tcp::socket;
42
Ed Tanous25b54db2024-04-17 15:40:31 -070043 using socket_type = std::conditional_t<BMCWEB_INSECURE_DISABLE_SSL,
44 raw_socket_t, ssl_socket_t>;
Ed Tanous8db83742024-04-13 09:11:15 -070045 using server_type = Server<App, socket_type>;
Ed Tanousceac6f72018-12-02 11:58:47 -080046
Ed Tanous52cc1122020-07-18 13:51:21 -070047 explicit App(std::shared_ptr<boost::asio::io_context> ioIn =
48 std::make_shared<boost::asio::io_context>()) :
Ed Tanous271584a2019-07-09 16:24:22 -070049 io(std::move(ioIn))
Gunnar Mills1214b7e2020-06-04 10:11:30 -050050 {}
Ed Tanousecd6a3a2022-01-07 09:18:40 -080051
Ed Tanous1abe55e2018-09-05 08:30:59 -070052 template <typename Adaptor>
Jonathan Doman102a4cd2024-04-15 16:56:23 -070053 void handleUpgrade(const std::shared_ptr<Request>& req,
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +053054 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
55 Adaptor&& adaptor)
Ed Tanous1abe55e2018-09-05 08:30:59 -070056 {
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +053057 router.handleUpgrade(req, asyncResp, std::forward<Adaptor>(adaptor));
Ed Tanous1abe55e2018-09-05 08:30:59 -070058 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070059
Jonathan Doman102a4cd2024-04-15 16:56:23 -070060 void handle(const std::shared_ptr<Request>& req,
zhanghch058d1b46d2021-04-01 11:18:24 +080061 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -070062 {
zhanghch058d1b46d2021-04-01 11:18:24 +080063 router.handle(req, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -070064 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070065
Ed Tanous8cb2c022024-03-27 16:31:46 -070066 DynamicRule& routeDynamic(const std::string& rule)
Ed Tanous1abe55e2018-09-05 08:30:59 -070067 {
68 return router.newRuleDynamic(rule);
69 }
70
Gunnar Mills1214b7e2020-06-04 10:11:30 -050071 template <uint64_t Tag>
72 auto& route(std::string&& rule)
Ed Tanous1abe55e2018-09-05 08:30:59 -070073 {
74 return router.newRuleTagged<Tag>(std::move(rule));
75 }
76
Ed Tanous1abe55e2018-09-05 08:30:59 -070077 void validate()
78 {
79 router.validate();
80 }
81
Ed Tanous3281bcf2024-06-25 16:02:05 -070082 void loadCertificate()
83 {
84 BMCWEB_LOG_DEBUG("Loading certificate");
85 if (!server)
86 {
87 return;
88 }
89 server->loadCertificate();
90 }
91
Ed Tanous8db83742024-04-13 09:11:15 -070092 std::optional<boost::asio::ip::tcp::acceptor> setupSocket()
93 {
94 if (io == nullptr)
95 {
96 BMCWEB_LOG_CRITICAL("IO was nullptr?");
97 return std::nullopt;
98 }
99 constexpr int defaultPort = 18080;
Ed Tanous38afdb92024-12-11 23:57:53 -0800100 if (sd_listen_fds(0) == 1)
Ed Tanous8db83742024-04-13 09:11:15 -0700101 {
102 BMCWEB_LOG_INFO("attempting systemd socket activation");
103 if (sd_is_socket_inet(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_STREAM,
104 1, 0) != 0)
105 {
106 BMCWEB_LOG_INFO("Starting webserver on socket handle {}",
107 SD_LISTEN_FDS_START);
108 return boost::asio::ip::tcp::acceptor(
109 *io, boost::asio::ip::tcp::v6(), SD_LISTEN_FDS_START);
110 }
111 BMCWEB_LOG_ERROR(
112 "bad incoming socket, starting webserver on port {}",
113 defaultPort);
114 }
115 BMCWEB_LOG_INFO("Starting webserver on port {}", defaultPort);
116 return boost::asio::ip::tcp::acceptor(
117 *io, boost::asio::ip::tcp::endpoint(
118 boost::asio::ip::make_address("0.0.0.0"), defaultPort));
119 }
120
Ed Tanous1abe55e2018-09-05 08:30:59 -0700121 void run()
122 {
123 validate();
Ed Tanous789a6a32018-11-29 15:17:22 -0800124
Ed Tanous8db83742024-04-13 09:11:15 -0700125 std::optional<boost::asio::ip::tcp::acceptor> acceptor = setupSocket();
126 if (!acceptor)
Ed Tanous789a6a32018-11-29 15:17:22 -0800127 {
Ed Tanous8db83742024-04-13 09:11:15 -0700128 BMCWEB_LOG_CRITICAL("Couldn't start server");
129 return;
Ed Tanous789a6a32018-11-29 15:17:22 -0800130 }
Ed Tanous8db83742024-04-13 09:11:15 -0700131 server.emplace(this, std::move(*acceptor), sslContext, io);
Ed Tanous789a6a32018-11-29 15:17:22 -0800132 server->run();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700133 }
134
Ed Tanous1abe55e2018-09-05 08:30:59 -0700135 void debugPrint()
136 {
Ed Tanous62598e32023-07-17 17:06:25 -0700137 BMCWEB_LOG_DEBUG("Routing:");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700138 router.debugPrint();
139 }
140
141 std::vector<const std::string*> getRoutes()
142 {
Ed Tanouse05aec52022-01-25 10:28:56 -0800143 const std::string root;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700144 return router.getRoutes(root);
145 }
146 std::vector<const std::string*> getRoutes(const std::string& parent)
147 {
148 return router.getRoutes(parent);
149 }
Ed Tanousb4a7bfa2017-04-04 17:23:00 -0700150
Ed Tanousb74e4402020-09-09 20:26:26 -0700151 App& ssl(std::shared_ptr<boost::asio::ssl::context>&& ctx)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700152 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700153 sslContext = std::move(ctx);
Ed Tanous62598e32023-07-17 17:06:25 -0700154 BMCWEB_LOG_INFO("app::ssl context use_count={}",
155 sslContext.use_count());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700156 return *this;
157 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700158
Ed Tanous4fa45df2023-09-01 14:20:50 -0700159 std::shared_ptr<boost::asio::ssl::context> sslContext = nullptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700160
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700161 boost::asio::io_context& ioContext()
162 {
163 return *io;
164 }
165
Ed Tanous1abe55e2018-09-05 08:30:59 -0700166 private:
Ed Tanous23e64202020-09-15 19:21:30 -0700167 std::shared_ptr<boost::asio::io_context> io;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700168
Ed Tanous8db83742024-04-13 09:11:15 -0700169 std::optional<server_type> server;
170
171 Router router;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700172};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700173} // namespace crow
Ed Tanous52cc1122020-07-18 13:51:21 -0700174using App = crow::App;