Skip to content

Commit eaff20e

Browse files
committed
Added move set_body for preformance
1 parent c6d7e29 commit eaff20e

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

examples/telemetry_server/telemetry_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class telemetry_server {
149149
response.assign((std::istreambuf_iterator<char>(file)),
150150
std::istreambuf_iterator<char>());
151151

152-
con->set_body(response);
152+
con->set_body(std::move(response));
153153
con->set_status(websocketpp::http::status_code::ok);
154154
}
155155

test/connection/connection.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,20 @@ void http_func(server* s, websocketpp::connection_hdl hdl) {
175175
BOOST_CHECK_EQUAL(con->get_response_msg(), status_code::get_string(status_code::ok));
176176
}
177177

178+
void http_func_with_move(server* s, websocketpp::connection_hdl hdl) {
179+
using namespace websocketpp::http;
180+
181+
server::connection_ptr con = s->get_con_from_hdl(hdl);
182+
183+
std::string res = con->get_resource();
184+
185+
con->set_body(std::move(res));
186+
con->set_status(status_code::ok);
187+
188+
BOOST_CHECK_EQUAL(con->get_response_code(), status_code::ok);
189+
BOOST_CHECK_EQUAL(con->get_response_msg(), status_code::get_string(status_code::ok));
190+
}
191+
178192
void defer_http_func(server* s, bool * deferred, websocketpp::connection_hdl hdl) {
179193
*deferred = true;
180194

@@ -240,6 +254,18 @@ BOOST_AUTO_TEST_CASE( http_request ) {
240254
BOOST_CHECK_EQUAL(run_server_test(s,input), output);
241255
}
242256

257+
BOOST_AUTO_TEST_CASE( http_request_with_move ) {
258+
std::string input = "GET /foo/bar HTTP/1.1\r\nHost: www.example.com\r\nOrigin: http://www.example.com\r\n\r\n";
259+
std::string output = "HTTP/1.1 200 OK\r\nContent-Length: 8\r\nServer: ";
260+
output+=websocketpp::user_agent;
261+
output+="\r\n\r\n/foo/bar";
262+
263+
server s;
264+
s.set_http_handler(bind(&http_func_with_move,&s,::_1));
265+
266+
BOOST_CHECK_EQUAL(run_server_test(s,input), output);
267+
}
268+
243269
BOOST_AUTO_TEST_CASE( deferred_http_request ) {
244270
std::string input = "GET /foo/bar HTTP/1.1\r\nHost: www.example.com\r\nOrigin: http://www.example.com\r\n\r\n";
245271
std::string output = "HTTP/1.1 200 OK\r\nContent-Length: 8\r\nServer: ";

websocketpp/connection.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,7 @@ class connection
10411041
* @see websocketpp::http::response::set_body
10421042
*/
10431043
void set_body(std::string const & value);
1044+
void set_body( std::string&& value );
10441045

10451046
/// Append a header
10461047
/**

websocketpp/impl/connection_impl.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,16 @@ void connection<config>::set_body(std::string const & value) {
575575
m_response.set_body(value);
576576
}
577577

578+
template <typename config>
579+
void connection<config>::set_body( std::string&& value ) {
580+
if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
581+
throw exception("Call to set_status from invalid state",
582+
error::make_error_code(error::invalid_state));
583+
}
584+
585+
m_response.set_body(std::move(value));
586+
}
587+
578588
// TODO: EXCEPTION_FREE
579589
template <typename config>
580590
void connection<config>::append_header(std::string const & key,

0 commit comments

Comments
 (0)