Line data Source code
1 : //
2 : // Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com)
3 : // Copyright (c) 2024 Christian Mazakas
4 : //
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)
7 : //
8 : // Official repository: https://github.com/cppalliance/http_proto
9 : //
10 :
11 : #ifndef BOOST_HTTP_PROTO_MESSAGE_BASE_HPP
12 : #define BOOST_HTTP_PROTO_MESSAGE_BASE_HPP
13 :
14 : #include <boost/http_proto/detail/config.hpp>
15 : #include <boost/http_proto/fields_base.hpp>
16 : #include <boost/core/detail/string_view.hpp>
17 :
18 : namespace boost {
19 : namespace http_proto {
20 :
21 : /** Mixin for modifing common metadata
22 : in HTTP request and response messages.
23 :
24 : This type is useful for modifying common
25 : properties shared by both requests
26 : and responses.
27 :
28 : @see
29 : @ref response,
30 : @ref request,
31 : @ref static_response,
32 : @ref static_request,
33 : @ref metadata.
34 : */
35 : class message_base
36 : : public fields_base
37 : {
38 : friend class request_base;
39 : friend class response_base;
40 :
41 : using fields_base::fields_base;
42 :
43 : public:
44 : //--------------------------------------------
45 : //
46 : // Observers
47 : //
48 : //--------------------------------------------
49 :
50 : /** Return the type of payload of this message.
51 : */
52 : auto
53 474403 : payload() const noexcept ->
54 : http_proto::payload
55 : {
56 474403 : return h_.md.payload;
57 : }
58 :
59 : /** Return the payload size.
60 :
61 : When @ref payload returns @ref payload::size,
62 : this function returns the number of octets
63 : in the actual message payload.
64 :
65 : @return The number of octets in the
66 : actual message payload.
67 : */
68 : std::uint64_t
69 8725 : payload_size() const noexcept
70 : {
71 8725 : BOOST_ASSERT(
72 : payload() == payload::size);
73 8725 : return h_.md.payload_size;
74 : }
75 :
76 : /** Return true if semantics indicate
77 : connection persistence.
78 : */
79 : bool
80 22 : keep_alive() const noexcept
81 : {
82 22 : return h_.keep_alive();
83 : }
84 :
85 : /** Return metadata about the message.
86 : */
87 : auto
88 9132 : metadata() const noexcept ->
89 : http_proto::metadata const&
90 : {
91 9132 : return h_.md;
92 : }
93 :
94 : /** Return true if the message is using a chunked
95 : transfer encoding.
96 : */
97 : bool
98 1194 : chunked() const noexcept
99 : {
100 1194 : return h_.md.transfer_encoding.is_chunked;
101 : }
102 :
103 : /** Return the HTTP-version.
104 : */
105 : http_proto::version
106 119 : version() const noexcept
107 : {
108 119 : return h_.version;
109 : }
110 :
111 : //--------------------------------------------
112 : //
113 : // Modifiers
114 : //
115 : //--------------------------------------------
116 :
117 : /** Set the payload size.
118 :
119 : @par Exception Safety
120 : Strong guarantee.
121 : Calls to allocate may throw.
122 : Exception thrown if max capacity exceeded.
123 :
124 : @throw std::length_error
125 : Max capacity would be exceeded.
126 :
127 : @param n The payload size to set.
128 : */
129 : BOOST_HTTP_PROTO_DECL
130 : void
131 : set_payload_size(
132 : std::uint64_t n);
133 :
134 : /** Set the Content-Length to the specified value.
135 :
136 : @par Exception Safety
137 : Strong guarantee.
138 : Calls to allocate may throw.
139 : Exception thrown if max capacity exceeded.
140 :
141 : @throw std::length_error
142 : Max capacity would be exceeded.
143 :
144 : @param n The Content-Length to set.
145 : */
146 : BOOST_HTTP_PROTO_DECL
147 : void
148 : set_content_length(
149 : std::uint64_t n);
150 :
151 : /** Set whether the payload is chunked.
152 :
153 : @par Exception Safety
154 : Strong guarantee.
155 : Calls to allocate may throw.
156 : Exception thrown if max capacity exceeded.
157 :
158 : @throw std::length_error
159 : Max capacity would be exceeded.
160 :
161 : @param value The value to set.
162 : */
163 : BOOST_HTTP_PROTO_DECL
164 : void
165 : set_chunked(bool value);
166 :
167 : /** Set whether the connection should stay open.
168 :
169 : Even when keep-alive is set to true, the
170 : semantics of the other header fields may
171 : require the connection to be closed. For
172 : example when there is no content length
173 : specified in a response.
174 :
175 : @par Exception Safety
176 : Strong guarantee.
177 : Calls to allocate may throw.
178 : Exception thrown if max capacity exceeded.
179 :
180 : @throw std::length_error
181 : Max capacity would be exceeded.
182 :
183 : @param value The value to set.
184 : */
185 : BOOST_HTTP_PROTO_DECL
186 : void
187 : set_keep_alive(bool value);
188 : };
189 :
190 : } // http_proto
191 : } // boost
192 :
193 : #endif
|