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