• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot 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/boostorg/beast
8 //
9 
10 #ifndef BOOST_BEAST_CORE_DETAIL_FLAT_STREAM_HPP
11 #define BOOST_BEAST_CORE_DETAIL_FLAT_STREAM_HPP
12 
13 #include <boost/beast/core/buffer_traits.hpp>
14 #include <boost/asio/buffer.hpp>
15 #include <cstdlib>
16 
17 namespace boost {
18 namespace beast {
19 namespace detail {
20 
21 class flat_stream_base
22 {
23 public:
24     // Largest buffer size we will flatten.
25     // 16KB is the upper limit on reasonably sized HTTP messages.
26     static std::size_t constexpr max_size = 16 * 1024;
27 
28     // Largest stack we will use to flatten
29     static std::size_t constexpr max_stack = 8 * 1024;
30 
31     struct flatten_result
32     {
33         std::size_t size;
34         bool flatten;
35     };
36 
37     // calculates the flatten settings for a buffer sequence
38     template<class BufferSequence>
39     static
40     flatten_result
flatten(BufferSequence const & buffers,std::size_t limit)41     flatten(
42         BufferSequence const& buffers, std::size_t limit)
43     {
44         flatten_result result{0, false};
45         auto first = net::buffer_sequence_begin(buffers);
46         auto last = net::buffer_sequence_end(buffers);
47         if(first != last)
48         {
49             result.size = buffer_bytes(*first);
50             if(result.size < limit)
51             {
52                 auto it = first;
53                 auto prev = first;
54                 while(++it != last)
55                 {
56                     auto const n = buffer_bytes(*it);
57                     if(result.size + n > limit)
58                         break;
59                     result.size += n;
60                     prev = it;
61                 }
62                 result.flatten = prev != first;
63             }
64         }
65         return result;
66     }
67 };
68 
69 } // detail
70 } // beast
71 } // boost
72 
73 #endif
74