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