90.91% Lines (50/55) 100.00% Functions (11/11)
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   // Copyright (c) 2026 Michael Vandeberg 3   // Copyright (c) 2026 Michael Vandeberg
4   // 4   //
5   // Distributed under the Boost Software License, Version 1.0. (See accompanying 5   // Distributed under the Boost Software License, Version 1.0. (See accompanying
6   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7   // 7   //
8   // Official repository: https://github.com/cppalliance/http 8   // Official repository: https://github.com/cppalliance/http
9   // 9   //
10   10  
11   #ifndef BOOST_HTTP_TEST_READ_SOURCE_HPP 11   #ifndef BOOST_HTTP_TEST_READ_SOURCE_HPP
12   #define BOOST_HTTP_TEST_READ_SOURCE_HPP 12   #define BOOST_HTTP_TEST_READ_SOURCE_HPP
13 - #include <boost/capy/detail/config.hpp>  
14   13  
15   #include <boost/capy/buffers.hpp> 14   #include <boost/capy/buffers.hpp>
16   #include <boost/capy/buffers/buffer_copy.hpp> 15   #include <boost/capy/buffers/buffer_copy.hpp>
17   #include <boost/capy/buffers/make_buffer.hpp> 16   #include <boost/capy/buffers/make_buffer.hpp>
18   #include <coroutine> 17   #include <coroutine>
19   #include <boost/capy/ex/await_context.hpp> 18   #include <boost/capy/ex/await_context.hpp>
20   #include <boost/capy/results.hpp> 19   #include <boost/capy/results.hpp>
21   #include <boost/capy/error.hpp> 20   #include <boost/capy/error.hpp>
22   #include <boost/capy/test/fuse.hpp> 21   #include <boost/capy/test/fuse.hpp>
23   22  
24   #include <string> 23   #include <string>
25   #include <string_view> 24   #include <string_view>
26   25  
27   namespace boost { 26   namespace boost {
28   namespace http { 27   namespace http {
29   namespace test { 28   namespace test {
30   29  
31   /** A mock source for testing read operations. 30   /** A mock source for testing read operations.
32   31  
33   Use this to verify code that performs complete reads without needing 32   Use this to verify code that performs complete reads without needing
34   real I/O. Call @ref provide to supply data, then @ref read 33   real I/O. Call @ref provide to supply data, then @ref read
35   to consume it. The associated @ref capy::test::fuse enables error injection 34   to consume it. The associated @ref capy::test::fuse enables error injection
36   at controlled points. 35   at controlled points.
37   36  
38   This class satisfies the @ref ReadSource concept by providing both 37   This class satisfies the @ref ReadSource concept by providing both
39   partial reads via `read_some` (satisfying @ref ReadStream) and 38   partial reads via `read_some` (satisfying @ref ReadStream) and
40   complete reads via `read` that fill the entire buffer sequence 39   complete reads via `read` that fill the entire buffer sequence
41   before returning. 40   before returning.
42   41  
43   @par Thread Safety 42   @par Thread Safety
44   Not thread-safe. 43   Not thread-safe.
45   44  
46   @par Example 45   @par Example
47   @code 46   @code
48   capy::test::fuse f; 47   capy::test::fuse f;
49   read_source rs( f ); 48   read_source rs( f );
50   rs.provide( "Hello, " ); 49   rs.provide( "Hello, " );
51   rs.provide( "World!" ); 50   rs.provide( "World!" );
52   51  
53   auto r = f.armed( [&]( capy::test::fuse& ) -> task<void> { 52   auto r = f.armed( [&]( capy::test::fuse& ) -> task<void> {
54   char buf[32]; 53   char buf[32];
55   auto [ec, n] = co_await rs.read( 54   auto [ec, n] = co_await rs.read(
56   capy::mutable_buffer( buf, sizeof( buf ) ) ); 55   capy::mutable_buffer( buf, sizeof( buf ) ) );
57   if( ec ) 56   if( ec )
58   co_return; 57   co_return;
59   // buf contains "Hello, World!" 58   // buf contains "Hello, World!"
60   } ); 59   } );
61   @endcode 60   @endcode
62   61  
63   @see capy::test::fuse, ReadSource 62   @see capy::test::fuse, ReadSource
64   */ 63   */
65   class read_source 64   class read_source
66   { 65   {
67   capy::test::fuse f_; 66   capy::test::fuse f_;
68   std::string data_; 67   std::string data_;
69   std::size_t pos_ = 0; 68   std::size_t pos_ = 0;
70   std::size_t max_read_size_; 69   std::size_t max_read_size_;
71   70  
72   public: 71   public:
73   /** Construct a read source. 72   /** Construct a read source.
74   73  
75   @param f The capy::test::fuse used to inject errors during reads. 74   @param f The capy::test::fuse used to inject errors during reads.
76   75  
77   @param max_read_size Maximum bytes returned per read. 76   @param max_read_size Maximum bytes returned per read.
78   Use to simulate chunked delivery. 77   Use to simulate chunked delivery.
79   */ 78   */
HITCBC 80   277 explicit read_source( 79   277 explicit read_source(
81   capy::test::fuse f = {}, 80   capy::test::fuse f = {},
82   std::size_t max_read_size = std::size_t(-1)) noexcept 81   std::size_t max_read_size = std::size_t(-1)) noexcept
HITCBC 83   277 : f_(std::move(f)) 82   277 : f_(std::move(f))
HITCBC 84   277 , max_read_size_(max_read_size) 83   277 , max_read_size_(max_read_size)
85   { 84   {
HITCBC 86   277 } 85   277 }
87   86  
88   /** Append data to be returned by subsequent reads. 87   /** Append data to be returned by subsequent reads.
89   88  
90   Multiple calls accumulate data that @ref read returns. 89   Multiple calls accumulate data that @ref read returns.
91   90  
92   @param sv The data to append. 91   @param sv The data to append.
93   */ 92   */
94   void 93   void
HITCBC 95   258 provide(std::string_view sv) 94   258 provide(std::string_view sv)
96   { 95   {
HITCBC 97   258 data_.append(sv); 96   258 data_.append(sv);
HITCBC 98   258 } 97   258 }
99   98  
100   /// Clear all data and reset the read position. 99   /// Clear all data and reset the read position.
101   void 100   void
102   clear() noexcept 101   clear() noexcept
103   { 102   {
104   data_.clear(); 103   data_.clear();
105   pos_ = 0; 104   pos_ = 0;
106   } 105   }
107   106  
108   /// Return the number of bytes available for reading. 107   /// Return the number of bytes available for reading.
109   std::size_t 108   std::size_t
HITCBC 110   8 available() const noexcept 109   8 available() const noexcept
111   { 110   {
HITCBC 112   8 return data_.size() - pos_; 111   8 return data_.size() - pos_;
113   } 112   }
114   113  
115   /** Asynchronously read some data from the source. 114   /** Asynchronously read some data from the source.
116   115  
117   Transfers up to `capy::buffer_size( buffers )` bytes from the internal 116   Transfers up to `capy::buffer_size( buffers )` bytes from the internal
118   buffer to the provided mutable buffer sequence. If no data 117   buffer to the provided mutable buffer sequence. If no data
119   remains, returns `capy::error::eof`. Before every read, the attached 118   remains, returns `capy::error::eof`. Before every read, the attached
120   @ref capy::test::fuse is consulted to possibly inject an error for testing 119   @ref capy::test::fuse is consulted to possibly inject an error for testing
121   fault scenarios. 120   fault scenarios.
122   121  
123   @param buffers The mutable buffer sequence to receive data. 122   @param buffers The mutable buffer sequence to receive data.
124   123  
125   @return An awaitable that await-returns `(error_code,std::size_t)`. 124   @return An awaitable that await-returns `(error_code,std::size_t)`.
126   125  
127   @par Cancellation 126   @par Cancellation
128   If the environment's stop token has been requested, the read 127   If the environment's stop token has been requested, the read
129   completes immediately with `capy::error::canceled` and transfers no 128   completes immediately with `capy::error::canceled` and transfers no
130   data. An empty buffer sequence is a no-op that completes 129   data. An empty buffer sequence is a no-op that completes
131   successfully regardless of the stop token. 130   successfully regardless of the stop token.
132   131  
133   @see capy::test::fuse 132   @see capy::test::fuse
134   */ 133   */
135   template<capy::MutableBufferSequence MB> 134   template<capy::MutableBufferSequence MB>
136   auto 135   auto
HITCBC 137   50 read_some(MB buffers) 136   50 read_some(MB buffers)
138   { 137   {
139   struct awaitable 138   struct awaitable
140   { 139   {
141   read_source* self_; 140   read_source* self_;
142   MB buffers_; 141   MB buffers_;
143   bool canceled_ = false; 142   bool canceled_ = false;
144   143  
HITCBC 145   50 bool await_ready() const noexcept { return false; } 144   50 bool await_ready() const noexcept { return false; }
146   145  
147   // The operation completes synchronously, but await_suspend is 146   // The operation completes synchronously, but await_suspend is
148   // the only place capy::await_context is delivered (the promise's 147   // the only place capy::await_context is delivered (the promise's
149 - // transform_awaiter forwards it here). Returning false means 148 + // awaiter forwards it here). Returning false means
150   // the coroutine does not actually suspend; it resumes 149   // the coroutine does not actually suspend; it resumes
151   // immediately, having observed the stop token. See capy::await_context, 150   // immediately, having observed the stop token. See capy::await_context,
152   // ContextAwaitable. 151   // ContextAwaitable.
153   bool 152   bool
HITCBC 154   50 await_suspend( 153   50 await_suspend(
155   std::coroutine_handle<>, 154   std::coroutine_handle<>,
156   capy::await_context const* env) noexcept 155   capy::await_context const* env) noexcept
157   { 156   {
HITCBC 158   50 canceled_ = env->stop_token.stop_requested(); 157   50 canceled_ = env->stop_token.stop_requested();
HITCBC 159   50 return false; 158   50 return false;
160   } 159   }
161   160  
162   capy::results<std::size_t> 161   capy::results<std::size_t>
HITCBC 163   50 await_resume() 162   50 await_resume()
164   { 163   {
HITCBC 165   50 if(capy::buffers_empty(buffers_)) 164   50 if(capy::buffers_empty(buffers_))
MISUBC 166   ✗ return {std::error_code(), 0}; 165   ✗ return {std::error_code(), 0};
167   166  
HITCBC 168   50 if(canceled_) 167   50 if(canceled_)
MISUBC 169   ✗ return {capy::error::canceled, 0}; 168   ✗ return {capy::error::canceled, 0};
170   169  
HITCBC 171   50 auto ec = self_->f_.maybe_fail(); 170   50 auto ec = self_->f_.maybe_fail();
HITCBC 172   36 if(ec) 171   36 if(ec)
HITCBC 173   14 return {ec, 0}; 172   14 return {ec, 0};
174   173  
HITCBC 175   22 if(self_->pos_ >= self_->data_.size()) 174   22 if(self_->pos_ >= self_->data_.size())
HITCBC 176   2 return {capy::error::eof, 0}; 175   2 return {capy::error::eof, 0};
177   176  
HITCBC 178   20 std::size_t avail = self_->data_.size() - self_->pos_; 177   20 std::size_t avail = self_->data_.size() - self_->pos_;
HITCBC 179   20 if(avail > self_->max_read_size_) 178   20 if(avail > self_->max_read_size_)
MISUBC 180   ✗ avail = self_->max_read_size_; 179   ✗ avail = self_->max_read_size_;
HITCBC 181   20 auto src = capy::make_buffer(self_->data_.data() + self_->pos_, avail); 180   20 auto src = capy::make_buffer(self_->data_.data() + self_->pos_, avail);
HITCBC 182   20 std::size_t const n = capy::buffer_copy(buffers_, src); 181   20 std::size_t const n = capy::buffer_copy(buffers_, src);
HITCBC 183   20 self_->pos_ += n; 182   20 self_->pos_ += n;
HITCBC 184   20 return {std::error_code(), n}; 183   20 return {std::error_code(), n};
185   } 184   }
186   }; 185   };
HITCBC 187   50 return awaitable{this, buffers}; 186   50 return awaitable{this, buffers};
188   } 187   }
189   188  
190   /** Asynchronously read data from the source. 189   /** Asynchronously read data from the source.
191   190  
192   Fills the entire buffer sequence from the internal data. 191   Fills the entire buffer sequence from the internal data.
193   If the available data is less than the buffer size, returns 192   If the available data is less than the buffer size, returns
194   `capy::error::eof` with the number of bytes transferred. Before 193   `capy::error::eof` with the number of bytes transferred. Before
195   every read, the attached @ref capy::test::fuse is consulted to possibly 194   every read, the attached @ref capy::test::fuse is consulted to possibly
196   inject an error for testing fault scenarios. 195   inject an error for testing fault scenarios.
197   196  
198   Unlike @ref read_some, this ignores `max_read_size` and 197   Unlike @ref read_some, this ignores `max_read_size` and
199   transfers all available data in a single operation, matching 198   transfers all available data in a single operation, matching
200   the @ref ReadSource semantic contract. 199   the @ref ReadSource semantic contract.
201   200  
202   @param buffers The mutable buffer sequence to receive data. 201   @param buffers The mutable buffer sequence to receive data.
203   202  
204   @return An awaitable that await-returns `(error_code,std::size_t)`. 203   @return An awaitable that await-returns `(error_code,std::size_t)`.
205   204  
206   @par Cancellation 205   @par Cancellation
207   If the environment's stop token has been requested, the read 206   If the environment's stop token has been requested, the read
208   completes immediately with `capy::error::canceled` and transfers no 207   completes immediately with `capy::error::canceled` and transfers no
209   data. An empty buffer sequence is a no-op that completes 208   data. An empty buffer sequence is a no-op that completes
210   successfully regardless of the stop token. 209   successfully regardless of the stop token.
211   210  
212   @see capy::test::fuse 211   @see capy::test::fuse
213   */ 212   */
214   template<capy::MutableBufferSequence MB> 213   template<capy::MutableBufferSequence MB>
215   auto 214   auto
HITCBC 216   292 read(MB buffers) 215   292 read(MB buffers)
217   { 216   {
218   struct awaitable 217   struct awaitable
219   { 218   {
220   read_source* self_; 219   read_source* self_;
221   MB buffers_; 220   MB buffers_;
222   bool canceled_ = false; 221   bool canceled_ = false;
223   222  
HITCBC 224   292 bool await_ready() const noexcept { return false; } 223   292 bool await_ready() const noexcept { return false; }
225   224  
226   // Reads the stop token without suspending; see the comment 225   // Reads the stop token without suspending; see the comment
227   // on read_some() for details. 226   // on read_some() for details.
228   bool 227   bool
HITCBC 229   292 await_suspend( 228   292 await_suspend(
230   std::coroutine_handle<>, 229   std::coroutine_handle<>,
231   capy::await_context const* env) noexcept 230   capy::await_context const* env) noexcept
232   { 231   {
HITCBC 233   292 canceled_ = env->stop_token.stop_requested(); 232   292 canceled_ = env->stop_token.stop_requested();
HITCBC 234   292 return false; 233   292 return false;
235   } 234   }
236   235  
237   capy::results<std::size_t> 236   capy::results<std::size_t>
HITCBC 238   292 await_resume() 237   292 await_resume()
239   { 238   {
HITCBC 240   292 if(capy::buffers_empty(buffers_)) 239   292 if(capy::buffers_empty(buffers_))
MISUBC 241   ✗ return {std::error_code(), 0}; 240   ✗ return {std::error_code(), 0};
242   241  
HITCBC 243   292 if(canceled_) 242   292 if(canceled_)
MISUBC 244   ✗ return {capy::error::canceled, 0}; 243   ✗ return {capy::error::canceled, 0};
245   244  
HITCBC 246   292 auto ec = self_->f_.maybe_fail(); 245   292 auto ec = self_->f_.maybe_fail();
HITCBC 247   232 if(ec) 246   232 if(ec)
HITCBC 248   60 return {ec, 0}; 247   60 return {ec, 0};
249   248  
HITCBC 250   172 if(self_->pos_ >= self_->data_.size()) 249   172 if(self_->pos_ >= self_->data_.size())
HITCBC 251   16 return {capy::error::eof, 0}; 250   16 return {capy::error::eof, 0};
252   251  
HITCBC 253   156 std::size_t avail = self_->data_.size() - self_->pos_; 252   156 std::size_t avail = self_->data_.size() - self_->pos_;
HITCBC 254   156 auto src = capy::make_buffer(self_->data_.data() + self_->pos_, avail); 253   156 auto src = capy::make_buffer(self_->data_.data() + self_->pos_, avail);
HITCBC 255   156 std::size_t const n = capy::buffer_copy(buffers_, src); 254   156 std::size_t const n = capy::buffer_copy(buffers_, src);
HITCBC 256   156 self_->pos_ += n; 255   156 self_->pos_ += n;
257   256  
HITCBC 258   156 if(n < capy::buffer_size(buffers_)) 257   156 if(n < capy::buffer_size(buffers_))
HITCBC 259   64 return {capy::error::eof, n}; 258   64 return {capy::error::eof, n};
HITCBC 260   92 return {std::error_code(), n}; 259   92 return {std::error_code(), n};
261   } 260   }
262   }; 261   };
HITCBC 263   292 return awaitable{this, buffers}; 262   292 return awaitable{this, buffers};
264   } 263   }
265   }; 264   };
266   265  
267   } // test 266   } // test
268   } // capy 267   } // capy
269   } // boost 268   } // boost
270   269  
271   #endif 270   #endif