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