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