Skip to content

Commit 412cb46

Browse files
committed
feat: set_version
1 parent dc7e481 commit 412cb46

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

include/boost/http_proto/response_base.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ class response_base
129129
obsolete_reason(sc), v);
130130
}
131131

132+
/** Set the HTTP version of the response
133+
134+
@par Exception Safety
135+
Strong guarantee.
136+
Calls to allocate may throw.
137+
Exception thrown if maximum capacity exceeded.
138+
139+
@throw std::length_error
140+
Maximum capacity would be exceeded.
141+
142+
@param v The version to set.
143+
*/
144+
BOOST_HTTP_PROTO_DECL
145+
void
146+
set_version(
147+
http_proto::version v);
148+
132149
/** Set the status code of the response.
133150
134151
The reason-phrase will be set to the

src/response_base.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,37 @@ set_start_line_impl(
5757
h_.on_start_line();
5858
}
5959

60+
void
61+
response_base::
62+
set_version(
63+
http_proto::version v)
64+
{
65+
if(v == h_.version)
66+
return;
67+
if(h_.is_default())
68+
{
69+
auto def = h_.get_default(detail::kind::response);
70+
return set_start_line_impl(
71+
def->res.status, def->res.status_int,
72+
core::string_view(
73+
def->cbuf + 13, def->prefix - 15), v);
74+
}
75+
76+
// Introduce a new scope so that prefix_op's
77+
// destructor runs before h_.on_start_line().
78+
{
79+
auto op = prefix_op_t(
80+
*this, h_.prefix, nullptr);
81+
char* dest = h_.buf;
82+
if(v == version::http_1_1)
83+
dest[7] = '1';
84+
else
85+
dest[7] = '0';
86+
h_.version = v;
87+
}
88+
89+
h_.on_start_line();
90+
}
91+
6092
} // http_proto
6193
} // boost

test/unit/response.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,20 @@ class response_test
284284
}
285285
}
286286

287+
// set_version
288+
{
289+
response res;
290+
res.set_version(version::http_1_1);
291+
BOOST_TEST_EQ(res.buffer(),
292+
"HTTP/1.1 200 OK\r\n\r\n");
293+
res.set_version(version::http_1_0);
294+
BOOST_TEST_EQ(res.buffer(),
295+
"HTTP/1.0 200 OK\r\n\r\n");
296+
res.set_version(version::http_1_1);
297+
BOOST_TEST_EQ(res.buffer(),
298+
"HTTP/1.1 200 OK\r\n\r\n");
299+
}
300+
287301
// set_status
288302
{
289303
response res;

0 commit comments

Comments
 (0)