1  
//
1  
//
2  
// Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com)
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
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)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/cppalliance/http
7  
// Official repository: https://github.com/cppalliance/http
8  
//
8  
//
9  

9  

10  
#include <boost/http/header_limits.hpp>
10  
#include <boost/http/header_limits.hpp>
11  
#include <boost/http/detail/except.hpp>
11  
#include <boost/http/detail/except.hpp>
12  
#include <boost/http/detail/header.hpp>
12  
#include <boost/http/detail/header.hpp>
13  

13  

14  
namespace boost {
14  
namespace boost {
15  
namespace http {
15  
namespace http {
16  

16  

17  
std::size_t
17  
std::size_t
18  
header_limits::
18  
header_limits::
19  
valid_space_needed() const
19  
valid_space_needed() const
20  
{
20  
{
21  
/*
21  
/*
22  
    // "HTTP/1.1 200 OK\r\n\r\n" = 19
22  
    // "HTTP/1.1 200 OK\r\n\r\n" = 19
23  
    // "X / HTTP/1.1\r\n"        = 14
23  
    // "X / HTTP/1.1\r\n"        = 14
24  
    // "HTTP/1.1 200\r\n"        = 14
24  
    // "HTTP/1.1 200\r\n"        = 14
25  
    // "X:\r\n"                  =  4
25  
    // "X:\r\n"                  =  4
26  

26  

27  
    // make sure `size` is big enough
27  
    // make sure `size` is big enough
28  
    // to hold the largest default buffer:
28  
    // to hold the largest default buffer:
29  
    //if( max_size < 19)
29  
    //if( max_size < 19)
30  
        //max_size = 19;
30  
        //max_size = 19;
31  

31  

32  
    // max_size too small
32  
    // max_size too small
33  
    if( max_size < 19)
33  
    if( max_size < 19)
34  
        detail::throw_invalid_argument();
34  
        detail::throw_invalid_argument();
35  

35  

36  
    // max_size too large
36  
    // max_size too large
37  
    if( max_size >
37  
    if( max_size >
38  
        BOOST_HTTP_MAX_HEADER)
38  
        BOOST_HTTP_MAX_HEADER)
39  
        detail::throw_invalid_argument();
39  
        detail::throw_invalid_argument();
40  

40  

41  
    // max_start_line too small
41  
    // max_start_line too small
42  
    if( max_start_line < 14)
42  
    if( max_start_line < 14)
43  
        detail::throw_invalid_argument();
43  
        detail::throw_invalid_argument();
44  

44  

45  
    // max_start_line too large
45  
    // max_start_line too large
46  
    if( max_start_line >
46  
    if( max_start_line >
47  
        max_size - 2)
47  
        max_size - 2)
48  
        detail::throw_invalid_argument();
48  
        detail::throw_invalid_argument();
49  

49  

50  
    // max_field too small
50  
    // max_field too small
51  
    if( max_field < 4)
51  
    if( max_field < 4)
52  
        detail::throw_invalid_argument();
52  
        detail::throw_invalid_argument();
53  

53  

54  
    // max_field too large
54  
    // max_field too large
55  
    if( max_field >
55  
    if( max_field >
56  
        max_size)
56  
        max_size)
57  
        detail::throw_invalid_argument();
57  
        detail::throw_invalid_argument();
58  

58  

59  
    // max_fields too large
59  
    // max_fields too large
60  
    if( max_fields >
60  
    if( max_fields >
61  
        max_size / 4)
61  
        max_size / 4)
62  
        detail::throw_invalid_argument();
62  
        detail::throw_invalid_argument();
63  
*/
63  
*/
64  
    static constexpr auto Align =
64  
    static constexpr auto Align =
65  
        alignof(detail::header::entry);
65  
        alignof(detail::header::entry);
66  
    // round up to alignof(A)
66  
    // round up to alignof(A)
67  
    return Align * (
67  
    return Align * (
68  
        (max_size + Align - 1) / Align) + (
68  
        (max_size + Align - 1) / Align) + (
69  
            max_fields * sizeof(
69  
            max_fields * sizeof(
70  
                detail::header::entry));
70  
                detail::header::entry));
71  
}
71  
}
72  

72  

73  
} // http
73  
} // http
74  
} // boost
74  
} // boost