Skip to content

Commit dc7e481

Browse files
committed
feat: set_status
1 parent 3ff8a02 commit dc7e481

3 files changed

Lines changed: 51 additions & 6 deletions

File tree

include/boost/http_proto/response_base.hpp

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ class response_base
110110
111111
@throw std::length_error
112112
Max capacity would be exceeded.
113+
@throw std::invalid_argument
114+
`sc == status::unknown`
113115
114116
@param sc The status code to set. This
115117
must not be @ref status::unknown.
@@ -122,12 +124,40 @@ class response_base
122124
http_proto::version v =
123125
http_proto::version::http_1_1)
124126
{
125-
set_start_line_impl(
126-
sc,
127-
static_cast<
128-
unsigned short>(sc),
127+
set_start_line_impl(sc,
128+
static_cast<unsigned short>(sc),
129+
obsolete_reason(sc), v);
130+
}
131+
132+
/** Set the status code of the response.
133+
134+
The reason-phrase will be set to the
135+
standard text for the specified status
136+
code. The version will remain unchanged.
137+
138+
@par Exception Safety
139+
Strong guarantee.
140+
Calls to allocate may throw.
141+
Exception thrown if maximum capacity exceeded.
142+
143+
@throw std::length_error
144+
Maximum capacity would be exceeded.
145+
@throw std::invalid_argument
146+
`sc == status::unknown`
147+
148+
@param sc The status code to set. This
149+
must not be @ref status::unknown.
150+
*/
151+
void
152+
set_status(
153+
http_proto::status sc)
154+
{
155+
if(sc == http_proto::status::unknown)
156+
detail::throw_invalid_argument();
157+
set_start_line_impl(sc,
158+
static_cast<unsigned short>(sc),
129159
obsolete_reason(sc),
130-
v);
160+
version());
131161
}
132162

133163
/** Set the status code and version of the response.

src/status.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
//
99

1010
#include <boost/http_proto/status.hpp>
11-
#include <boost/throw_exception.hpp>
11+
#include <boost/http_proto/detail/except.hpp>
12+
//#include <boost/throw_exception.hpp>
1213
#include <ostream>
1314

1415
namespace boost {

test/unit/response.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,20 @@ class response_test
283283
BOOST_TEST_EQ(it->value, "0");
284284
}
285285
}
286+
287+
// set_status
288+
{
289+
response res;
290+
res.set_status(status::not_found);
291+
BOOST_TEST_EQ(res.buffer(),
292+
"HTTP/1.1 404 Not Found\r\n\r\n");
293+
res.set_status(status::bad_request);
294+
BOOST_TEST_EQ(res.buffer(),
295+
"HTTP/1.1 400 Bad Request\r\n\r\n");
296+
res.set_status(status::ok);
297+
BOOST_TEST_EQ(res.buffer(),
298+
"HTTP/1.1 200 OK\r\n\r\n");
299+
}
286300
}
287301

288302
void

0 commit comments

Comments
 (0)