blob: 68f1642017ea264361a80c642c1ee722f0481f10 [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
zhanghch058d1b46d2021-04-01 11:18:24 +08005#include "async_resp.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -07006#include "http_request.hpp"
7#include "http_server.hpp"
8#include "logging.hpp"
Tanousf00032d2018-11-05 01:18:10 -03009#include "privileges.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -070010#include "routing.hpp"
11#include "utility.hpp"
Tanousf00032d2018-11-05 01:18:10 -030012
Ed Tanous8db83742024-04-13 09:11:15 -070013#include <systemd/sd-daemon.h>
14
Nan Zhoucec58fe2022-06-14 20:45:45 +000015#include <boost/asio/io_context.hpp>
16#include <boost/asio/ip/tcp.hpp>
Nan Zhoucec58fe2022-06-14 20:45:45 +000017#include <boost/asio/ssl/context.hpp>
Ed Tanous003301a2024-04-16 09:59:19 -070018#include <boost/asio/ssl/stream.hpp>
Nan Zhoucec58fe2022-06-14 20:45:45 +000019
Ed Tanous7045c8d2017-04-03 10:04:37 -070020#include <chrono>
21#include <cstdint>
22#include <functional>
23#include <future>
24#include <memory>
25#include <string>
Ed Tanous911ac312017-08-15 09:37:42 -070026#include <utility>
Ed Tanous1abe55e2018-09-05 08:30:59 -070027
Patrick Williamsa2323432023-05-12 10:06:35 -050028// NOLINTNEXTLINE(cppcoreguidelines-macro-usage, clang-diagnostic-unused-macros)
Ed Tanous1abe55e2018-09-05 08:30:59 -070029#define BMCWEB_ROUTE(app, url) \
Ed Tanous47488a92023-06-26 18:19:33 -070030 app.template route<crow::utility::getParameterTag(url)>(url)
Ed Tanous7045c8d2017-04-03 10:04:37 -070031
Ed Tanous1abe55e2018-09-05 08:30:59 -070032namespace crow
33{
Ed Tanous52cc1122020-07-18 13:51:21 -070034class App
Ed Tanous1abe55e2018-09-05 08:30:59 -070035{
36 public:
Ed Tanous003301a2024-04-16 09:59:19 -070037 using ssl_socket_t = boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
Ed Tanous8db83742024-04-13 09:11:15 -070038 using raw_socket_t = boost::asio::ip::tcp::socket;
39
Ed Tanous25b54db2024-04-17 15:40:31 -070040 using socket_type = std::conditional_t<BMCWEB_INSECURE_DISABLE_SSL,
41 raw_socket_t, ssl_socket_t>;
Ed Tanous8db83742024-04-13 09:11:15 -070042 using server_type = Server<App, socket_type>;
Ed Tanousceac6f72018-12-02 11:58:47 -080043
Ed Tanous52cc1122020-07-18 13:51:21 -070044 explicit App(std::shared_ptr<boost::asio::io_context> ioIn =
45 std::make_shared<boost::asio::io_context>()) :
Ed Tanous271584a2019-07-09 16:24:22 -070046 io(std::move(ioIn))
Gunnar Mills1214b7e2020-06-04 10:11:30 -050047 {}
Ed Tanousecd6a3a2022-01-07 09:18:40 -080048
Ed Tanous1abe55e2018-09-05 08:30:59 -070049 template <typename Adaptor>
Jonathan Doman102a4cd2024-04-15 16:56:23 -070050 void handleUpgrade(const std::shared_ptr<Request>& req,
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +053051 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
52 Adaptor&& adaptor)
Ed Tanous1abe55e2018-09-05 08:30:59 -070053 {
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +053054 router.handleUpgrade(req, asyncResp, std::forward<Adaptor>(adaptor));
Ed Tanous1abe55e2018-09-05 08:30:59 -070055 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070056
Jonathan Doman102a4cd2024-04-15 16:56:23 -070057 void handle(const std::shared_ptr<Request>& req,
zhanghch058d1b46d2021-04-01 11:18:24 +080058 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -070059 {
zhanghch058d1b46d2021-04-01 11:18:24 +080060 router.handle(req, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -070061 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070062
Ed Tanous8cb2c022024-03-27 16:31:46 -070063 DynamicRule& routeDynamic(const std::string& rule)
Ed Tanous1abe55e2018-09-05 08:30:59 -070064 {
65 return router.newRuleDynamic(rule);
66 }
67
Gunnar Mills1214b7e2020-06-04 10:11:30 -050068 template <uint64_t Tag>
69 auto& route(std::string&& rule)
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 {
71 return router.newRuleTagged<Tag>(std::move(rule));
72 }
73
Ed Tanous1abe55e2018-09-05 08:30:59 -070074 void validate()
75 {
76 router.validate();
77 }
78
Ed Tanous3281bcf2024-06-25 16:02:05 -070079 void loadCertificate()
80 {
81 BMCWEB_LOG_DEBUG("Loading certificate");
82 if (!server)
83 {
84 return;
85 }
86 server->loadCertificate();
87 }
88
Ed Tanous8db83742024-04-13 09:11:15 -070089 std::optional<boost::asio::ip::tcp::acceptor> setupSocket()
90 {
91 if (io == nullptr)
92 {
93 BMCWEB_LOG_CRITICAL("IO was nullptr?");
94 return std::nullopt;
95 }
96 constexpr int defaultPort = 18080;
Ed Tanous38afdb92024-12-11 23:57:53 -080097 if (sd_listen_fds(0) == 1)
Ed Tanous8db83742024-04-13 09:11:15 -070098 {
99 BMCWEB_LOG_INFO("attempting systemd socket activation");
100 if (sd_is_socket_inet(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_STREAM,
101 1, 0) != 0)
102 {
103 BMCWEB_LOG_INFO("Starting webserver on socket handle {}",
104 SD_LISTEN_FDS_START);
105 return boost::asio::ip::tcp::acceptor(
106 *io, boost::asio::ip::tcp::v6(), SD_LISTEN_FDS_START);
107 }
108 BMCWEB_LOG_ERROR(
109 "bad incoming socket, starting webserver on port {}",
110 defaultPort);
111 }
112 BMCWEB_LOG_INFO("Starting webserver on port {}", defaultPort);
113 return boost::asio::ip::tcp::acceptor(
114 *io, boost::asio::ip::tcp::endpoint(
115 boost::asio::ip::make_address("0.0.0.0"), defaultPort));
116 }
117
Ed Tanous1abe55e2018-09-05 08:30:59 -0700118 void run()
119 {
120 validate();
Ed Tanous789a6a32018-11-29 15:17:22 -0800121
Ed Tanous8db83742024-04-13 09:11:15 -0700122 std::optional<boost::asio::ip::tcp::acceptor> acceptor = setupSocket();
123 if (!acceptor)
Ed Tanous789a6a32018-11-29 15:17:22 -0800124 {
Ed Tanous8db83742024-04-13 09:11:15 -0700125 BMCWEB_LOG_CRITICAL("Couldn't start server");
126 return;
Ed Tanous789a6a32018-11-29 15:17:22 -0800127 }
Ed Tanous8db83742024-04-13 09:11:15 -0700128 server.emplace(this, std::move(*acceptor), sslContext, io);
Ed Tanous789a6a32018-11-29 15:17:22 -0800129 server->run();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700130 }
131
Ed Tanous1abe55e2018-09-05 08:30:59 -0700132 void debugPrint()
133 {
Ed Tanous62598e32023-07-17 17:06:25 -0700134 BMCWEB_LOG_DEBUG("Routing:");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700135 router.debugPrint();
136 }
137
138 std::vector<const std::string*> getRoutes()
139 {
Ed Tanouse05aec52022-01-25 10:28:56 -0800140 const std::string root;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700141 return router.getRoutes(root);
142 }
143 std::vector<const std::string*> getRoutes(const std::string& parent)
144 {
145 return router.getRoutes(parent);
146 }
Ed Tanousb4a7bfa2017-04-04 17:23:00 -0700147
Ed Tanousb74e4402020-09-09 20:26:26 -0700148 App& ssl(std::shared_ptr<boost::asio::ssl::context>&& ctx)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700149 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700150 sslContext = std::move(ctx);
Ed Tanous62598e32023-07-17 17:06:25 -0700151 BMCWEB_LOG_INFO("app::ssl context use_count={}",
152 sslContext.use_count());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700153 return *this;
154 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700155
Ed Tanous4fa45df2023-09-01 14:20:50 -0700156 std::shared_ptr<boost::asio::ssl::context> sslContext = nullptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700157
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700158 boost::asio::io_context& ioContext()
159 {
160 return *io;
161 }
162
Ed Tanous1abe55e2018-09-05 08:30:59 -0700163 private:
Ed Tanous23e64202020-09-15 19:21:30 -0700164 std::shared_ptr<boost::asio::io_context> io;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700165
Ed Tanous8db83742024-04-13 09:11:15 -0700166 std::optional<server_type> server;
167
168 Router router;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700169};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700170} // namespace crow
Ed Tanous52cc1122020-07-18 13:51:21 -0700171using App = crow::App;