include/boost/burl/response_head_base.hpp

100.0% Lines (30/30) 100.0% List of functions (10/10) 71.4% Branches (10/14)
response_head_base.hpp
f(x) Functions (10)
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2021 Vinnie Falco ([email protected])
3 // Copyright (c) 2026 Mohammad Nejati
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/burl
9 //
10
11 #ifndef BOOST_BURL_RESPONSE_HEAD_BASE_HPP
12 #define BOOST_BURL_RESPONSE_HEAD_BASE_HPP
13
14 #include <boost/burl/detail/config.hpp>
15 #include <boost/burl/message_head_base.hpp>
16
17 namespace boost
18 {
19 namespace burl
20 {
21
22 /** Mixin for an HTTP response header.
23
24 This type extends @ref message_head_base with observers
25 and modifiers for the status line
26
27 Users cannot construct or copy objects of this
28 type; see @ref message_head_base. To build a request
29 header for sending, use @ref response_head or
30 @ref static_response_head.
31
32 Strings passed to the start-line modifiers are
33 stored verbatim; arguments which are views into
34 the message itself remain valid to pass.
35 */
36 class response_head_base : public message_head_base
37 {
38 friend class head_parser;
39
40 protected:
41 BOOST_BURL_DECL
42 response_head_base() noexcept;
43
44 860x response_head_base(
45 char* base,
46 std::size_t cap,
47 std::uint32_t size = 0,
48 std::uint16_t prefix = 0) noexcept
49 860x : message_head_base(
50 860x false, base, cap, size, prefix)
51 {
52 860x }
53
54 623x response_head_base(response_head_base const&) = default;
55
56 response_head_base&
57 82x operator=(response_head_base const&) = default;
58
59 ~response_head_base() = default;
60
61 void
62 19x swap_(response_head_base& other) noexcept
63 {
64 19x message_head_base::swap_(other);
65 19x }
66
67 public:
68 /** Return the reason-phrase.
69
70 For serialized responses this is the text
71 chosen by the caller; for parsed responses
72 it is the text as received, which may be
73 empty.
74
75 @par Complexity
76 Constant.
77 */
78 std::string_view
79 20x reason() const noexcept
80 {
81 // "HTTP/1.x NNN reason\r\n"; parsed status
82 // lines may omit the space and the phrase
83
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 19 times.
20x if(prefix_ < 15)
84 1x return {};
85 19x return { base_() + 13, std::size_t(prefix_) - 15 };
86 }
87
88 /** Return the status code as a constant.
89
90 If the received code is not recognized,
91 @ref http::status::unknown is returned and
92 @ref status_int holds the original value.
93
94 @par Complexity
95 Constant.
96 */
97 http::status
98 68x status() const noexcept
99 {
100 68x return res_.status_;
101 }
102
103 /** Return the status code as an integer.
104
105 @par Complexity
106 Constant.
107 */
108 unsigned short
109 65x status_int() const noexcept
110 {
111 65x return res_.status_int_;
112 }
113
114 //--------------------------------------------
115
116 /** Set the status line.
117
118 The reason-phrase is set to the standard
119 text for the status code.
120
121 @par Exception Safety
122 Strong guarantee.
123
124 @throw std::length_error
125 The storage cannot accommodate the change.
126
127 @param sc The status code. Must have an
128 integer value in [100, 999].
129
130 @param v The version.
131 */
132 void
133 56x set_start_line(
134 http::status sc,
135 http::version v =
136 http::version::http_1_1)
137 {
138
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
56x BOOST_ASSERT(in_range_(sc));
139
1/1
✓ Branch 2 taken 55 times.
56x set_start_line(
140 static_cast<unsigned short>(sc),
141
1/1
✓ Branch 1 taken 56 times.
56x http::to_string(sc),
142 v);
143 55x }
144
145 /** Set the status line.
146
147 The reason-phrase is stored verbatim.
148
149 @par Exception Safety
150 Strong guarantee.
151
152 @throw std::invalid_argument
153 `si` is not in [100, 999].
154
155 @throw std::length_error
156 The storage cannot accommodate the change.
157
158 @param si The status code, in [100, 999].
159
160 @param reason The reason-phrase.
161
162 @param v The version.
163 */
164 BOOST_BURL_DECL
165 void
166 set_start_line(
167 unsigned short si,
168 std::string_view reason,
169 http::version v =
170 http::version::http_1_1);
171
172 /** Set the HTTP version.
173
174 @par Exception Safety
175 Strong guarantee.
176
177 @param v The version.
178 */
179 BOOST_BURL_DECL
180 void
181 set_version(http::version v);
182
183 /** Set the status code.
184
185 The reason-phrase is set to the standard
186 text for the status code. The version is
187 unchanged.
188
189 @par Exception Safety
190 Strong guarantee.
191
192 @throw std::length_error
193 The storage cannot accommodate the change.
194
195 @param sc The status code. Must have an
196 integer value in [100, 999].
197 */
198 void
199 9x set_status(http::status sc)
200 {
201
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9x BOOST_ASSERT(in_range_(sc));
202
1/1
✓ Branch 3 taken 9 times.
18x set_start_line(
203 static_cast<unsigned short>(sc),
204
1/1
✓ Branch 1 taken 9 times.
9x http::to_string(sc),
205 version());
206 9x }
207
208 private:
209 static
210 bool
211 65x in_range_(http::status sc) noexcept
212 {
213 65x auto const si = static_cast<unsigned short>(sc);
214
2/4
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
✗ Branch 3 not taken.
65x return si >= 100 && si <= 999;
215 }
216 };
217
218 } // namespace burl
219 } // namespace boost
220
221 #endif
222