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