blob: 2c60c1268ebffef5bd20b786474797c5c0507388 [file] [log] [blame]
Ed Tanousfca2cbe2021-01-28 14:49:59 -08001#pragma once
2#include "bmcweb_config.h"
3
4#include "async_resp.hpp"
5#include "authentication.hpp"
6#include "complete_response_fields.hpp"
Ed Tanous325310d2024-03-15 09:05:04 -07007#include "http_body.hpp"
Ed Tanousfca2cbe2021-01-28 14:49:59 -08008#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 Tanousfca2cbe2021-01-28 14:49:59 -080016#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 Tanousfca2cbe2021-01-28 14:49:59 -080020#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 Tanousfca2cbe2021-01-28 14:49:59 -080024#include <boost/beast/http/write.hpp>
Ed Tanousfca2cbe2021-01-28 14:49:59 -080025#include <boost/beast/websocket.hpp>
Ed Tanousd0882182024-01-26 23:45:25 -080026#include <boost/system/error_code.hpp>
Ed Tanousfca2cbe2021-01-28 14:49:59 -080027
Ed Tanousd0882182024-01-26 23:45:25 -080028#include <array>
Ed Tanousfca2cbe2021-01-28 14:49:59 -080029#include <atomic>
30#include <chrono>
Ed Tanousd0882182024-01-26 23:45:25 -080031#include <functional>
32#include <memory>
Ed Tanousfca2cbe2021-01-28 14:49:59 -080033#include <vector>
34
35namespace crow
36{
37
38struct Http2StreamData
39{
Ed Tanous47f29342024-03-19 12:18:06 -070040 Request req;
Ed Tanous325310d2024-03-15 09:05:04 -070041 std::optional<bmcweb::HttpBody::reader> reqReader;
Ed Tanous47f29342024-03-19 12:18:06 -070042 Response res;
Ed Tanousb2896142024-01-31 15:25:47 -080043 std::optional<bmcweb::HttpBody::writer> writer;
Ed Tanousfca2cbe2021-01-28 14:49:59 -080044};
45
46template <typename Adaptor, typename Handler>
47class 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 Tanous52e31622024-01-23 16:31:11 -080054 std::function<std::string()>& getCachedDateStrF) :
Ed Tanousfca2cbe2021-01-28 14:49:59 -080055 adaptor(std::move(adaptorIn)),
Ed Tanous52e31622024-01-23 16:31:11 -080056 ngSession(initializeNghttp2Session()), handler(handlerIn),
57 getCachedDateStr(getCachedDateStrF)
Ed Tanousfca2cbe2021-01-28 14:49:59 -080058 {}
59
60 void start()
61 {
62 // Create the control stream
Ed Tanousf42e8592023-08-25 10:47:44 -070063 streams[0];
Ed Tanousfca2cbe2021-01-28 14:49:59 -080064
65 if (sendServerConnectionHeader() != 0)
66 {
Ed Tanous62598e32023-07-17 17:06:25 -070067 BMCWEB_LOG_ERROR("send_server_connection_header failed");
Ed Tanousfca2cbe2021-01-28 14:49:59 -080068 return;
69 }
70 doRead();
71 }
72
73 int sendServerConnectionHeader()
74 {
Ed Tanous62598e32023-07-17 17:06:25 -070075 BMCWEB_LOG_DEBUG("send_server_connection_header()");
Ed Tanousfca2cbe2021-01-28 14:49:59 -080076
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 Tanous62598e32023-07-17 17:06:25 -070084 BMCWEB_LOG_ERROR("Fatal error: {}", nghttp2_strerror(rv));
Ed Tanousfca2cbe2021-01-28 14:49:59 -080085 return -1;
86 }
Ed Tanousd0882182024-01-26 23:45:25 -080087 writeBuffer();
Ed Tanousfca2cbe2021-01-28 14:49:59 -080088 return 0;
89 }
90
91 static ssize_t fileReadCallback(nghttp2_session* /* session */,
Ed Tanousf42e8592023-08-25 10:47:44 -070092 int32_t streamId, uint8_t* buf,
Ed Tanousfca2cbe2021-01-28 14:49:59 -080093 size_t length, uint32_t* dataFlags,
Ed Tanousf42e8592023-08-25 10:47:44 -070094 nghttp2_data_source* /*source*/,
95 void* userPtr)
Ed Tanousfca2cbe2021-01-28 14:49:59 -080096 {
Ed Tanousf42e8592023-08-25 10:47:44 -070097 self_type& self = userPtrToSelf(userPtr);
98
99 auto streamIt = self.streams.find(streamId);
100 if (streamIt == self.streams.end())
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800101 {
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800102 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
103 }
Ed Tanousf42e8592023-08-25 10:47:44 -0700104 Http2StreamData& stream = streamIt->second;
Ed Tanous62598e32023-07-17 17:06:25 -0700105 BMCWEB_LOG_DEBUG("File read callback length: {}", length);
Ed Tanousd547d8d2024-03-16 18:04:41 -0700106 if (!stream.writer)
107 {
108 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
109 }
Ed Tanous52e31622024-01-23 16:31:11 -0800110 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 Tanous27b0cf92023-08-07 12:02:40 -0700114 {
Ed Tanous325310d2024-03-15 09:05:04 -0700115 BMCWEB_LOG_CRITICAL("Failed to get buffer");
Ed Tanous27b0cf92023-08-07 12:02:40 -0700116 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
117 }
Ed Tanous52e31622024-01-23 16:31:11 -0800118 if (!out)
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800119 {
Ed Tanous325310d2024-03-15 09:05:04 -0700120 BMCWEB_LOG_ERROR("Empty file, setting EOF");
Ed Tanous52e31622024-01-23 16:31:11 -0800121 *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 Tanous325310d2024-03-15 09:05:04 -0700128 BMCWEB_LOG_CRITICAL(
129 "Buffer overflow that should never happen happened");
Ed Tanous52e31622024-01-23 16:31:11 -0800130 // Should never happen because of length limit on get() above
131 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
132 }
Ed Tanous325310d2024-03-15 09:05:04 -0700133 boost::asio::mutable_buffer writeableBuf(buf, length);
Ed Tanous52e31622024-01-23 16:31:11 -0800134 BMCWEB_LOG_DEBUG("Copying {} bytes to buf", out->first.size());
Ed Tanous325310d2024-03-15 09:05:04 -0700135 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 Tanous52e31622024-01-23 16:31:11 -0800143
144 if (!out->second)
145 {
Ed Tanous325310d2024-03-15 09:05:04 -0700146 BMCWEB_LOG_DEBUG("Setting EOF flag");
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800147 *dataFlags |= NGHTTP2_DATA_FLAG_EOF;
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800148 }
Ed Tanous325310d2024-03-15 09:05:04 -0700149 return static_cast<ssize_t>(copied);
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800150 }
151
152 nghttp2_nv headerFromStringViews(std::string_view name,
Ed Tanous52e31622024-01-23 16:31:11 -0800153 std::string_view value, uint8_t flags)
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800154 {
155 uint8_t* nameData = std::bit_cast<uint8_t*>(name.data());
156 uint8_t* valueData = std::bit_cast<uint8_t*>(value.data());
Ed Tanous52e31622024-01-23 16:31:11 -0800157 return {nameData, valueData, name.size(), value.size(), flags};
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800158 }
159
160 int sendResponse(Response& completedRes, int32_t streamId)
161 {
Ed Tanous62598e32023-07-17 17:06:25 -0700162 BMCWEB_LOG_DEBUG("send_response stream_id:{}", streamId);
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800163
164 auto it = streams.find(streamId);
165 if (it == streams.end())
166 {
167 close();
168 return -1;
169 }
Ed Tanous499b5b42024-04-06 08:39:18 -0700170 Http2StreamData& stream = it->second;
171 Response& res = stream.res;
172 res = std::move(completedRes);
173 crow::Request& thisReq = stream.req;
174
175 completeResponseFields(thisReq, res);
176 res.addHeader(boost::beast::http::field::date, getCachedDateStr());
177 res.preparePayload();
178
179 boost::beast::http::fields& fields = res.fields();
180 std::string code = std::to_string(res.resultInt());
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800181 std::vector<nghttp2_nv> hdr;
Ed Tanous52e31622024-01-23 16:31:11 -0800182 hdr.emplace_back(
183 headerFromStringViews(":status", code, NGHTTP2_NV_FLAG_NONE));
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800184 for (const boost::beast::http::fields::value_type& header : fields)
185 {
Ed Tanous52e31622024-01-23 16:31:11 -0800186 hdr.emplace_back(headerFromStringViews(
Ed Tanousd0882182024-01-26 23:45:25 -0800187 header.name_string(), header.value(), NGHTTP2_NV_FLAG_NONE));
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800188 }
Ed Tanousb2896142024-01-31 15:25:47 -0800189 http::response<bmcweb::HttpBody>& fbody = res.response;
Ed Tanous52e31622024-01-23 16:31:11 -0800190 stream.writer.emplace(fbody.base(), fbody.body());
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800191
192 nghttp2_data_provider dataPrd{
Ed Tanousf42e8592023-08-25 10:47:44 -0700193 .source = {.fd = 0},
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800194 .read_callback = fileReadCallback,
195 };
196
197 int rv = ngSession.submitResponse(streamId, hdr, &dataPrd);
198 if (rv != 0)
199 {
Ed Tanous62598e32023-07-17 17:06:25 -0700200 BMCWEB_LOG_ERROR("Fatal error: {}", nghttp2_strerror(rv));
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800201 close();
202 return -1;
203 }
Ed Tanousd0882182024-01-26 23:45:25 -0800204 writeBuffer();
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800205
206 return 0;
207 }
208
209 nghttp2_session initializeNghttp2Session()
210 {
211 nghttp2_session_callbacks callbacks;
212 callbacks.setOnFrameRecvCallback(onFrameRecvCallbackStatic);
213 callbacks.setOnStreamCloseCallback(onStreamCloseCallbackStatic);
214 callbacks.setOnHeaderCallback(onHeaderCallbackStatic);
215 callbacks.setOnBeginHeadersCallback(onBeginHeadersCallbackStatic);
Ed Tanous325310d2024-03-15 09:05:04 -0700216 callbacks.setOnDataChunkRecvCallback(onDataChunkRecvStatic);
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800217
218 nghttp2_session session(callbacks);
219 session.setUserData(this);
220
221 return session;
222 }
223
224 int onRequestRecv(int32_t streamId)
225 {
Ed Tanous62598e32023-07-17 17:06:25 -0700226 BMCWEB_LOG_DEBUG("on_request_recv");
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800227
228 auto it = streams.find(streamId);
229 if (it == streams.end())
230 {
231 close();
232 return -1;
233 }
Ed Tanousd547d8d2024-03-16 18:04:41 -0700234 auto& reqReader = it->second.reqReader;
235 if (reqReader)
Ed Tanous325310d2024-03-15 09:05:04 -0700236 {
237 boost::beast::error_code ec;
Ed Tanousd547d8d2024-03-16 18:04:41 -0700238 reqReader->finish(ec);
Ed Tanous325310d2024-03-15 09:05:04 -0700239 if (ec)
240 {
241 BMCWEB_LOG_CRITICAL("Failed to finalize payload");
242 close();
243 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
244 }
245 }
Ed Tanousf42e8592023-08-25 10:47:44 -0700246 crow::Request& thisReq = it->second.req;
Ed Tanous8e8245d2024-04-11 22:21:38 -0700247 thisReq.ioService = static_cast<decltype(thisReq.ioService)>(
248 &adaptor.get_executor().context());
Ed Tanous62598e32023-07-17 17:06:25 -0700249 BMCWEB_LOG_DEBUG("Handling {} \"{}\"", logPtr(&thisReq),
250 thisReq.url().encoded_path());
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800251
Ed Tanousf42e8592023-08-25 10:47:44 -0700252 crow::Response& thisRes = it->second.res;
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800253
254 thisRes.setCompleteRequestHandler(
255 [this, streamId](Response& completeRes) {
Ed Tanous62598e32023-07-17 17:06:25 -0700256 BMCWEB_LOG_DEBUG("res.completeRequestHandler called");
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800257 if (sendResponse(completeRes, streamId) != 0)
258 {
259 close();
260 return;
261 }
262 });
263 auto asyncResp =
Ed Tanousf42e8592023-08-25 10:47:44 -0700264 std::make_shared<bmcweb::AsyncResp>(std::move(it->second.res));
Ed Tanous325310d2024-03-15 09:05:04 -0700265#ifndef BMCWEB_INSECURE_DISABLE_AUTHX
266 thisReq.session = crow::authentication::authenticate(
Ed Tanous52c15022024-04-19 16:09:59 -0700267 {}, asyncResp->res, thisReq.method(), thisReq.req, nullptr);
Ed Tanous325310d2024-03-15 09:05:04 -0700268 if (!crow::authentication::isOnAllowlist(thisReq.url().path(),
269 thisReq.method()) &&
270 thisReq.session == nullptr)
271 {
272 BMCWEB_LOG_WARNING("Authentication failed");
273 forward_unauthorized::sendUnauthorized(
274 thisReq.url().encoded_path(),
275 thisReq.getHeaderValue("X-Requested-With"),
Ed Tanous52c15022024-04-19 16:09:59 -0700276 thisReq.getHeaderValue("Accept"), asyncResp->res);
Ed Tanous325310d2024-03-15 09:05:04 -0700277 }
278 else
279#endif // BMCWEB_INSECURE_DISABLE_AUTHX
280 {
Ed Tanous499b5b42024-04-06 08:39:18 -0700281 std::string_view expected = thisReq.getHeaderValue(
282 boost::beast::http::field::if_none_match);
283 BMCWEB_LOG_DEBUG("Setting expected hash {}", expected);
284 if (!expected.empty())
285 {
286 asyncResp->res.setExpectedHash(expected);
287 }
Ed Tanous325310d2024-03-15 09:05:04 -0700288 handler->handle(thisReq, asyncResp);
289 }
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800290 return 0;
291 }
292
Ed Tanous325310d2024-03-15 09:05:04 -0700293 int onDataChunkRecvCallback(uint8_t /*flags*/, int32_t streamId,
294 const uint8_t* data, size_t len)
295 {
296 auto thisStream = streams.find(streamId);
297 if (thisStream == streams.end())
298 {
299 BMCWEB_LOG_ERROR("Unknown stream{}", streamId);
300 close();
301 return -1;
302 }
Ed Tanousd547d8d2024-03-16 18:04:41 -0700303
304 std::optional<bmcweb::HttpBody::reader>& reqReader =
305 thisStream->second.reqReader;
306 if (!reqReader)
Ed Tanous325310d2024-03-15 09:05:04 -0700307 {
Ed Tanous8e5cc7b2024-04-02 11:00:54 -0700308 reqReader.emplace(
309 bmcweb::HttpBody::reader(thisStream->second.req.req.base(),
310 thisStream->second.req.req.body()));
Ed Tanous325310d2024-03-15 09:05:04 -0700311 }
312 boost::beast::error_code ec;
Ed Tanousd547d8d2024-03-16 18:04:41 -0700313 reqReader->put(boost::asio::const_buffer(data, len), ec);
Ed Tanous325310d2024-03-15 09:05:04 -0700314 if (ec)
315 {
316 BMCWEB_LOG_CRITICAL("Failed to write payload");
317 return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
318 }
319 return 0;
320 }
321
322 static int onDataChunkRecvStatic(nghttp2_session* /* session */,
323 uint8_t flags, int32_t streamId,
324 const uint8_t* data, size_t len,
325 void* userData)
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 }
333 return userPtrToSelf(userData).onDataChunkRecvCallback(flags, streamId,
334 data, len);
335 }
336
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800337 int onFrameRecvCallback(const nghttp2_frame& frame)
338 {
Ed Tanous62598e32023-07-17 17:06:25 -0700339 BMCWEB_LOG_DEBUG("frame type {}", static_cast<int>(frame.hd.type));
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800340 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 Tanous62598e32023-07-17 17:06:25 -0700360 BMCWEB_LOG_DEBUG("on_frame_recv_callback");
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800361 if (userData == nullptr)
362 {
Ed Tanous62598e32023-07-17 17:06:25 -0700363 BMCWEB_LOG_CRITICAL("user data was null?");
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800364 return NGHTTP2_ERR_CALLBACK_FAILURE;
365 }
366 if (frame == nullptr)
367 {
Ed Tanous62598e32023-07-17 17:06:25 -0700368 BMCWEB_LOG_CRITICAL("frame was null?");
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800369 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 Tanous62598e32023-07-17 17:06:25 -0700386 BMCWEB_LOG_DEBUG("on_stream_close_callback stream {}", streamId);
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800387 if (userData == nullptr)
388 {
Ed Tanous62598e32023-07-17 17:06:25 -0700389 BMCWEB_LOG_CRITICAL("user data was null?");
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800390 return NGHTTP2_ERR_CALLBACK_FAILURE;
391 }
Ed Tanousf42e8592023-08-25 10:47:44 -0700392 if (userPtrToSelf(userData).streams.erase(streamId) <= 0)
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800393 {
394 return -1;
395 }
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800396 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 Tanous62598e32023-07-17 17:06:25 -0700410 BMCWEB_LOG_DEBUG("on_header_callback name: {} value {}", nameSv,
411 valueSv);
Ed Tanousa07e9812024-03-19 10:31:13 -0700412 if (frame.hd.type != NGHTTP2_HEADERS)
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800413 {
Ed Tanousa07e9812024-03-19 10:31:13 -0700414 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 Tanousfca2cbe2021-01-28 14:49:59 -0800427
Ed Tanousa07e9812024-03-19 10:31:13 -0700428 crow::Request& thisReq = thisStream->second.req;
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800429
Ed Tanousa07e9812024-03-19 10:31:13 -0700430 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);
441 close();
442 return -1;
443 }
Myung Bae1873a042024-04-01 09:27:39 -0500444 thisReq.method(verb);
Ed Tanousa07e9812024-03-19 10:31:13 -0700445 }
446 else if (nameSv == ":scheme")
447 {
448 // Nothing to check on scheme
449 }
450 else
451 {
Myung Bae1873a042024-04-01 09:27:39 -0500452 thisReq.addHeader(nameSv, valueSv);
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800453 }
454 return 0;
455 }
456
457 static int onHeaderCallbackStatic(nghttp2_session* /* session */,
458 const nghttp2_frame* frame,
459 const uint8_t* name, size_t namelen,
460 const uint8_t* value, size_t vallen,
461 uint8_t /* flags */, void* userData)
462 {
463 if (userData == nullptr)
464 {
Ed Tanous62598e32023-07-17 17:06:25 -0700465 BMCWEB_LOG_CRITICAL("user data was null?");
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800466 return NGHTTP2_ERR_CALLBACK_FAILURE;
467 }
468 if (frame == nullptr)
469 {
Ed Tanous62598e32023-07-17 17:06:25 -0700470 BMCWEB_LOG_CRITICAL("frame was null?");
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800471 return NGHTTP2_ERR_CALLBACK_FAILURE;
472 }
473 if (name == nullptr)
474 {
Ed Tanous62598e32023-07-17 17:06:25 -0700475 BMCWEB_LOG_CRITICAL("name was null?");
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800476 return NGHTTP2_ERR_CALLBACK_FAILURE;
477 }
478 if (value == nullptr)
479 {
Ed Tanous62598e32023-07-17 17:06:25 -0700480 BMCWEB_LOG_CRITICAL("value was null?");
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800481 return NGHTTP2_ERR_CALLBACK_FAILURE;
482 }
483 return userPtrToSelf(userData).onHeaderCallback(*frame, {name, namelen},
484 {value, vallen});
485 }
486
487 int onBeginHeadersCallback(const nghttp2_frame& frame)
488 {
489 if (frame.hd.type == NGHTTP2_HEADERS &&
490 frame.headers.cat == NGHTTP2_HCAT_REQUEST)
491 {
Ed Tanous62598e32023-07-17 17:06:25 -0700492 BMCWEB_LOG_DEBUG("create stream for id {}", frame.hd.stream_id);
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800493
Ed Tanousf42e8592023-08-25 10:47:44 -0700494 Http2StreamData& stream = streams[frame.hd.stream_id];
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800495 // http2 is by definition always tls
Ed Tanousf42e8592023-08-25 10:47:44 -0700496 stream.req.isSecure = true;
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800497 }
498 return 0;
499 }
500
501 static int onBeginHeadersCallbackStatic(nghttp2_session* /* session */,
502 const nghttp2_frame* frame,
503 void* userData)
504 {
Ed Tanous62598e32023-07-17 17:06:25 -0700505 BMCWEB_LOG_DEBUG("on_begin_headers_callback");
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800506 if (userData == nullptr)
507 {
Ed Tanous62598e32023-07-17 17:06:25 -0700508 BMCWEB_LOG_CRITICAL("user data was null?");
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800509 return NGHTTP2_ERR_CALLBACK_FAILURE;
510 }
511 if (frame == nullptr)
512 {
Ed Tanous62598e32023-07-17 17:06:25 -0700513 BMCWEB_LOG_CRITICAL("frame was null?");
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800514 return NGHTTP2_ERR_CALLBACK_FAILURE;
515 }
516 return userPtrToSelf(userData).onBeginHeadersCallback(*frame);
517 }
518
519 static void afterWriteBuffer(const std::shared_ptr<self_type>& self,
520 const boost::system::error_code& ec,
521 size_t sendLength)
522 {
523 self->isWriting = false;
Ed Tanous62598e32023-07-17 17:06:25 -0700524 BMCWEB_LOG_DEBUG("Sent {}", sendLength);
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800525 if (ec)
526 {
527 self->close();
528 return;
529 }
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800530 self->writeBuffer();
531 }
532
533 void writeBuffer()
534 {
535 if (isWriting)
536 {
537 return;
538 }
Ed Tanousd0882182024-01-26 23:45:25 -0800539 std::span<const uint8_t> data = ngSession.memSend();
540 if (data.empty())
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800541 {
542 return;
543 }
544 isWriting = true;
Ed Tanous325310d2024-03-15 09:05:04 -0700545 boost::asio::async_write(
546 adaptor, boost::asio::const_buffer(data.data(), data.size()),
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800547 std::bind_front(afterWriteBuffer, shared_from_this()));
548 }
549
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800550 void close()
551 {
552 if constexpr (std::is_same_v<Adaptor,
Ed Tanous003301a2024-04-16 09:59:19 -0700553 boost::asio::ssl::stream<
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800554 boost::asio::ip::tcp::socket>>)
555 {
556 adaptor.next_layer().close();
557 }
558 else
559 {
560 adaptor.close();
561 }
562 }
563
Ed Tanousd0882182024-01-26 23:45:25 -0800564 void afterDoRead(const std::shared_ptr<self_type>& /*self*/,
565 const boost::system::error_code& ec,
566 size_t bytesTransferred)
567 {
568 BMCWEB_LOG_DEBUG("{} async_read_some {} Bytes", logPtr(this),
569 bytesTransferred);
570
571 if (ec)
572 {
573 BMCWEB_LOG_ERROR("{} Error while reading: {}", logPtr(this),
574 ec.message());
575 close();
576 BMCWEB_LOG_DEBUG("{} from read(1)", logPtr(this));
577 return;
578 }
579 std::span<uint8_t> bufferSpan{inBuffer.data(), bytesTransferred};
580
581 ssize_t readLen = ngSession.memRecv(bufferSpan);
582 if (readLen < 0)
583 {
584 BMCWEB_LOG_ERROR("nghttp2_session_mem_recv returned {}", readLen);
585 close();
586 return;
587 }
588 writeBuffer();
589
590 doRead();
591 }
592
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800593 void doRead()
594 {
Ed Tanous62598e32023-07-17 17:06:25 -0700595 BMCWEB_LOG_DEBUG("{} doRead", logPtr(this));
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800596 adaptor.async_read_some(
Ed Tanousd0882182024-01-26 23:45:25 -0800597 boost::asio::buffer(inBuffer),
598 std::bind_front(&self_type::afterDoRead, this, shared_from_this()));
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800599 }
600
601 // A mapping from http2 stream ID to Stream Data
Ed Tanous52e31622024-01-23 16:31:11 -0800602 std::map<int32_t, Http2StreamData> streams;
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800603
Ed Tanousd0882182024-01-26 23:45:25 -0800604 std::array<uint8_t, 8192> inBuffer{};
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800605
606 Adaptor adaptor;
607 bool isWriting = false;
608
609 nghttp2_session ngSession;
610
611 Handler* handler;
612 std::function<std::string()>& getCachedDateStr;
613
614 using std::enable_shared_from_this<
615 HTTP2Connection<Adaptor, Handler>>::shared_from_this;
616
617 using std::enable_shared_from_this<
618 HTTP2Connection<Adaptor, Handler>>::weak_from_this;
619};
620} // namespace crow