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