File indexing completed on 2025-01-18 09:29:27
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_BEAST_IMPL_READ_SIZE_HPP
0011 #define BOOST_BEAST_IMPL_READ_SIZE_HPP
0012
0013 #include <boost/asio/buffer.hpp>
0014 #include <boost/assert.hpp>
0015 #include <stdexcept>
0016 #include <type_traits>
0017
0018 namespace boost {
0019 namespace beast {
0020
0021 namespace detail {
0022
0023 template<class T, class = void>
0024 struct has_read_size_helper : std::false_type {};
0025
0026 template<class T>
0027 struct has_read_size_helper<T, decltype(
0028 read_size_helper(std::declval<T&>(), 512),
0029 (void)0)> : std::true_type
0030 {
0031 };
0032
0033 template<class DynamicBuffer>
0034 std::size_t
0035 read_size(DynamicBuffer& buffer,
0036 std::size_t max_size, std::true_type)
0037 {
0038 return read_size_helper(buffer, max_size);
0039 }
0040
0041 template<class DynamicBuffer>
0042 std::size_t
0043 read_size(DynamicBuffer& buffer,
0044 std::size_t max_size, std::false_type)
0045 {
0046 static_assert(
0047 net::is_dynamic_buffer<DynamicBuffer>::value,
0048 "DynamicBuffer type requirements not met");
0049 auto const size = buffer.size();
0050 auto const limit = buffer.max_size() - size;
0051 BOOST_ASSERT(size <= buffer.max_size());
0052 return std::min<std::size_t>(
0053 std::max<std::size_t>(512, buffer.capacity() - size),
0054 std::min<std::size_t>(max_size, limit));
0055 }
0056
0057 }
0058
0059 template<class DynamicBuffer>
0060 std::size_t
0061 read_size(
0062 DynamicBuffer& buffer, std::size_t max_size)
0063 {
0064 return detail::read_size(buffer, max_size,
0065 detail::has_read_size_helper<DynamicBuffer>{});
0066 }
0067
0068 template<class DynamicBuffer>
0069 std::size_t
0070 read_size_or_throw(
0071 DynamicBuffer& buffer, std::size_t max_size)
0072 {
0073 auto const n = read_size(buffer, max_size);
0074 if(n == 0)
0075 BOOST_THROW_EXCEPTION(std::length_error{
0076 "buffer overflow"});
0077 return n;
0078 }
0079
0080 }
0081 }
0082
0083 #endif