Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 3 | #pragma once |
| 4 | #include "bmcweb_config.h" |
| 5 | |
| 6 | #include "async_resp.hpp" |
| 7 | #include "authentication.hpp" |
| 8 | #include "complete_response_fields.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 9 | #include "forward_unauthorized.hpp" |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 10 | #include "http_body.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 11 | #include "http_request.hpp" |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 12 | #include "http_response.hpp" |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 13 | #include "logging.hpp" |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 14 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 15 | // NOLINTNEXTLINE(misc-include-cleaner) |
| 16 | #include "nghttp2_adapters.hpp" |
| 17 | |
| 18 | #include <nghttp2/nghttp2.h> |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | #include <boost/asio/buffer.hpp> |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 22 | #include <boost/asio/ip/tcp.hpp> |
| 23 | #include <boost/asio/ssl/stream.hpp> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 24 | #include <boost/beast/core/error.hpp> |
| 25 | #include <boost/beast/http/field.hpp> |
| 26 | #include <boost/beast/http/fields.hpp> |
| 27 | #include <boost/beast/http/message.hpp> |
| 28 | #include <boost/beast/http/verb.hpp> |
| 29 | #include <boost/optional/optional.hpp> |
Ed Tanous | d088218 | 2024-01-26 23:45:25 -0800 | [diff] [blame] | 30 | #include <boost/system/error_code.hpp> |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 31 | |
Ed Tanous | d088218 | 2024-01-26 23:45:25 -0800 | [diff] [blame] | 32 | #include <array> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 33 | #include <bit> |
| 34 | #include <cstddef> |
| 35 | #include <cstdint> |
Ed Tanous | d088218 | 2024-01-26 23:45:25 -0800 | [diff] [blame] | 36 | #include <functional> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 37 | #include <map> |
Ed Tanous | d088218 | 2024-01-26 23:45:25 -0800 | [diff] [blame] | 38 | #include <memory> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 39 | #include <optional> |
| 40 | #include <span> |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 41 | #include <string> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 42 | #include <string_view> |
| 43 | #include <utility> |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 44 | #include <vector> |
| 45 | |
| 46 | namespace crow |
| 47 | { |
| 48 | |
| 49 | struct Http2StreamData |
| 50 | { |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 51 | std::shared_ptr<Request> req = std::make_shared<Request>(); |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 52 | std::optional<bmcweb::HttpBody::reader> reqReader; |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 53 | std::string accept; |
Ed Tanous | 47f2934 | 2024-03-19 12:18:06 -0700 | [diff] [blame] | 54 | Response res; |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 55 | std::optional<bmcweb::HttpBody::writer> writer; |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | template <typename Adaptor, typename Handler> |
| 59 | class HTTP2Connection : |
| 60 | public std::enable_shared_from_this<HTTP2Connection<Adaptor, Handler>> |
| 61 | { |
| 62 | using self_type = HTTP2Connection<Adaptor, Handler>; |
| 63 | |
| 64 | public: |
| 65 | HTTP2Connection(Adaptor&& adaptorIn, Handler* handlerIn, |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 66 | std::function<std::string()>& getCachedDateStrF) : |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 67 | adaptor(std::move(adaptorIn)), ngSession(initializeNghttp2Session()), |
| 68 | handler(handlerIn), getCachedDateStr(getCachedDateStrF) |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 69 | {} |
| 70 | |
| 71 | void start() |
| 72 | { |
| 73 | // Create the control stream |
Ed Tanous | f42e859 | 2023-08-25 10:47:44 -0700 | [diff] [blame] | 74 | streams[0]; |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 75 | |
| 76 | if (sendServerConnectionHeader() != 0) |
| 77 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 78 | BMCWEB_LOG_ERROR("send_server_connection_header failed"); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 79 | return; |
| 80 | } |
| 81 | doRead(); |
| 82 | } |
| 83 | |
| 84 | int sendServerConnectionHeader() |
| 85 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 86 | BMCWEB_LOG_DEBUG("send_server_connection_header()"); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 87 | |
| 88 | uint32_t maxStreams = 4; |
| 89 | std::array<nghttp2_settings_entry, 2> iv = { |
| 90 | {{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, maxStreams}, |
| 91 | {NGHTTP2_SETTINGS_ENABLE_PUSH, 0}}}; |
| 92 | int rv = ngSession.submitSettings(iv); |
| 93 | if (rv != 0) |
| 94 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 95 | BMCWEB_LOG_ERROR("Fatal error: {}", nghttp2_strerror(rv)); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 96 | return -1; |
| 97 | } |
Ed Tanous | d088218 | 2024-01-26 23:45:25 -0800 | [diff] [blame] | 98 | writeBuffer(); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 102 | static ssize_t fileReadCallback( |
| 103 | nghttp2_session* /* session */, int32_t streamId, uint8_t* buf, |
| 104 | size_t length, uint32_t* dataFlags, nghttp2_data_source* /*source*/, |
| 105 | void* userPtr) |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 106 | { |
Ed Tanous | f42e859 | 2023-08-25 10:47:44 -0700 | [diff] [blame] | 107 | self_type& self = userPtrToSelf(userPtr); |
| 108 | |
| 109 | auto streamIt = self.streams.find(streamId); |
| 110 | if (streamIt == self.streams.end()) |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 111 | { |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 112 | return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; |
| 113 | } |
Ed Tanous | f42e859 | 2023-08-25 10:47:44 -0700 | [diff] [blame] | 114 | Http2StreamData& stream = streamIt->second; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 115 | BMCWEB_LOG_DEBUG("File read callback length: {}", length); |
Ed Tanous | d547d8d | 2024-03-16 18:04:41 -0700 | [diff] [blame] | 116 | if (!stream.writer) |
| 117 | { |
| 118 | return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; |
| 119 | } |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 120 | boost::beast::error_code ec; |
| 121 | boost::optional<std::pair<boost::asio::const_buffer, bool>> out = |
| 122 | stream.writer->getWithMaxSize(ec, length); |
| 123 | if (ec) |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 124 | { |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 125 | BMCWEB_LOG_CRITICAL("Failed to get buffer"); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 126 | return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; |
| 127 | } |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 128 | if (!out) |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 129 | { |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 130 | BMCWEB_LOG_ERROR("Empty file, setting EOF"); |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 131 | *dataFlags |= NGHTTP2_DATA_FLAG_EOF; |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | BMCWEB_LOG_DEBUG("Send chunk of size: {}", out->first.size()); |
| 136 | if (length < out->first.size()) |
| 137 | { |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 138 | BMCWEB_LOG_CRITICAL( |
| 139 | "Buffer overflow that should never happen happened"); |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 140 | // Should never happen because of length limit on get() above |
| 141 | return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; |
| 142 | } |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 143 | boost::asio::mutable_buffer writeableBuf(buf, length); |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 144 | BMCWEB_LOG_DEBUG("Copying {} bytes to buf", out->first.size()); |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 145 | size_t copied = boost::asio::buffer_copy(writeableBuf, out->first); |
| 146 | if (copied != out->first.size()) |
| 147 | { |
| 148 | BMCWEB_LOG_ERROR( |
| 149 | "Couldn't copy all {} bytes into buffer, only copied {}", |
| 150 | out->first.size(), copied); |
| 151 | return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; |
| 152 | } |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 153 | |
| 154 | if (!out->second) |
| 155 | { |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 156 | BMCWEB_LOG_DEBUG("Setting EOF flag"); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 157 | *dataFlags |= NGHTTP2_DATA_FLAG_EOF; |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 158 | } |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 159 | return static_cast<ssize_t>(copied); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | nghttp2_nv headerFromStringViews(std::string_view name, |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 163 | std::string_view value, uint8_t flags) |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 164 | { |
| 165 | uint8_t* nameData = std::bit_cast<uint8_t*>(name.data()); |
| 166 | uint8_t* valueData = std::bit_cast<uint8_t*>(value.data()); |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 167 | return {nameData, valueData, name.size(), value.size(), flags}; |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | int sendResponse(Response& completedRes, int32_t streamId) |
| 171 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 172 | BMCWEB_LOG_DEBUG("send_response stream_id:{}", streamId); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 173 | |
| 174 | auto it = streams.find(streamId); |
| 175 | if (it == streams.end()) |
| 176 | { |
| 177 | close(); |
| 178 | return -1; |
| 179 | } |
Ed Tanous | 499b5b4 | 2024-04-06 08:39:18 -0700 | [diff] [blame] | 180 | Http2StreamData& stream = it->second; |
| 181 | Response& res = stream.res; |
| 182 | res = std::move(completedRes); |
Ed Tanous | 499b5b4 | 2024-04-06 08:39:18 -0700 | [diff] [blame] | 183 | |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 184 | completeResponseFields(stream.accept, res); |
Ed Tanous | 499b5b4 | 2024-04-06 08:39:18 -0700 | [diff] [blame] | 185 | res.addHeader(boost::beast::http::field::date, getCachedDateStr()); |
| 186 | res.preparePayload(); |
| 187 | |
| 188 | boost::beast::http::fields& fields = res.fields(); |
| 189 | std::string code = std::to_string(res.resultInt()); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 190 | std::vector<nghttp2_nv> hdr; |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 191 | hdr.emplace_back( |
| 192 | headerFromStringViews(":status", code, NGHTTP2_NV_FLAG_NONE)); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 193 | for (const boost::beast::http::fields::value_type& header : fields) |
| 194 | { |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 195 | hdr.emplace_back(headerFromStringViews( |
Ed Tanous | d088218 | 2024-01-26 23:45:25 -0800 | [diff] [blame] | 196 | header.name_string(), header.value(), NGHTTP2_NV_FLAG_NONE)); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 197 | } |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 198 | http::response<bmcweb::HttpBody>& fbody = res.response; |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 199 | stream.writer.emplace(fbody.base(), fbody.body()); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 200 | |
| 201 | nghttp2_data_provider dataPrd{ |
Ed Tanous | f42e859 | 2023-08-25 10:47:44 -0700 | [diff] [blame] | 202 | .source = {.fd = 0}, |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 203 | .read_callback = fileReadCallback, |
| 204 | }; |
| 205 | |
| 206 | int rv = ngSession.submitResponse(streamId, hdr, &dataPrd); |
| 207 | if (rv != 0) |
| 208 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 209 | BMCWEB_LOG_ERROR("Fatal error: {}", nghttp2_strerror(rv)); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 210 | close(); |
| 211 | return -1; |
| 212 | } |
Ed Tanous | d088218 | 2024-01-26 23:45:25 -0800 | [diff] [blame] | 213 | writeBuffer(); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | nghttp2_session initializeNghttp2Session() |
| 219 | { |
| 220 | nghttp2_session_callbacks callbacks; |
| 221 | callbacks.setOnFrameRecvCallback(onFrameRecvCallbackStatic); |
| 222 | callbacks.setOnStreamCloseCallback(onStreamCloseCallbackStatic); |
| 223 | callbacks.setOnHeaderCallback(onHeaderCallbackStatic); |
| 224 | callbacks.setOnBeginHeadersCallback(onBeginHeadersCallbackStatic); |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 225 | callbacks.setOnDataChunkRecvCallback(onDataChunkRecvStatic); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 226 | |
| 227 | nghttp2_session session(callbacks); |
| 228 | session.setUserData(this); |
| 229 | |
| 230 | return session; |
| 231 | } |
| 232 | |
| 233 | int onRequestRecv(int32_t streamId) |
| 234 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 235 | BMCWEB_LOG_DEBUG("on_request_recv"); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 236 | |
| 237 | auto it = streams.find(streamId); |
| 238 | if (it == streams.end()) |
| 239 | { |
| 240 | close(); |
| 241 | return -1; |
| 242 | } |
Ed Tanous | d547d8d | 2024-03-16 18:04:41 -0700 | [diff] [blame] | 243 | auto& reqReader = it->second.reqReader; |
| 244 | if (reqReader) |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 245 | { |
| 246 | boost::beast::error_code ec; |
Ed Tanous | daadfb2 | 2024-12-20 09:25:54 -0800 | [diff] [blame] | 247 | bmcweb::HttpBody::reader::finish(ec); |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 248 | if (ec) |
| 249 | { |
| 250 | BMCWEB_LOG_CRITICAL("Failed to finalize payload"); |
| 251 | close(); |
| 252 | return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; |
| 253 | } |
| 254 | } |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 255 | crow::Request& thisReq = *it->second.req; |
Ed Tanous | 8e8245d | 2024-04-11 22:21:38 -0700 | [diff] [blame] | 256 | thisReq.ioService = static_cast<decltype(thisReq.ioService)>( |
| 257 | &adaptor.get_executor().context()); |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 258 | |
| 259 | it->second.accept = thisReq.getHeaderValue("Accept"); |
| 260 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 261 | BMCWEB_LOG_DEBUG("Handling {} \"{}\"", logPtr(&thisReq), |
| 262 | thisReq.url().encoded_path()); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 263 | |
Ed Tanous | f42e859 | 2023-08-25 10:47:44 -0700 | [diff] [blame] | 264 | crow::Response& thisRes = it->second.res; |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 265 | |
| 266 | thisRes.setCompleteRequestHandler( |
| 267 | [this, streamId](Response& completeRes) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 268 | BMCWEB_LOG_DEBUG("res.completeRequestHandler called"); |
| 269 | if (sendResponse(completeRes, streamId) != 0) |
| 270 | { |
| 271 | close(); |
| 272 | return; |
| 273 | } |
| 274 | }); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 275 | auto asyncResp = |
Ed Tanous | f42e859 | 2023-08-25 10:47:44 -0700 | [diff] [blame] | 276 | std::make_shared<bmcweb::AsyncResp>(std::move(it->second.res)); |
Ed Tanous | 8332831 | 2024-05-09 15:48:09 -0700 | [diff] [blame] | 277 | if constexpr (!BMCWEB_INSECURE_DISABLE_AUTH) |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 278 | { |
Ed Tanous | 8332831 | 2024-05-09 15:48:09 -0700 | [diff] [blame] | 279 | thisReq.session = crow::authentication::authenticate( |
| 280 | {}, asyncResp->res, thisReq.method(), thisReq.req, nullptr); |
| 281 | if (!crow::authentication::isOnAllowlist(thisReq.url().path(), |
| 282 | thisReq.method()) && |
| 283 | thisReq.session == nullptr) |
Ed Tanous | 499b5b4 | 2024-04-06 08:39:18 -0700 | [diff] [blame] | 284 | { |
Ed Tanous | 8332831 | 2024-05-09 15:48:09 -0700 | [diff] [blame] | 285 | BMCWEB_LOG_WARNING("Authentication failed"); |
| 286 | forward_unauthorized::sendUnauthorized( |
| 287 | thisReq.url().encoded_path(), |
| 288 | thisReq.getHeaderValue("X-Requested-With"), |
| 289 | thisReq.getHeaderValue("Accept"), asyncResp->res); |
| 290 | return 0; |
Ed Tanous | 499b5b4 | 2024-04-06 08:39:18 -0700 | [diff] [blame] | 291 | } |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 292 | } |
Ed Tanous | 8332831 | 2024-05-09 15:48:09 -0700 | [diff] [blame] | 293 | std::string_view expected = |
| 294 | thisReq.getHeaderValue(boost::beast::http::field::if_none_match); |
| 295 | BMCWEB_LOG_DEBUG("Setting expected hash {}", expected); |
| 296 | if (!expected.empty()) |
| 297 | { |
| 298 | asyncResp->res.setExpectedHash(expected); |
| 299 | } |
| 300 | handler->handle(it->second.req, asyncResp); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 301 | return 0; |
| 302 | } |
| 303 | |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 304 | int onDataChunkRecvCallback(uint8_t /*flags*/, int32_t streamId, |
| 305 | const uint8_t* data, size_t len) |
| 306 | { |
| 307 | auto thisStream = streams.find(streamId); |
| 308 | if (thisStream == streams.end()) |
| 309 | { |
| 310 | BMCWEB_LOG_ERROR("Unknown stream{}", streamId); |
| 311 | close(); |
| 312 | return -1; |
| 313 | } |
Ed Tanous | d547d8d | 2024-03-16 18:04:41 -0700 | [diff] [blame] | 314 | |
| 315 | std::optional<bmcweb::HttpBody::reader>& reqReader = |
| 316 | thisStream->second.reqReader; |
| 317 | if (!reqReader) |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 318 | { |
Ed Tanous | 8e5cc7b | 2024-04-02 11:00:54 -0700 | [diff] [blame] | 319 | reqReader.emplace( |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 320 | bmcweb::HttpBody::reader(thisStream->second.req->req.base(), |
| 321 | thisStream->second.req->req.body())); |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 322 | } |
| 323 | boost::beast::error_code ec; |
Ed Tanous | d547d8d | 2024-03-16 18:04:41 -0700 | [diff] [blame] | 324 | reqReader->put(boost::asio::const_buffer(data, len), ec); |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 325 | if (ec) |
| 326 | { |
| 327 | BMCWEB_LOG_CRITICAL("Failed to write payload"); |
| 328 | return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE; |
| 329 | } |
| 330 | return 0; |
| 331 | } |
| 332 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 333 | static int onDataChunkRecvStatic( |
| 334 | nghttp2_session* /* session */, uint8_t flags, int32_t streamId, |
| 335 | const uint8_t* data, size_t len, void* userData) |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 336 | { |
| 337 | BMCWEB_LOG_DEBUG("on_frame_recv_callback"); |
| 338 | if (userData == nullptr) |
| 339 | { |
| 340 | BMCWEB_LOG_CRITICAL("user data was null?"); |
| 341 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
| 342 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 343 | return userPtrToSelf(userData).onDataChunkRecvCallback( |
| 344 | flags, streamId, data, len); |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 347 | int onFrameRecvCallback(const nghttp2_frame& frame) |
| 348 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 349 | BMCWEB_LOG_DEBUG("frame type {}", static_cast<int>(frame.hd.type)); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 350 | switch (frame.hd.type) |
| 351 | { |
| 352 | case NGHTTP2_DATA: |
| 353 | case NGHTTP2_HEADERS: |
| 354 | // Check that the client request has finished |
| 355 | if ((frame.hd.flags & NGHTTP2_FLAG_END_STREAM) != 0) |
| 356 | { |
| 357 | return onRequestRecv(frame.hd.stream_id); |
| 358 | } |
| 359 | break; |
| 360 | default: |
| 361 | break; |
| 362 | } |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | static int onFrameRecvCallbackStatic(nghttp2_session* /* session */, |
| 367 | const nghttp2_frame* frame, |
| 368 | void* userData) |
| 369 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 370 | BMCWEB_LOG_DEBUG("on_frame_recv_callback"); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 371 | if (userData == nullptr) |
| 372 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 373 | BMCWEB_LOG_CRITICAL("user data was null?"); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 374 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
| 375 | } |
| 376 | if (frame == nullptr) |
| 377 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 378 | BMCWEB_LOG_CRITICAL("frame was null?"); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 379 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
| 380 | } |
| 381 | return userPtrToSelf(userData).onFrameRecvCallback(*frame); |
| 382 | } |
| 383 | |
| 384 | static self_type& userPtrToSelf(void* userData) |
| 385 | { |
| 386 | // This method exists to keep the unsafe reinterpret cast in one |
| 387 | // place. |
| 388 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 389 | return *reinterpret_cast<self_type*>(userData); |
| 390 | } |
| 391 | |
| 392 | static int onStreamCloseCallbackStatic(nghttp2_session* /* session */, |
| 393 | int32_t streamId, |
| 394 | uint32_t /*unused*/, void* userData) |
| 395 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 396 | BMCWEB_LOG_DEBUG("on_stream_close_callback stream {}", streamId); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 397 | if (userData == nullptr) |
| 398 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 399 | BMCWEB_LOG_CRITICAL("user data was null?"); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 400 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
| 401 | } |
Ed Tanous | f42e859 | 2023-08-25 10:47:44 -0700 | [diff] [blame] | 402 | if (userPtrToSelf(userData).streams.erase(streamId) <= 0) |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 403 | { |
| 404 | return -1; |
| 405 | } |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | int onHeaderCallback(const nghttp2_frame& frame, |
| 410 | std::span<const uint8_t> name, |
| 411 | std::span<const uint8_t> value) |
| 412 | { |
| 413 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 414 | std::string_view nameSv(reinterpret_cast<const char*>(name.data()), |
| 415 | name.size()); |
| 416 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 417 | std::string_view valueSv(reinterpret_cast<const char*>(value.data()), |
| 418 | value.size()); |
| 419 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 420 | BMCWEB_LOG_DEBUG("on_header_callback name: {} value {}", nameSv, |
| 421 | valueSv); |
Ed Tanous | a07e981 | 2024-03-19 10:31:13 -0700 | [diff] [blame] | 422 | if (frame.hd.type != NGHTTP2_HEADERS) |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 423 | { |
Ed Tanous | a07e981 | 2024-03-19 10:31:13 -0700 | [diff] [blame] | 424 | return 0; |
| 425 | } |
| 426 | if (frame.headers.cat != NGHTTP2_HCAT_REQUEST) |
| 427 | { |
| 428 | return 0; |
| 429 | } |
| 430 | auto thisStream = streams.find(frame.hd.stream_id); |
| 431 | if (thisStream == streams.end()) |
| 432 | { |
| 433 | BMCWEB_LOG_ERROR("Unknown stream{}", frame.hd.stream_id); |
| 434 | close(); |
| 435 | return -1; |
| 436 | } |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 437 | |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 438 | crow::Request& thisReq = *thisStream->second.req; |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 439 | |
Ed Tanous | a07e981 | 2024-03-19 10:31:13 -0700 | [diff] [blame] | 440 | if (nameSv == ":path") |
| 441 | { |
| 442 | thisReq.target(valueSv); |
| 443 | } |
| 444 | else if (nameSv == ":method") |
| 445 | { |
| 446 | boost::beast::http::verb verb = |
| 447 | boost::beast::http::string_to_verb(valueSv); |
| 448 | if (verb == boost::beast::http::verb::unknown) |
| 449 | { |
| 450 | BMCWEB_LOG_ERROR("Unknown http verb {}", valueSv); |
Ed Tanous | 50bfc91 | 2024-07-29 14:20:50 -0700 | [diff] [blame] | 451 | verb = boost::beast::http::verb::trace; |
Ed Tanous | a07e981 | 2024-03-19 10:31:13 -0700 | [diff] [blame] | 452 | } |
Myung Bae | 1873a04 | 2024-04-01 09:27:39 -0500 | [diff] [blame] | 453 | thisReq.method(verb); |
Ed Tanous | a07e981 | 2024-03-19 10:31:13 -0700 | [diff] [blame] | 454 | } |
| 455 | else if (nameSv == ":scheme") |
| 456 | { |
| 457 | // Nothing to check on scheme |
| 458 | } |
| 459 | else |
| 460 | { |
Myung Bae | 1873a04 | 2024-04-01 09:27:39 -0500 | [diff] [blame] | 461 | thisReq.addHeader(nameSv, valueSv); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 462 | } |
| 463 | return 0; |
| 464 | } |
| 465 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 466 | static int onHeaderCallbackStatic( |
| 467 | nghttp2_session* /* session */, const nghttp2_frame* frame, |
| 468 | const uint8_t* name, size_t namelen, const uint8_t* value, |
| 469 | size_t vallen, uint8_t /* flags */, void* userData) |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 470 | { |
| 471 | if (userData == nullptr) |
| 472 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 473 | BMCWEB_LOG_CRITICAL("user data was null?"); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 474 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
| 475 | } |
| 476 | if (frame == nullptr) |
| 477 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 478 | BMCWEB_LOG_CRITICAL("frame was null?"); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 479 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
| 480 | } |
| 481 | if (name == nullptr) |
| 482 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 483 | BMCWEB_LOG_CRITICAL("name was null?"); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 484 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
| 485 | } |
| 486 | if (value == nullptr) |
| 487 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 488 | BMCWEB_LOG_CRITICAL("value was null?"); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 489 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
| 490 | } |
| 491 | return userPtrToSelf(userData).onHeaderCallback(*frame, {name, namelen}, |
| 492 | {value, vallen}); |
| 493 | } |
| 494 | |
| 495 | int onBeginHeadersCallback(const nghttp2_frame& frame) |
| 496 | { |
| 497 | if (frame.hd.type == NGHTTP2_HEADERS && |
| 498 | frame.headers.cat == NGHTTP2_HCAT_REQUEST) |
| 499 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 500 | BMCWEB_LOG_DEBUG("create stream for id {}", frame.hd.stream_id); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 501 | |
Ed Tanous | 3a49941 | 2024-06-05 16:05:35 -0700 | [diff] [blame] | 502 | streams.emplace(frame.hd.stream_id, Http2StreamData()); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 503 | } |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static int onBeginHeadersCallbackStatic(nghttp2_session* /* session */, |
| 508 | const nghttp2_frame* frame, |
| 509 | void* userData) |
| 510 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 511 | BMCWEB_LOG_DEBUG("on_begin_headers_callback"); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 512 | if (userData == nullptr) |
| 513 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 514 | BMCWEB_LOG_CRITICAL("user data was null?"); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 515 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
| 516 | } |
| 517 | if (frame == nullptr) |
| 518 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 519 | BMCWEB_LOG_CRITICAL("frame was null?"); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 520 | return NGHTTP2_ERR_CALLBACK_FAILURE; |
| 521 | } |
| 522 | return userPtrToSelf(userData).onBeginHeadersCallback(*frame); |
| 523 | } |
| 524 | |
| 525 | static void afterWriteBuffer(const std::shared_ptr<self_type>& self, |
| 526 | const boost::system::error_code& ec, |
| 527 | size_t sendLength) |
| 528 | { |
| 529 | self->isWriting = false; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 530 | BMCWEB_LOG_DEBUG("Sent {}", sendLength); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 531 | if (ec) |
| 532 | { |
| 533 | self->close(); |
| 534 | return; |
| 535 | } |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 536 | self->writeBuffer(); |
| 537 | } |
| 538 | |
| 539 | void writeBuffer() |
| 540 | { |
| 541 | if (isWriting) |
| 542 | { |
| 543 | return; |
| 544 | } |
Ed Tanous | d088218 | 2024-01-26 23:45:25 -0800 | [diff] [blame] | 545 | std::span<const uint8_t> data = ngSession.memSend(); |
| 546 | if (data.empty()) |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 547 | { |
| 548 | return; |
| 549 | } |
| 550 | isWriting = true; |
Ed Tanous | 325310d | 2024-03-15 09:05:04 -0700 | [diff] [blame] | 551 | boost::asio::async_write( |
| 552 | adaptor, boost::asio::const_buffer(data.data(), data.size()), |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 553 | std::bind_front(afterWriteBuffer, shared_from_this())); |
| 554 | } |
| 555 | |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 556 | void close() |
| 557 | { |
| 558 | if constexpr (std::is_same_v<Adaptor, |
Ed Tanous | 003301a | 2024-04-16 09:59:19 -0700 | [diff] [blame] | 559 | boost::asio::ssl::stream< |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 560 | boost::asio::ip::tcp::socket>>) |
| 561 | { |
| 562 | adaptor.next_layer().close(); |
| 563 | } |
| 564 | else |
| 565 | { |
| 566 | adaptor.close(); |
| 567 | } |
| 568 | } |
| 569 | |
Ed Tanous | d088218 | 2024-01-26 23:45:25 -0800 | [diff] [blame] | 570 | void afterDoRead(const std::shared_ptr<self_type>& /*self*/, |
| 571 | const boost::system::error_code& ec, |
| 572 | size_t bytesTransferred) |
| 573 | { |
| 574 | BMCWEB_LOG_DEBUG("{} async_read_some {} Bytes", logPtr(this), |
| 575 | bytesTransferred); |
| 576 | |
| 577 | if (ec) |
| 578 | { |
| 579 | BMCWEB_LOG_ERROR("{} Error while reading: {}", logPtr(this), |
| 580 | ec.message()); |
| 581 | close(); |
| 582 | BMCWEB_LOG_DEBUG("{} from read(1)", logPtr(this)); |
| 583 | return; |
| 584 | } |
| 585 | std::span<uint8_t> bufferSpan{inBuffer.data(), bytesTransferred}; |
| 586 | |
| 587 | ssize_t readLen = ngSession.memRecv(bufferSpan); |
| 588 | if (readLen < 0) |
| 589 | { |
| 590 | BMCWEB_LOG_ERROR("nghttp2_session_mem_recv returned {}", readLen); |
| 591 | close(); |
| 592 | return; |
| 593 | } |
| 594 | writeBuffer(); |
| 595 | |
| 596 | doRead(); |
| 597 | } |
| 598 | |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 599 | void doRead() |
| 600 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 601 | BMCWEB_LOG_DEBUG("{} doRead", logPtr(this)); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 602 | adaptor.async_read_some( |
Ed Tanous | d088218 | 2024-01-26 23:45:25 -0800 | [diff] [blame] | 603 | boost::asio::buffer(inBuffer), |
| 604 | std::bind_front(&self_type::afterDoRead, this, shared_from_this())); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | // A mapping from http2 stream ID to Stream Data |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 608 | std::map<int32_t, Http2StreamData> streams; |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 609 | |
Ed Tanous | d088218 | 2024-01-26 23:45:25 -0800 | [diff] [blame] | 610 | std::array<uint8_t, 8192> inBuffer{}; |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 611 | |
| 612 | Adaptor adaptor; |
| 613 | bool isWriting = false; |
| 614 | |
| 615 | nghttp2_session ngSession; |
| 616 | |
| 617 | Handler* handler; |
| 618 | std::function<std::string()>& getCachedDateStr; |
| 619 | |
| 620 | using std::enable_shared_from_this< |
| 621 | HTTP2Connection<Adaptor, Handler>>::shared_from_this; |
| 622 | |
| 623 | using std::enable_shared_from_this< |
| 624 | HTTP2Connection<Adaptor, Handler>>::weak_from_this; |
| 625 | }; |
| 626 | } // namespace crow |