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