flat_stream.hpp 1.9 KB

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