100.00% Lines (4/4)
100.00% Functions (2/2)
| TLA | Baseline | Branch | ||||||
|---|---|---|---|---|---|---|---|---|
| Line | Hits | Code | Line | Hits | Code | |||
| 1 | // | 1 | // | |||||
| 2 | // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) | 2 | // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) | |||||
| 3 | // | 3 | // | |||||
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |||||
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||||
| 6 | // | 6 | // | |||||
| 7 | // Official repository: https://github.com/cppalliance/http | 7 | // Official repository: https://github.com/cppalliance/http | |||||
| 8 | // | 8 | // | |||||
| 9 | 9 | |||||||
| 10 | #ifndef BOOST_HTTP_IO_PULL_FROM_HPP | 10 | #ifndef BOOST_HTTP_IO_PULL_FROM_HPP | |||||
| 11 | #define BOOST_HTTP_IO_PULL_FROM_HPP | 11 | #define BOOST_HTTP_IO_PULL_FROM_HPP | |||||
| 12 | 12 | |||||||
| 13 | #include <boost/capy/detail/config.hpp> | 13 | #include <boost/capy/detail/config.hpp> | |||||
| 14 | #include <boost/capy/buffers.hpp> | 14 | #include <boost/capy/buffers.hpp> | |||||
| 15 | #include <boost/capy/cond.hpp> | 15 | #include <boost/capy/cond.hpp> | |||||
| 16 | #include <boost/http/concept/buffer_sink.hpp> | 16 | #include <boost/http/concept/buffer_sink.hpp> | |||||
| 17 | #include <boost/http/concept/read_source.hpp> | 17 | #include <boost/http/concept/read_source.hpp> | |||||
| 18 | #include <boost/capy/concept/read_stream.hpp> | 18 | #include <boost/capy/concept/read_stream.hpp> | |||||
| 19 | #include <boost/capy/results.hpp> | 19 | #include <boost/capy/results.hpp> | |||||
| 20 | #include <boost/capy/task.hpp> | 20 | #include <boost/capy/task.hpp> | |||||
| 21 | 21 | |||||||
| 22 | #include <cstddef> | 22 | #include <cstddef> | |||||
| 23 | #include <span> | 23 | #include <span> | |||||
| 24 | 24 | |||||||
| 25 | namespace boost { | 25 | namespace boost { | |||||
| 26 | namespace http { | 26 | namespace http { | |||||
| 27 | 27 | |||||||
| 28 | /** Transfer data from a ReadSource to a BufferSink. | 28 | /** Transfer data from a ReadSource to a BufferSink. | |||||
| 29 | 29 | |||||||
| 30 | This function reads data from the source directly into the sink's | 30 | This function reads data from the source directly into the sink's | |||||
| 31 | internal buffers using the callee-owns-buffers model. The sink | 31 | internal buffers using the callee-owns-buffers model. The sink | |||||
| 32 | provides writable buffers via `prepare()`, the source reads into | 32 | provides writable buffers via `prepare()`, the source reads into | |||||
| 33 | them, and the sink commits the data. When the source signals EOF, | 33 | them, and the sink commits the data. When the source signals EOF, | |||||
| 34 | `commit_eof()` is called on the sink to finalize the transfer. | 34 | `commit_eof()` is called on the sink to finalize the transfer. | |||||
| 35 | 35 | |||||||
| 36 | @tparam Src The source type, must satisfy @ref ReadSource. | 36 | @tparam Src The source type, must satisfy @ref ReadSource. | |||||
| 37 | @tparam Sink The sink type, must satisfy @ref BufferSink. | 37 | @tparam Sink The sink type, must satisfy @ref BufferSink. | |||||
| 38 | 38 | |||||||
| 39 | @param source The source to read data from. | 39 | @param source The source to read data from. | |||||
| 40 | @param sink The sink to write data to. | 40 | @param sink The sink to write data to. | |||||
| 41 | 41 | |||||||
| 42 | @return A task that yields `(std::error_code, std::size_t)`. | 42 | @return A task that yields `(std::error_code, std::size_t)`. | |||||
| 43 | On success, `ec` is default-constructed (no error) and `n` is | 43 | On success, `ec` is default-constructed (no error) and `n` is | |||||
| 44 | the total number of bytes transferred. On error, `ec` contains | 44 | the total number of bytes transferred. On error, `ec` contains | |||||
| 45 | the error code and `n` is the total number of bytes transferred | 45 | the error code and `n` is the total number of bytes transferred | |||||
| 46 | before the error. | 46 | before the error. | |||||
| 47 | 47 | |||||||
| 48 | @par Example | 48 | @par Example | |||||
| 49 | @code | 49 | @code | |||||
| 50 | task<void> transfer_body(ReadSource auto& source, BufferSink auto& sink) | 50 | task<void> transfer_body(ReadSource auto& source, BufferSink auto& sink) | |||||
| 51 | { | 51 | { | |||||
| 52 | auto [ec, n] = co_await pull_from(source, sink); | 52 | auto [ec, n] = co_await pull_from(source, sink); | |||||
| 53 | if (ec) | 53 | if (ec) | |||||
| 54 | { | 54 | { | |||||
| 55 | // Handle error | 55 | // Handle error | |||||
| 56 | } | 56 | } | |||||
| 57 | // n bytes were transferred | 57 | // n bytes were transferred | |||||
| 58 | } | 58 | } | |||||
| 59 | @endcode | 59 | @endcode | |||||
| 60 | 60 | |||||||
| 61 | @see ReadSource, BufferSink, push_to | 61 | @see ReadSource, BufferSink, push_to | |||||
| 62 | */ | 62 | */ | |||||
| 63 | template<ReadSource Src, BufferSink Sink> | 63 | template<ReadSource Src, BufferSink Sink> | |||||
| 64 | capy::task<capy::results<std::size_t>> | 64 | capy::task<capy::results<std::size_t>> | |||||
| HITCBC | 65 | 136 | pull_from(Src& source, Sink& sink) | 65 | 136 | pull_from(Src& source, Sink& sink) | ||
| 66 | { | 66 | { | |||||
| 67 | capy::mutable_buffer dst_arr[capy::detail::max_iovec_]; | 67 | capy::mutable_buffer dst_arr[capy::detail::max_iovec_]; | |||||
| 68 | std::size_t total = 0; | 68 | std::size_t total = 0; | |||||
| 69 | 69 | |||||||
| 70 | for(;;) | 70 | for(;;) | |||||
| 71 | { | 71 | { | |||||
| 72 | auto dst_bufs = sink.prepare(dst_arr); | 72 | auto dst_bufs = sink.prepare(dst_arr); | |||||
| 73 | if(dst_bufs.empty()) | 73 | if(dst_bufs.empty()) | |||||
| 74 | { | 74 | { | |||||
| 75 | // No buffer space available; commit nothing to flush | 75 | // No buffer space available; commit nothing to flush | |||||
| 76 | auto [flush_ec] = co_await sink.commit(0); | 76 | auto [flush_ec] = co_await sink.commit(0); | |||||
| 77 | if(flush_ec) | 77 | if(flush_ec) | |||||
| 78 | co_return {flush_ec, total}; | 78 | co_return {flush_ec, total}; | |||||
| 79 | continue; | 79 | continue; | |||||
| 80 | } | 80 | } | |||||
| 81 | 81 | |||||||
| 82 | auto [ec, n] = co_await source.read( | 82 | auto [ec, n] = co_await source.read( | |||||
| 83 | std::span<capy::mutable_buffer const>(dst_bufs)); | 83 | std::span<capy::mutable_buffer const>(dst_bufs)); | |||||
| 84 | 84 | |||||||
| 85 | auto [commit_ec] = co_await sink.commit(n); | 85 | auto [commit_ec] = co_await sink.commit(n); | |||||
| 86 | total += n; | 86 | total += n; | |||||
| 87 | 87 | |||||||
| 88 | if(commit_ec) | 88 | if(commit_ec) | |||||
| 89 | co_return {commit_ec, total}; | 89 | co_return {commit_ec, total}; | |||||
| 90 | 90 | |||||||
| 91 | if(ec == capy::cond::eof) | 91 | if(ec == capy::cond::eof) | |||||
| 92 | { | 92 | { | |||||
| 93 | auto [eof_ec] = co_await sink.commit_eof(0); | 93 | auto [eof_ec] = co_await sink.commit_eof(0); | |||||
| 94 | co_return {eof_ec, total}; | 94 | co_return {eof_ec, total}; | |||||
| 95 | } | 95 | } | |||||
| 96 | 96 | |||||||
| 97 | if(ec) | 97 | if(ec) | |||||
| 98 | co_return {ec, total}; | 98 | co_return {ec, total}; | |||||
| 99 | } | 99 | } | |||||
| HITCBC | 100 | 272 | } | 100 | 272 | } | ||
| 101 | 101 | |||||||
| 102 | /** Transfer data from a capy::ReadStream to a BufferSink. | 102 | /** Transfer data from a capy::ReadStream to a BufferSink. | |||||
| 103 | 103 | |||||||
| 104 | This function reads data from the stream directly into the sink's | 104 | This function reads data from the stream directly into the sink's | |||||
| 105 | internal buffers using the callee-owns-buffers model. The sink | 105 | internal buffers using the callee-owns-buffers model. The sink | |||||
| 106 | provides writable buffers via `prepare()`, the stream reads into | 106 | provides writable buffers via `prepare()`, the stream reads into | |||||
| 107 | them using `read_some()`, and the sink commits the data. When the | 107 | them using `read_some()`, and the sink commits the data. When the | |||||
| 108 | stream signals EOF, `commit_eof()` is called on the sink to | 108 | stream signals EOF, `commit_eof()` is called on the sink to | |||||
| 109 | finalize the transfer. | 109 | finalize the transfer. | |||||
| 110 | 110 | |||||||
| 111 | This overload handles partial reads from the stream, committing | 111 | This overload handles partial reads from the stream, committing | |||||
| 112 | data incrementally as it arrives. It loops until EOF is encountered | 112 | data incrementally as it arrives. It loops until EOF is encountered | |||||
| 113 | or an error occurs. | 113 | or an error occurs. | |||||
| 114 | 114 | |||||||
| 115 | @tparam Src The stream type, must satisfy @ref capy::ReadStream. | 115 | @tparam Src The stream type, must satisfy @ref capy::ReadStream. | |||||
| 116 | @tparam Sink The sink type, must satisfy @ref BufferSink. | 116 | @tparam Sink The sink type, must satisfy @ref BufferSink. | |||||
| 117 | 117 | |||||||
| 118 | @param source The stream to read data from. | 118 | @param source The stream to read data from. | |||||
| 119 | @param sink The sink to write data to. | 119 | @param sink The sink to write data to. | |||||
| 120 | 120 | |||||||
| 121 | @return A task that yields `(std::error_code, std::size_t)`. | 121 | @return A task that yields `(std::error_code, std::size_t)`. | |||||
| 122 | On success, `ec` is default-constructed (no error) and `n` is | 122 | On success, `ec` is default-constructed (no error) and `n` is | |||||
| 123 | the total number of bytes transferred. On error, `ec` contains | 123 | the total number of bytes transferred. On error, `ec` contains | |||||
| 124 | the error code and `n` is the total number of bytes transferred | 124 | the error code and `n` is the total number of bytes transferred | |||||
| 125 | before the error. | 125 | before the error. | |||||
| 126 | 126 | |||||||
| 127 | @par Example | 127 | @par Example | |||||
| 128 | @code | 128 | @code | |||||
| 129 | task<void> transfer_body(capy::ReadStream auto& stream, BufferSink auto& sink) | 129 | task<void> transfer_body(capy::ReadStream auto& stream, BufferSink auto& sink) | |||||
| 130 | { | 130 | { | |||||
| 131 | auto [ec, n] = co_await pull_from(stream, sink); | 131 | auto [ec, n] = co_await pull_from(stream, sink); | |||||
| 132 | if (ec) | 132 | if (ec) | |||||
| 133 | { | 133 | { | |||||
| 134 | // Handle error | 134 | // Handle error | |||||
| 135 | } | 135 | } | |||||
| 136 | // n bytes were transferred | 136 | // n bytes were transferred | |||||
| 137 | } | 137 | } | |||||
| 138 | @endcode | 138 | @endcode | |||||
| 139 | 139 | |||||||
| 140 | @see capy::ReadStream, BufferSink, push_to | 140 | @see capy::ReadStream, BufferSink, push_to | |||||
| 141 | */ | 141 | */ | |||||
| 142 | template<capy::ReadStream Src, BufferSink Sink> | 142 | template<capy::ReadStream Src, BufferSink Sink> | |||||
| 143 | requires (!ReadSource<Src>) | 143 | requires (!ReadSource<Src>) | |||||
| 144 | capy::task<capy::results<std::size_t>> | 144 | capy::task<capy::results<std::size_t>> | |||||
| HITCBC | 145 | 226 | pull_from(Src& source, Sink& sink) | 145 | 226 | pull_from(Src& source, Sink& sink) | ||
| 146 | { | 146 | { | |||||
| 147 | capy::mutable_buffer dst_arr[capy::detail::max_iovec_]; | 147 | capy::mutable_buffer dst_arr[capy::detail::max_iovec_]; | |||||
| 148 | std::size_t total = 0; | 148 | std::size_t total = 0; | |||||
| 149 | 149 | |||||||
| 150 | for(;;) | 150 | for(;;) | |||||
| 151 | { | 151 | { | |||||
| 152 | // Prepare destination buffers from the sink | 152 | // Prepare destination buffers from the sink | |||||
| 153 | auto dst_bufs = sink.prepare(dst_arr); | 153 | auto dst_bufs = sink.prepare(dst_arr); | |||||
| 154 | if(dst_bufs.empty()) | 154 | if(dst_bufs.empty()) | |||||
| 155 | { | 155 | { | |||||
| 156 | // No buffer space available; commit nothing to flush | 156 | // No buffer space available; commit nothing to flush | |||||
| 157 | auto [flush_ec] = co_await sink.commit(0); | 157 | auto [flush_ec] = co_await sink.commit(0); | |||||
| 158 | if(flush_ec) | 158 | if(flush_ec) | |||||
| 159 | co_return {flush_ec, total}; | 159 | co_return {flush_ec, total}; | |||||
| 160 | continue; | 160 | continue; | |||||
| 161 | } | 161 | } | |||||
| 162 | 162 | |||||||
| 163 | // Read data from the stream into the sink's buffers | 163 | // Read data from the stream into the sink's buffers | |||||
| 164 | auto [ec, n] = co_await source.read_some( | 164 | auto [ec, n] = co_await source.read_some( | |||||
| 165 | std::span<capy::mutable_buffer const>(dst_bufs)); | 165 | std::span<capy::mutable_buffer const>(dst_bufs)); | |||||
| 166 | 166 | |||||||
| 167 | auto [commit_ec] = co_await sink.commit(n); | 167 | auto [commit_ec] = co_await sink.commit(n); | |||||
| 168 | total += n; | 168 | total += n; | |||||
| 169 | 169 | |||||||
| 170 | if(commit_ec) | 170 | if(commit_ec) | |||||
| 171 | co_return {commit_ec, total}; | 171 | co_return {commit_ec, total}; | |||||
| 172 | 172 | |||||||
| 173 | if(ec == capy::cond::eof) | 173 | if(ec == capy::cond::eof) | |||||
| 174 | { | 174 | { | |||||
| 175 | auto [eof_ec] = co_await sink.commit_eof(0); | 175 | auto [eof_ec] = co_await sink.commit_eof(0); | |||||
| 176 | co_return {eof_ec, total}; | 176 | co_return {eof_ec, total}; | |||||
| 177 | } | 177 | } | |||||
| 178 | 178 | |||||||
| 179 | // Check for other errors | 179 | // Check for other errors | |||||
| 180 | if(ec) | 180 | if(ec) | |||||
| 181 | co_return {ec, total}; | 181 | co_return {ec, total}; | |||||
| 182 | } | 182 | } | |||||
| HITCBC | 183 | 452 | } | 183 | 452 | } | ||
| 184 | 184 | |||||||
| 185 | } // namespace http | 185 | } // namespace http | |||||
| 186 | } // namespace boost | 186 | } // namespace boost | |||||
| 187 | 187 | |||||||
| 188 | #endif | 188 | #endif | |||||