Skip to content

Commit

Permalink
Merge pull request #1 from jmjatlanta/jmj_issue_999
Browse files Browse the repository at this point in the history
Added move version of set_body for performance (Issue zaphoyd#999)
  • Loading branch information
abitmore authored Jul 23, 2018
2 parents e6c4e3c + 3037682 commit 792cb45
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
28 changes: 28 additions & 0 deletions test/connection/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#include "connection_tu2.hpp"

#include <algorithm>

// Include special debugging transport
//#include <websocketpp/config/minimal_client.hpp>
#include <websocketpp/transport/debug/endpoint.hpp>
Expand Down Expand Up @@ -172,6 +174,20 @@ void http_func(server* s, websocketpp::connection_hdl hdl) {
BOOST_CHECK_EQUAL(con->get_response_msg(), status_code::get_string(status_code::ok));
}

void http_func_with_move(server* s, websocketpp::connection_hdl hdl) {
using namespace websocketpp::http;

server::connection_ptr con = s->get_con_from_hdl(hdl);

std::string res = con->get_resource();

con->set_body( std::move(res) );
con->set_status(status_code::ok);

BOOST_CHECK_EQUAL(con->get_response_code(), status_code::ok);
BOOST_CHECK_EQUAL(con->get_response_msg(), status_code::get_string(status_code::ok));
}

void defer_http_func(server* s, bool * deferred, websocketpp::connection_hdl hdl) {
*deferred = true;

Expand Down Expand Up @@ -237,6 +253,18 @@ BOOST_AUTO_TEST_CASE( http_request ) {
BOOST_CHECK_EQUAL(run_server_test(s,input), output);
}

BOOST_AUTO_TEST_CASE( http_request_with_move ) {
std::string input = "GET /foo/bar HTTP/1.1\r\nHost: www.example.com\r\nOrigin: http://www.example.com\r\n\r\n";
std::string output = "HTTP/1.1 200 OK\r\nContent-Length: 8\r\nServer: ";
output+=websocketpp::user_agent;
output+="\r\n\r\n/foo/bar";

server s;
s.set_http_handler(bind(&http_func_with_move,&s,::_1));

BOOST_CHECK_EQUAL(run_server_test(s,input), output);
}

BOOST_AUTO_TEST_CASE( deferred_http_request ) {
std::string input = "GET /foo/bar HTTP/1.1\r\nHost: www.example.com\r\nOrigin: http://www.example.com\r\n\r\n";
std::string output = "HTTP/1.1 200 OK\r\nContent-Length: 8\r\nServer: ";
Expand Down
1 change: 1 addition & 0 deletions websocketpp/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,7 @@ class connection
* @see websocketpp::http::response::set_body
*/
void set_body(std::string const & value);
void set_body( std::string&& value );

/// Append a header
/**
Expand Down
11 changes: 11 additions & 0 deletions websocketpp/impl/connection_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,17 @@ void connection<config>::set_body(std::string const & value) {
m_response.set_body(value);
}

template <typename config>
void connection<config>::set_body( std::string&& value )
{
if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
throw exception("Call to set_status from invalid state",
error::make_error_code(error::invalid_state));
}

m_response.set_body(std::move(value));
}

// TODO: EXCEPTION_FREE
template <typename config>
void connection<config>::append_header(std::string const & key,
Expand Down

0 comments on commit 792cb45

Please sign in to comment.