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