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 |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 3 | #pragma once |
| 4 | |
Ed Tanous | f51d863 | 2024-05-16 09:14:01 -0700 | [diff] [blame] | 5 | #include "duplicatable_file_handle.hpp" |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 6 | #include "logging.hpp" |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 7 | #include "utility.hpp" |
| 8 | |
Ed Tanous | 88c7c42 | 2024-04-06 08:52:40 -0700 | [diff] [blame] | 9 | #include <fcntl.h> |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 10 | #include <unistd.h> |
| 11 | |
| 12 | #include <boost/beast/core/buffers_range.hpp> |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 13 | #include <boost/beast/core/file_posix.hpp> |
| 14 | #include <boost/beast/http/message.hpp> |
| 15 | #include <boost/system/error_code.hpp> |
| 16 | |
Potin Lai | 608fb7b | 2024-08-09 16:05:06 +0800 | [diff] [blame] | 17 | #include <cstdint> |
| 18 | #include <optional> |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 19 | #include <string_view> |
| 20 | |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 21 | namespace bmcweb |
| 22 | { |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 23 | struct HttpBody |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 24 | { |
Ed Tanous | 0501696 | 2024-03-19 11:53:11 -0700 | [diff] [blame] | 25 | // Body concept requires specific naming of classes |
| 26 | // NOLINTBEGIN(readability-identifier-naming) |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 27 | class writer; |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 28 | class reader; |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 29 | class value_type; |
Ed Tanous | 0501696 | 2024-03-19 11:53:11 -0700 | [diff] [blame] | 30 | // NOLINTEND(readability-identifier-naming) |
Potin Lai | 608fb7b | 2024-08-09 16:05:06 +0800 | [diff] [blame] | 31 | |
| 32 | static std::uint64_t size(const value_type& body); |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | enum class EncodingType |
| 36 | { |
| 37 | Raw, |
| 38 | Base64, |
| 39 | }; |
| 40 | |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 41 | class HttpBody::value_type |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 42 | { |
Ed Tanous | f51d863 | 2024-05-16 09:14:01 -0700 | [diff] [blame] | 43 | DuplicatableFileHandle fileHandle; |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 44 | std::optional<size_t> fileSize; |
| 45 | std::string strBody; |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 46 | |
| 47 | public: |
Ed Tanous | f51d863 | 2024-05-16 09:14:01 -0700 | [diff] [blame] | 48 | value_type() = default; |
| 49 | explicit value_type(std::string_view s) : strBody(s) {} |
| 50 | explicit value_type(EncodingType e) : encodingType(e) {} |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 51 | EncodingType encodingType = EncodingType::Raw; |
| 52 | |
Ed Tanous | f51d863 | 2024-05-16 09:14:01 -0700 | [diff] [blame] | 53 | const boost::beast::file_posix& file() const |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 54 | { |
Ed Tanous | f51d863 | 2024-05-16 09:14:01 -0700 | [diff] [blame] | 55 | return fileHandle.fileHandle; |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 56 | } |
| 57 | |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 58 | std::string& str() |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 59 | { |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 60 | return strBody; |
| 61 | } |
| 62 | |
| 63 | const std::string& str() const |
| 64 | { |
| 65 | return strBody; |
| 66 | } |
| 67 | |
| 68 | std::optional<size_t> payloadSize() const |
| 69 | { |
Ed Tanous | f51d863 | 2024-05-16 09:14:01 -0700 | [diff] [blame] | 70 | if (!fileHandle.fileHandle.is_open()) |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 71 | { |
| 72 | return strBody.size(); |
| 73 | } |
| 74 | if (fileSize) |
| 75 | { |
| 76 | if (encodingType == EncodingType::Base64) |
| 77 | { |
| 78 | return crow::utility::Base64Encoder::encodedSize(*fileSize); |
| 79 | } |
| 80 | } |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 81 | return fileSize; |
| 82 | } |
| 83 | |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 84 | void clear() |
| 85 | { |
| 86 | strBody.clear(); |
| 87 | strBody.shrink_to_fit(); |
Ed Tanous | f51d863 | 2024-05-16 09:14:01 -0700 | [diff] [blame] | 88 | fileHandle.fileHandle = boost::beast::file_posix(); |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 89 | fileSize = std::nullopt; |
Ed Tanous | 06fc9be | 2024-03-27 19:58:11 -0700 | [diff] [blame] | 90 | encodingType = EncodingType::Raw; |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 93 | void open(const char* path, boost::beast::file_mode mode, |
| 94 | boost::system::error_code& ec) |
| 95 | { |
Ed Tanous | f51d863 | 2024-05-16 09:14:01 -0700 | [diff] [blame] | 96 | fileHandle.fileHandle.open(path, mode, ec); |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 97 | if (ec) |
| 98 | { |
| 99 | return; |
| 100 | } |
| 101 | boost::system::error_code ec2; |
Ed Tanous | f51d863 | 2024-05-16 09:14:01 -0700 | [diff] [blame] | 102 | uint64_t size = fileHandle.fileHandle.size(ec2); |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 103 | if (!ec2) |
| 104 | { |
| 105 | BMCWEB_LOG_INFO("File size was {} bytes", size); |
| 106 | fileSize = static_cast<size_t>(size); |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | BMCWEB_LOG_WARNING("Failed to read file size on {}", path); |
| 111 | } |
Ed Tanous | 88c7c42 | 2024-04-06 08:52:40 -0700 | [diff] [blame] | 112 | |
Ed Tanous | f51d863 | 2024-05-16 09:14:01 -0700 | [diff] [blame] | 113 | int fadvise = posix_fadvise(fileHandle.fileHandle.native_handle(), 0, 0, |
Ed Tanous | 88c7c42 | 2024-04-06 08:52:40 -0700 | [diff] [blame] | 114 | POSIX_FADV_SEQUENTIAL); |
| 115 | if (fadvise != 0) |
| 116 | { |
| 117 | BMCWEB_LOG_WARNING("Fasvise returned {} ignoring", fadvise); |
| 118 | } |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 119 | ec = {}; |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void setFd(int fd, boost::system::error_code& ec) |
| 123 | { |
Ed Tanous | f51d863 | 2024-05-16 09:14:01 -0700 | [diff] [blame] | 124 | fileHandle.fileHandle.native_handle(fd); |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 125 | |
| 126 | boost::system::error_code ec2; |
Ed Tanous | f51d863 | 2024-05-16 09:14:01 -0700 | [diff] [blame] | 127 | uint64_t size = fileHandle.fileHandle.size(ec2); |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 128 | if (!ec2) |
| 129 | { |
| 130 | if (size != 0 && size < std::numeric_limits<size_t>::max()) |
| 131 | { |
| 132 | fileSize = static_cast<size_t>(size); |
| 133 | } |
| 134 | } |
| 135 | ec = {}; |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 136 | } |
| 137 | }; |
| 138 | |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 139 | class HttpBody::writer |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 140 | { |
| 141 | public: |
| 142 | using const_buffers_type = boost::asio::const_buffer; |
| 143 | |
| 144 | private: |
| 145 | std::string buf; |
| 146 | crow::utility::Base64Encoder encoder; |
| 147 | |
| 148 | value_type& body; |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 149 | size_t sent = 0; |
Ed Tanous | e428b44 | 2024-03-29 10:49:27 -0700 | [diff] [blame] | 150 | // 64KB This number is arbitrary, and selected to try to optimize for larger |
| 151 | // files and fewer loops over per-connection reduction in memory usage. |
| 152 | // Nginx uses 16-32KB here, so we're in the range of what other webservers |
| 153 | // do. |
| 154 | constexpr static size_t readBufSize = 1024UL * 64UL; |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 155 | std::array<char, readBufSize> fileReadBuf{}; |
| 156 | |
| 157 | public: |
| 158 | template <bool IsRequest, class Fields> |
| 159 | writer(boost::beast::http::header<IsRequest, Fields>& /*header*/, |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 160 | value_type& bodyIn) : body(bodyIn) |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 161 | {} |
| 162 | |
| 163 | static void init(boost::beast::error_code& ec) |
| 164 | { |
| 165 | ec = {}; |
| 166 | } |
| 167 | |
| 168 | boost::optional<std::pair<const_buffers_type, bool>> |
| 169 | get(boost::beast::error_code& ec) |
| 170 | { |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 171 | return getWithMaxSize(ec, std::numeric_limits<size_t>::max()); |
| 172 | } |
| 173 | |
| 174 | boost::optional<std::pair<const_buffers_type, bool>> |
| 175 | getWithMaxSize(boost::beast::error_code& ec, size_t maxSize) |
| 176 | { |
| 177 | std::pair<const_buffers_type, bool> ret; |
| 178 | if (!body.file().is_open()) |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 179 | { |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 180 | size_t remain = body.str().size() - sent; |
| 181 | size_t toReturn = std::min(maxSize, remain); |
| 182 | ret.first = const_buffers_type(&body.str()[sent], toReturn); |
| 183 | |
| 184 | sent += toReturn; |
| 185 | ret.second = sent < body.str().size(); |
| 186 | BMCWEB_LOG_INFO("Returning {} bytes more={}", ret.first.size(), |
| 187 | ret.second); |
| 188 | return ret; |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 189 | } |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 190 | size_t readReq = std::min(fileReadBuf.size(), maxSize); |
Ed Tanous | 0242baf | 2024-05-16 19:52:47 -0700 | [diff] [blame] | 191 | BMCWEB_LOG_INFO("Reading {}", readReq); |
| 192 | boost::system::error_code readEc; |
| 193 | size_t read = body.file().read(fileReadBuf.data(), readReq, readEc); |
| 194 | if (readEc) |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 195 | { |
Ed Tanous | 0242baf | 2024-05-16 19:52:47 -0700 | [diff] [blame] | 196 | if (readEc != boost::system::errc::operation_would_block && |
| 197 | readEc != boost::system::errc::resource_unavailable_try_again) |
| 198 | { |
| 199 | BMCWEB_LOG_CRITICAL("Failed to read from file {}", |
| 200 | readEc.message()); |
| 201 | ec = readEc; |
| 202 | return boost::none; |
| 203 | } |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 204 | } |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 205 | |
| 206 | std::string_view chunkView(fileReadBuf.data(), read); |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 207 | BMCWEB_LOG_INFO("Read {} bytes from file", read); |
| 208 | // If the number of bytes read equals the amount requested, we haven't |
| 209 | // reached EOF yet |
| 210 | ret.second = read == readReq; |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 211 | if (body.encodingType == EncodingType::Base64) |
| 212 | { |
| 213 | buf.clear(); |
| 214 | buf.reserve( |
| 215 | crow::utility::Base64Encoder::encodedSize(chunkView.size())); |
| 216 | encoder.encode(chunkView, buf); |
| 217 | if (!ret.second) |
| 218 | { |
| 219 | encoder.finalize(buf); |
| 220 | } |
| 221 | ret.first = const_buffers_type(buf.data(), buf.size()); |
| 222 | } |
| 223 | else |
| 224 | { |
| 225 | ret.first = const_buffers_type(chunkView.data(), chunkView.size()); |
| 226 | } |
| 227 | return ret; |
| 228 | } |
| 229 | }; |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 230 | |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 231 | class HttpBody::reader |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 232 | { |
| 233 | value_type& value; |
| 234 | |
| 235 | public: |
| 236 | template <bool IsRequest, class Fields> |
| 237 | reader(boost::beast::http::header<IsRequest, Fields>& /*headers*/, |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 238 | value_type& body) : value(body) |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 239 | {} |
| 240 | |
| 241 | void init(const boost::optional<std::uint64_t>& contentLength, |
| 242 | boost::beast::error_code& ec) |
| 243 | { |
| 244 | if (contentLength) |
| 245 | { |
| 246 | if (!value.file().is_open()) |
| 247 | { |
| 248 | value.str().reserve(static_cast<size_t>(*contentLength)); |
| 249 | } |
| 250 | } |
| 251 | ec = {}; |
| 252 | } |
| 253 | |
| 254 | template <class ConstBufferSequence> |
| 255 | std::size_t put(const ConstBufferSequence& buffers, |
| 256 | boost::system::error_code& ec) |
| 257 | { |
| 258 | size_t extra = boost::beast::buffer_bytes(buffers); |
| 259 | for (const auto b : boost::beast::buffers_range_ref(buffers)) |
| 260 | { |
| 261 | const char* ptr = static_cast<const char*>(b.data()); |
| 262 | value.str() += std::string_view(ptr, b.size()); |
| 263 | } |
| 264 | ec = {}; |
| 265 | return extra; |
| 266 | } |
| 267 | |
| 268 | static void finish(boost::system::error_code& ec) |
| 269 | { |
| 270 | ec = {}; |
| 271 | } |
| 272 | }; |
| 273 | |
Potin Lai | 608fb7b | 2024-08-09 16:05:06 +0800 | [diff] [blame] | 274 | inline std::uint64_t HttpBody::size(const value_type& body) |
| 275 | { |
| 276 | std::optional<size_t> payloadSize = body.payloadSize(); |
| 277 | return payloadSize.value_or(0U); |
| 278 | } |
| 279 | |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 280 | } // namespace bmcweb |