include/boost/http/io/pull_from.hpp

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