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

10  

11  
#ifndef BOOST_HTTP_MESSAGE_BASE_HPP
11  
#ifndef BOOST_HTTP_MESSAGE_BASE_HPP
12  
#define BOOST_HTTP_MESSAGE_BASE_HPP
12  
#define BOOST_HTTP_MESSAGE_BASE_HPP
13  

13  

14  
#include <boost/http/detail/config.hpp>
14  
#include <boost/http/detail/config.hpp>
15  
#include <boost/http/fields_base.hpp>
15  
#include <boost/http/fields_base.hpp>
16  
#include <boost/core/detail/string_view.hpp>
16  
#include <boost/core/detail/string_view.hpp>
17  

17  

18  
namespace boost {
18  
namespace boost {
19  
namespace http {
19  
namespace http {
20  

20  

21  
/** Mixin for modifing common metadata
21  
/** Mixin for modifing common metadata
22  
    in HTTP request and response messages.
22  
    in HTTP request and response messages.
23  

23  

24  
    This type is useful for modifying common
24  
    This type is useful for modifying common
25  
    properties shared by both requests
25  
    properties shared by both requests
26  
    and responses.
26  
    and responses.
27  

27  

28  
    @see
28  
    @see
29  
        @ref response,
29  
        @ref response,
30  
        @ref request,
30  
        @ref request,
31  
        @ref static_response,
31  
        @ref static_response,
32  
        @ref static_request,
32  
        @ref static_request,
33  
        @ref metadata.
33  
        @ref metadata.
34  
*/
34  
*/
35  
class message_base
35  
class message_base
36  
    : public fields_base
36  
    : public fields_base
37  
{
37  
{
38  
    friend class request_base;
38  
    friend class request_base;
39  
    friend class response_base;
39  
    friend class response_base;
40  

40  

41  
    using fields_base::fields_base;
41  
    using fields_base::fields_base;
42  

42  

43  
public:
43  
public:
44  
    //--------------------------------------------
44  
    //--------------------------------------------
45  
    //
45  
    //
46  
    // Observers
46  
    // Observers
47  
    //
47  
    //
48  
    //--------------------------------------------
48  
    //--------------------------------------------
49  

49  

50  
    /** Return the type of payload of this message.
50  
    /** Return the type of payload of this message.
51  
    */
51  
    */
52  
    auto
52  
    auto
53  
    payload() const noexcept ->
53  
    payload() const noexcept ->
54  
        http::payload
54  
        http::payload
55  
    {
55  
    {
56  
        return h_.md.payload;
56  
        return h_.md.payload;
57  
    }
57  
    }
58  

58  

59  
    /** Return the payload size.
59  
    /** Return the payload size.
60  

60  

61  
        When @ref payload returns @ref payload::size,
61  
        When @ref payload returns @ref payload::size,
62  
        this function returns the number of octets
62  
        this function returns the number of octets
63  
        in the actual message payload.
63  
        in the actual message payload.
64  

64  

65  
        @return The number of octets in the
65  
        @return The number of octets in the
66  
        actual message payload.
66  
        actual message payload.
67  
    */
67  
    */
68  
    std::uint64_t
68  
    std::uint64_t
69  
    payload_size() const noexcept
69  
    payload_size() const noexcept
70  
    {
70  
    {
71  
        BOOST_ASSERT(
71  
        BOOST_ASSERT(
72  
            payload() == payload::size);
72  
            payload() == payload::size);
73  
        return h_.md.payload_size;
73  
        return h_.md.payload_size;
74  
    }
74  
    }
75  

75  

76  
    /** Return true if semantics indicate
76  
    /** Return true if semantics indicate
77  
        connection persistence.
77  
        connection persistence.
78  
    */
78  
    */
79  
    bool
79  
    bool
80  
    keep_alive() const noexcept
80  
    keep_alive() const noexcept
81  
    {
81  
    {
82  
        return h_.keep_alive();
82  
        return h_.keep_alive();
83  
    }
83  
    }
84  

84  

85  
    /** Return metadata about the message.
85  
    /** Return metadata about the message.
86  
    */
86  
    */
87  
    auto
87  
    auto
88  
    metadata() const noexcept ->
88  
    metadata() const noexcept ->
89  
        http::metadata const&
89  
        http::metadata const&
90  
    {
90  
    {
91  
        return h_.md;
91  
        return h_.md;
92  
    }
92  
    }
93  

93  

94  
    /** Return true if the message is using a chunked
94  
    /** Return true if the message is using a chunked
95  
        transfer encoding.
95  
        transfer encoding.
96  
    */
96  
    */
97  
    bool
97  
    bool
98  
    chunked() const noexcept
98  
    chunked() const noexcept
99  
    {
99  
    {
100  
        return h_.md.transfer_encoding.is_chunked;
100  
        return h_.md.transfer_encoding.is_chunked;
101  
    }
101  
    }
102  

102  

103  
    /** Return the HTTP-version.
103  
    /** Return the HTTP-version.
104  
    */
104  
    */
105  
    http::version
105  
    http::version
106  
    version() const noexcept
106  
    version() const noexcept
107  
    {
107  
    {
108  
        return h_.version;
108  
        return h_.version;
109  
    }
109  
    }
110  

110  

111  
    //--------------------------------------------
111  
    //--------------------------------------------
112  
    //
112  
    //
113  
    // Modifiers
113  
    // Modifiers
114  
    //
114  
    //
115  
    //--------------------------------------------
115  
    //--------------------------------------------
116  

116  

117  
    /** Set the payload size.
117  
    /** Set the payload size.
118  

118  

119  
        @par Exception Safety
119  
        @par Exception Safety
120  
        Strong guarantee.
120  
        Strong guarantee.
121  
        Calls to allocate may throw.
121  
        Calls to allocate may throw.
122  
        Exception thrown if max capacity exceeded.
122  
        Exception thrown if max capacity exceeded.
123  

123  

124  
        @throw std::length_error
124  
        @throw std::length_error
125  
        Max capacity would be exceeded.
125  
        Max capacity would be exceeded.
126  

126  

127  
        @param n The payload size to set.
127  
        @param n The payload size to set.
128  
    */
128  
    */
129  
    BOOST_HTTP_DECL
129  
    BOOST_HTTP_DECL
130  
    void
130  
    void
131  
    set_payload_size(
131  
    set_payload_size(
132  
        std::uint64_t n);
132  
        std::uint64_t n);
133  

133  

134  
    /** Set the Content-Length to the specified value.
134  
    /** Set the Content-Length to the specified value.
135  

135  

136  
        @par Exception Safety
136  
        @par Exception Safety
137  
        Strong guarantee.
137  
        Strong guarantee.
138  
        Calls to allocate may throw.
138  
        Calls to allocate may throw.
139  
        Exception thrown if max capacity exceeded.
139  
        Exception thrown if max capacity exceeded.
140  

140  

141  
        @throw std::length_error
141  
        @throw std::length_error
142  
        Max capacity would be exceeded.
142  
        Max capacity would be exceeded.
143  

143  

144  
        @param n The Content-Length to set.
144  
        @param n The Content-Length to set.
145  
    */
145  
    */
146  
    BOOST_HTTP_DECL
146  
    BOOST_HTTP_DECL
147  
    void
147  
    void
148  
    set_content_length(
148  
    set_content_length(
149  
        std::uint64_t n);
149  
        std::uint64_t n);
150  

150  

151  
    /** Set whether the payload is chunked.
151  
    /** Set whether the payload is chunked.
152  

152  

153  
        @par Exception Safety
153  
        @par Exception Safety
154  
        Strong guarantee.
154  
        Strong guarantee.
155  
        Calls to allocate may throw.
155  
        Calls to allocate may throw.
156  
        Exception thrown if max capacity exceeded.
156  
        Exception thrown if max capacity exceeded.
157  

157  

158  
        @throw std::length_error
158  
        @throw std::length_error
159  
        Max capacity would be exceeded.
159  
        Max capacity would be exceeded.
160  

160  

161  
        @param value The value to set.
161  
        @param value The value to set.
162  
    */
162  
    */
163  
    BOOST_HTTP_DECL
163  
    BOOST_HTTP_DECL
164  
    void
164  
    void
165  
    set_chunked(bool value);
165  
    set_chunked(bool value);
166  

166  

167  
    /** Set whether the connection should stay open.
167  
    /** Set whether the connection should stay open.
168  

168  

169  
        Even when keep-alive is set to true, the
169  
        Even when keep-alive is set to true, the
170  
        semantics of the other header fields may
170  
        semantics of the other header fields may
171  
        require the connection to be closed. For
171  
        require the connection to be closed. For
172  
        example when there is no content length
172  
        example when there is no content length
173  
        specified in a response.
173  
        specified in a response.
174  

174  

175  
        @par Exception Safety
175  
        @par Exception Safety
176  
        Strong guarantee.
176  
        Strong guarantee.
177  
        Calls to allocate may throw.
177  
        Calls to allocate may throw.
178  
        Exception thrown if max capacity exceeded.
178  
        Exception thrown if max capacity exceeded.
179  

179  

180  
        @throw std::length_error
180  
        @throw std::length_error
181  
        Max capacity would be exceeded.
181  
        Max capacity would be exceeded.
182  

182  

183  
        @param value The value to set.
183  
        @param value The value to set.
184  
    */
184  
    */
185  
    BOOST_HTTP_DECL
185  
    BOOST_HTTP_DECL
186  
    void
186  
    void
187  
    set_keep_alive(bool value);
187  
    set_keep_alive(bool value);
188  
};
188  
};
189  

189  

190  
} // http
190  
} // http
191  
} // boost
191  
} // boost
192  

192  

193  
#endif
193  
#endif