blob: 8687c9c98ac37324142df1351f13f4ecd0bee2da [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 Tanous796ba932020-08-02 04:29:21 +00006#include "http_connect_types.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -07007#include "http_request.hpp"
8#include "http_server.hpp"
Ed Tanous9838eb22025-01-29 16:24:42 -08009#include "io_context_singleton.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -070010#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/ip/tcp.hpp>
Nan Zhoucec58fe2022-06-14 20:45:45 +000018#include <boost/asio/ssl/context.hpp>
Nan Zhoucec58fe2022-06-14 20:45:45 +000019
Ed Tanous796ba932020-08-02 04:29:21 +000020#include <cstddef>
Ed Tanous7045c8d2017-04-03 10:04:37 -070021#include <cstdint>
Ed Tanous7045c8d2017-04-03 10:04:37 -070022#include <memory>
Ed Tanousd7857202025-01-28 15:32:26 -080023#include <optional>
Ed Tanous796ba932020-08-02 04:29:21 +000024#include <span>
Ed Tanous7045c8d2017-04-03 10:04:37 -070025#include <string>
Ed Tanous911ac312017-08-15 09:37:42 -070026#include <utility>
Ed Tanousd7857202025-01-28 15:32:26 -080027#include <vector>
Ed Tanous1abe55e2018-09-05 08:30:59 -070028
Patrick Williamsa2323432023-05-12 10:06:35 -050029// NOLINTNEXTLINE(cppcoreguidelines-macro-usage, clang-diagnostic-unused-macros)
Ed Tanous1abe55e2018-09-05 08:30:59 -070030#define BMCWEB_ROUTE(app, url) \
Ed Tanous47488a92023-06-26 18:19:33 -070031 app.template route<crow::utility::getParameterTag(url)>(url)
Ed Tanous7045c8d2017-04-03 10:04:37 -070032
Ed Tanous1abe55e2018-09-05 08:30:59 -070033namespace crow
34{
Ed Tanous52cc1122020-07-18 13:51:21 -070035class App
Ed Tanous1abe55e2018-09-05 08:30:59 -070036{
37 public:
Ed Tanous8db83742024-04-13 09:11:15 -070038 using raw_socket_t = boost::asio::ip::tcp::socket;
Ed Tanous796ba932020-08-02 04:29:21 +000039 using server_type = Server<App, raw_socket_t>;
Ed Tanousceac6f72018-12-02 11:58:47 -080040
Ed Tanous1abe55e2018-09-05 08:30:59 -070041 template <typename Adaptor>
Jonathan Doman102a4cd2024-04-15 16:56:23 -070042 void handleUpgrade(const std::shared_ptr<Request>& req,
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +053043 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
44 Adaptor&& adaptor)
Ed Tanous1abe55e2018-09-05 08:30:59 -070045 {
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +053046 router.handleUpgrade(req, asyncResp, std::forward<Adaptor>(adaptor));
Ed Tanous1abe55e2018-09-05 08:30:59 -070047 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070048
Jonathan Doman102a4cd2024-04-15 16:56:23 -070049 void handle(const std::shared_ptr<Request>& req,
zhanghch058d1b46d2021-04-01 11:18:24 +080050 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -070051 {
zhanghch058d1b46d2021-04-01 11:18:24 +080052 router.handle(req, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -070053 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070054
Ed Tanous8cb2c022024-03-27 16:31:46 -070055 DynamicRule& routeDynamic(const std::string& rule)
Ed Tanous1abe55e2018-09-05 08:30:59 -070056 {
57 return router.newRuleDynamic(rule);
58 }
59
Gunnar Mills1214b7e2020-06-04 10:11:30 -050060 template <uint64_t Tag>
61 auto& route(std::string&& rule)
Ed Tanous1abe55e2018-09-05 08:30:59 -070062 {
63 return router.newRuleTagged<Tag>(std::move(rule));
64 }
65
Ed Tanous1abe55e2018-09-05 08:30:59 -070066 void validate()
67 {
68 router.validate();
69 }
70
Ed Tanous3281bcf2024-06-25 16:02:05 -070071 void loadCertificate()
72 {
73 BMCWEB_LOG_DEBUG("Loading certificate");
74 if (!server)
75 {
76 return;
77 }
78 server->loadCertificate();
79 }
80
Ed Tanous796ba932020-08-02 04:29:21 +000081 static HttpType getHttpType(std::string_view socketTypeString)
Ed Tanous8db83742024-04-13 09:11:15 -070082 {
Ed Tanous796ba932020-08-02 04:29:21 +000083 if (socketTypeString == "http")
Ed Tanous8db83742024-04-13 09:11:15 -070084 {
Ed Tanous796ba932020-08-02 04:29:21 +000085 BMCWEB_LOG_DEBUG("Got http socket");
86 return HttpType::HTTP;
87 }
88 if (socketTypeString == "https")
89 {
90 BMCWEB_LOG_DEBUG("Got https socket");
91 return HttpType::HTTPS;
92 }
93 if (socketTypeString == "both")
94 {
95 BMCWEB_LOG_DEBUG("Got hybrid socket");
96 return HttpType::BOTH;
97 }
98
99 // all other types https
100 BMCWEB_LOG_ERROR("Unknown http type={} assuming HTTPS only",
101 socketTypeString);
102 return HttpType::HTTPS;
103 }
104
105 static std::vector<Acceptor> setupSocket()
106 {
107 std::vector<Acceptor> acceptors;
108 char** names = nullptr;
109 int listenFdCount = sd_listen_fds_with_names(0, &names);
110 BMCWEB_LOG_DEBUG("Got {} sockets to open", listenFdCount);
111
112 if (listenFdCount < 0)
113 {
114 BMCWEB_LOG_CRITICAL("Failed to read socket files");
115 return acceptors;
116 }
117 int socketIndex = 0;
118 for (char* name :
119 std::span<char*>(names, static_cast<size_t>(listenFdCount)))
120 {
121 if (name == nullptr)
122 {
123 continue;
124 }
125 // name looks like bmcweb_443_https_auth
126 // Assume HTTPS as default
127 std::string socketName(name);
128
129 std::vector<std::string> socknameComponents;
130 bmcweb::split(socknameComponents, socketName, '_');
131 HttpType httpType = getHttpType(socknameComponents[2]);
132
133 int listenFd = socketIndex + SD_LISTEN_FDS_START;
134 if (sd_is_socket_inet(listenFd, AF_UNSPEC, SOCK_STREAM, 1, 0) > 0)
Ed Tanous8db83742024-04-13 09:11:15 -0700135 {
136 BMCWEB_LOG_INFO("Starting webserver on socket handle {}",
Ed Tanous796ba932020-08-02 04:29:21 +0000137 listenFd);
138 acceptors.emplace_back(Acceptor{
139 boost::asio::ip::tcp::acceptor(
140 getIoContext(), boost::asio::ip::tcp::v6(), listenFd),
141 httpType});
Ed Tanous8db83742024-04-13 09:11:15 -0700142 }
Ed Tanous796ba932020-08-02 04:29:21 +0000143 socketIndex++;
Ed Tanous8db83742024-04-13 09:11:15 -0700144 }
Ed Tanous796ba932020-08-02 04:29:21 +0000145
146 return acceptors;
Ed Tanous8db83742024-04-13 09:11:15 -0700147 }
148
Ed Tanous1abe55e2018-09-05 08:30:59 -0700149 void run()
150 {
151 validate();
Ed Tanous789a6a32018-11-29 15:17:22 -0800152
Ed Tanous796ba932020-08-02 04:29:21 +0000153 std::vector<Acceptor> acceptors = setupSocket();
154
155 server.emplace(this, std::move(acceptors));
Ed Tanous789a6a32018-11-29 15:17:22 -0800156 server->run();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700157 }
158
Ed Tanous1abe55e2018-09-05 08:30:59 -0700159 void debugPrint()
160 {
Ed Tanous62598e32023-07-17 17:06:25 -0700161 BMCWEB_LOG_DEBUG("Routing:");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700162 router.debugPrint();
163 }
164
165 std::vector<const std::string*> getRoutes()
166 {
Ed Tanouse05aec52022-01-25 10:28:56 -0800167 const std::string root;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700168 return router.getRoutes(root);
169 }
170 std::vector<const std::string*> getRoutes(const std::string& parent)
171 {
172 return router.getRoutes(parent);
173 }
Ed Tanousb4a7bfa2017-04-04 17:23:00 -0700174
Ed Tanous8db83742024-04-13 09:11:15 -0700175 std::optional<server_type> server;
176
177 Router router;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700178};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700179} // namespace crow
Ed Tanous52cc1122020-07-18 13:51:21 -0700180using App = crow::App;