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_PUSH_TO_HPP 10   #ifndef BOOST_HTTP_IO_PUSH_TO_HPP
11   #define BOOST_HTTP_IO_PUSH_TO_HPP 11   #define BOOST_HTTP_IO_PUSH_TO_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_source.hpp> 16   #include <boost/http/concept/buffer_source.hpp>
17   #include <boost/http/concept/write_sink.hpp> 17   #include <boost/http/concept/write_sink.hpp>
18   #include <boost/capy/concept/write_stream.hpp> 18   #include <boost/capy/concept/write_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 BufferSource to a WriteSink. 28   /** Transfer data from a BufferSource to a WriteSink.
29   29  
30   This function pulls data from the source and writes it to the 30   This function pulls data from the source and writes it to the
31   sink until the source is exhausted or an error occurs. When 31   sink until the source is exhausted or an error occurs. When
32   the source signals completion, `write_eof()` is called on the 32   the source signals completion, `write_eof()` is called on the
33   sink to finalize the transfer. 33   sink to finalize the transfer.
34   34  
35   @tparam Src The source type, must satisfy @ref BufferSource. 35   @tparam Src The source type, must satisfy @ref BufferSource.
36   @tparam Sink The sink type, must satisfy @ref WriteSink. 36   @tparam Sink The sink type, must satisfy @ref WriteSink.
37   37  
38   @param source The source to pull data from. 38   @param source The source to pull data from.
39   @param sink The sink to write data to. 39   @param sink The sink to write data to.
40   40  
41   @return A task that yields `(std::error_code, std::size_t)`. 41   @return A task that yields `(std::error_code, std::size_t)`.
42   On success, `ec` is default-constructed (no error) and `n` is 42   On success, `ec` is default-constructed (no error) and `n` is
43   the total number of bytes transferred. On error, `ec` contains 43   the total number of bytes transferred. On error, `ec` contains
44   the error code and `n` is the total number of bytes transferred 44   the error code and `n` is the total number of bytes transferred
45   before the error. 45   before the error.
46   46  
47   @par Example 47   @par Example
48   @code 48   @code
49   task<void> transfer_body(BufferSource auto& source, WriteSink auto& sink) 49   task<void> transfer_body(BufferSource auto& source, WriteSink auto& sink)
50   { 50   {
51   auto [ec, n] = co_await push_to(source, sink); 51   auto [ec, n] = co_await push_to(source, sink);
52   if (ec) 52   if (ec)
53   { 53   {
54   // Handle error 54   // Handle error
55   } 55   }
56   // n bytes were transferred 56   // n bytes were transferred
57   } 57   }
58   @endcode 58   @endcode
59   59  
60   @see BufferSource, WriteSink 60   @see BufferSource, WriteSink
61   */ 61   */
62   template<BufferSource Src, WriteSink Sink> 62   template<BufferSource Src, WriteSink Sink>
63   capy::task<capy::results<std::size_t>> 63   capy::task<capy::results<std::size_t>>
HITCBC 64   140 push_to(Src& source, Sink& sink) 64   140 push_to(Src& source, Sink& sink)
65   { 65   {
66   capy::const_buffer arr[capy::detail::max_iovec_]; 66   capy::const_buffer arr[capy::detail::max_iovec_];
67   std::size_t total = 0; 67   std::size_t total = 0;
68   68  
69   for(;;) 69   for(;;)
70   { 70   {
71   auto [ec, bufs] = co_await source.pull(arr); 71   auto [ec, bufs] = co_await source.pull(arr);
72   if(ec == capy::cond::eof) 72   if(ec == capy::cond::eof)
73   { 73   {
74   auto [eof_ec] = co_await sink.write_eof(); 74   auto [eof_ec] = co_await sink.write_eof();
75   co_return {eof_ec, total}; 75   co_return {eof_ec, total};
76   } 76   }
77   if(ec) 77   if(ec)
78   co_return {ec, total}; 78   co_return {ec, total};
79   79  
80   auto [write_ec, n] = co_await sink.write(bufs); 80   auto [write_ec, n] = co_await sink.write(bufs);
81   total += n; 81   total += n;
82   source.consume(n); 82   source.consume(n);
83   if(write_ec) 83   if(write_ec)
84   co_return {write_ec, total}; 84   co_return {write_ec, total};
85   } 85   }
HITCBC 86   280 } 86   280 }
87   87  
88   /** Transfer data from a BufferSource to a capy::WriteStream. 88   /** Transfer data from a BufferSource to a capy::WriteStream.
89   89  
90   This function pulls data from the source and writes it to the 90   This function pulls data from the source and writes it to the
91   stream until the source is exhausted or an error occurs. The 91   stream until the source is exhausted or an error occurs. The
92   stream uses `write_some()` which may perform partial writes, 92   stream uses `write_some()` which may perform partial writes,
93   so this function loops until all pulled data is consumed. 93   so this function loops until all pulled data is consumed.
94   94  
95   Unlike the WriteSink overload, this function does not signal 95   Unlike the WriteSink overload, this function does not signal
96   EOF explicitly since capy::WriteStream does not provide a write_eof 96   EOF explicitly since capy::WriteStream does not provide a write_eof
97   method. The transfer completes when the source is exhausted. 97   method. The transfer completes when the source is exhausted.
98   98  
99   @tparam Src The source type, must satisfy @ref BufferSource. 99   @tparam Src The source type, must satisfy @ref BufferSource.
100   @tparam Stream The stream type, must satisfy @ref capy::WriteStream. 100   @tparam Stream The stream type, must satisfy @ref capy::WriteStream.
101   101  
102   @param source The source to pull data from. 102   @param source The source to pull data from.
103   @param stream The stream to write data to. 103   @param stream The stream to write data to.
104   104  
105   @return A task that yields `(std::error_code, std::size_t)`. 105   @return A task that yields `(std::error_code, std::size_t)`.
106   On success, `ec` is default-constructed (no error) and `n` is 106   On success, `ec` is default-constructed (no error) and `n` is
107   the total number of bytes transferred. On error, `ec` contains 107   the total number of bytes transferred. On error, `ec` contains
108   the error code and `n` is the total number of bytes transferred 108   the error code and `n` is the total number of bytes transferred
109   before the error. 109   before the error.
110   110  
111   @par Example 111   @par Example
112   @code 112   @code
113   task<void> transfer_body(BufferSource auto& source, capy::WriteStream auto& stream) 113   task<void> transfer_body(BufferSource auto& source, capy::WriteStream auto& stream)
114   { 114   {
115   auto [ec, n] = co_await push_to(source, stream); 115   auto [ec, n] = co_await push_to(source, stream);
116   if (ec) 116   if (ec)
117   { 117   {
118   // Handle error 118   // Handle error
119   } 119   }
120   // n bytes were transferred 120   // n bytes were transferred
121   } 121   }
122   @endcode 122   @endcode
123   123  
124   @see BufferSource, capy::WriteStream, pull_from 124   @see BufferSource, capy::WriteStream, pull_from
125   */ 125   */
126   template<BufferSource Src, capy::WriteStream Stream> 126   template<BufferSource Src, capy::WriteStream Stream>
127   requires (!WriteSink<Stream>) 127   requires (!WriteSink<Stream>)
128   capy::task<capy::results<std::size_t>> 128   capy::task<capy::results<std::size_t>>
HITCBC 129   104 push_to(Src& source, Stream& stream) 129   104 push_to(Src& source, Stream& stream)
130   { 130   {
131   capy::const_buffer arr[capy::detail::max_iovec_]; 131   capy::const_buffer arr[capy::detail::max_iovec_];
132   std::size_t total = 0; 132   std::size_t total = 0;
133   133  
134   for(;;) 134   for(;;)
135   { 135   {
136   auto [ec, bufs] = co_await source.pull(arr); 136   auto [ec, bufs] = co_await source.pull(arr);
137   if(ec == capy::cond::eof) 137   if(ec == capy::cond::eof)
138   co_return {std::error_code(), total}; 138   co_return {std::error_code(), total};
139   if(ec) 139   if(ec)
140   co_return {ec, total}; 140   co_return {ec, total};
141   141  
142   auto [write_ec, n] = co_await stream.write_some(bufs); 142   auto [write_ec, n] = co_await stream.write_some(bufs);
143   total += n; 143   total += n;
144   source.consume(n); 144   source.consume(n);
145   if(write_ec) 145   if(write_ec)
146   co_return {write_ec, total}; 146   co_return {write_ec, total};
147   } 147   }
HITCBC 148   208 } 148   208 }
149   149  
150   } // namespace http 150   } // namespace http
151   } // namespace boost 151   } // namespace boost
152   152  
153   #endif 153   #endif