src/detail/filter.cpp

70.0% Lines (28 / 40) 100.0% Functions (2 / 2)
filter.cpp
f(x) Functions (2)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2024 Mohammad Nejati
4 //
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)
7 //
8 // Official repository: https://github.com/cppalliance/http
9 //
10
11 #include "src/detail/filter.hpp"
12
13 #include <boost/capy/buffers/buffers_front.hpp>
14 #include <boost/http/io/buffer_param.hpp>
15
16 namespace boost {
17 namespace http {
18 namespace detail {
19
20 namespace {
21
22 // Returns true if the slice's current data view contains at most one
23 // non-empty buffer (i.e., we are processing the last logical chunk).
24 template<class Slice>
25 2x bool single_or_empty(Slice& s)
26 {
27 2x auto d = s.data();
28 2x auto it = d.begin();
29 2x auto const end_it = d.end();
30 2x if(it == end_it)
31 1x return true;
32 1x ++it;
33 1x return it == end_it;
34 }
35
36 } // anonymous
37
38 auto
39 2x filter::
40 process(
41 boost::span<const capy::mutable_buffer> out_seq,
42 std::array<capy::const_buffer, 2> in_seq,
43 bool more) -> results
44 {
45 2x buffer_param out(out_seq);
46 2x buffer_param in(in_seq);
47
48 // data() spans only the current window, so track totals separately
49 2x std::size_t out_left = capy::buffer_size(out_seq);
50 2x std::size_t in_left = capy::buffer_size(in_seq);
51
52 2x results rv;
53 2x bool p_more = true;
54 for(;;)
55 {
56 2x if(!more && p_more && single_or_empty(in))
57 {
58 2x if(out_left < min_out_buffer())
59 {
60 ✗ rv.out_short = true;
61 2x return rv;
62 }
63 2x p_more = false;
64 }
65
66 2x auto ob = capy::buffers_front(out.data());
67 2x auto ib = capy::buffers_front(in.data());
68 2x auto rs = do_process(ob, ib, p_more);
69
70 2x rv.in_bytes += rs.in_bytes;
71 2x rv.out_bytes += rs.out_bytes;
72
73 2x if(rs.ec)
74 {
75 ✗ rv.ec = rs.ec;
76 ✗ return rv;
77 }
78
79 2x if(rs.finished)
80 {
81 2x rv.finished = true;
82 2x return rv;
83 }
84
85 ✗ out.consume(rs.out_bytes);
86 ✗ in.consume(rs.in_bytes);
87 ✗ out_left -= rs.out_bytes;
88 ✗ in_left -= rs.in_bytes;
89
90 ✗ if(out_left == 0)
91 ✗ return rv;
92
93 ✗ if(in_left == 0 && rs.out_bytes < ob.size())
94 ✗ return rv;
95 ✗ }
96 }
97
98 } // detail
99 } // http
100 } // boost
101