Skip to content

Commit

Permalink
Refactor deferred http handler support to better match the library co…
Browse files Browse the repository at this point in the history
…nventions
  • Loading branch information
Peter Thorson committed Apr 30, 2015
1 parent c18f210 commit acb53a3
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 11 deletions.
6 changes: 3 additions & 3 deletions test/connection/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ void defer_http_func(server* s, bool * deferred, websocketpp::connection_hdl hdl

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

websocketpp::lib::error_code ec;
con->defer_http_response(ec);
websocketpp::lib::error_code ec = con->defer_http_response();
BOOST_CHECK_EQUAL(ec, websocketpp::lib::error_code());
}

void check_on_fail(server* s, websocketpp::lib::error_code ec, bool & called,
Expand Down Expand Up @@ -262,7 +262,7 @@ BOOST_AUTO_TEST_CASE( deferred_http_request ) {
con->set_status(websocketpp::http::status_code::ok);

websocketpp::lib::error_code ec;
con->send_http_response(ec);
s.send_http_response(con->get_handle(),ec);
BOOST_CHECK_EQUAL(ec, websocketpp::lib::error_code());
BOOST_CHECK_EQUAL(ostream.str(), output);
con->send_http_response(ec);
Expand Down
11 changes: 7 additions & 4 deletions websocketpp/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ class connection
return m_request;
}

/// Defer HTTP Response until later
/// Defer HTTP Response until later (Exception free)
/**
* Used in the http handler to defer the HTTP response for this connection
* until later. Handshake timers will be canceled and the connection will be
Expand All @@ -1087,11 +1087,11 @@ class connection
*
* @since 0.6.0
*
* @param ec A status code, zero on success, non-zero otherwise
* @return A status code, zero on success, non-zero otherwise
*/
void defer_http_response(lib::error_code & ec);
lib::error_code defer_http_response();

/// Send deferred HTTP Response
/// Send deferred HTTP Response (exception free)
/**
* Sends an http response to an HTTP connection that was deferred. This will
* send a complete response including all headers, status line, and body
Expand All @@ -1103,6 +1103,9 @@ class connection
*/
void send_http_response(lib::error_code & ec);

/// Send deferred HTTP Response
void send_http_response();

// TODO HTTPNBIO: write_headers
// function that processes headers + status so far and writes it to the wire
// beginning the HTTP response body state. This method will ignore anything
Expand Down
29 changes: 29 additions & 0 deletions websocketpp/endpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,35 @@ class endpoint : public config::transport_type, public config::endpoint_base {
/// Resume reading of new data
void resume_reading(connection_hdl hdl);

/// Send deferred HTTP Response
/**
* Sends an http response to an HTTP connection that was deferred. This will
* send a complete response including all headers, status line, and body
* text. The connection will be closed afterwards.
*
* Exception free variant
*
* @since 0.6.0
*
* @param hdl The connection to send the response on
* @param ec A status code, zero on success, non-zero otherwise
*/
void send_http_response(connection_hdl hdl, lib::error_code & ec);

/// Send deferred HTTP Response (exception free)
/**
* Sends an http response to an HTTP connection that was deferred. This will
* send a complete response including all headers, status line, and body
* text. The connection will be closed afterwards.
*
* Exception variant
*
* @since 0.6.0
*
* @param hdl The connection to send the response on
*/
void send_http_response(connection_hdl hdl);

/// Create a message and add it to the outgoing send queue (exception free)
/**
* Convenience method to send a message given a payload string and an opcode
Expand Down
17 changes: 13 additions & 4 deletions websocketpp/impl/connection_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,10 +644,10 @@ void connection<config>::remove_header(std::string const & key)
* Warning: deferred connections won't time out and as a result can tie up
* resources.
*
* @param ec A status code, zero on success, non-zero otherwise
* @return A status code, zero on success, non-zero otherwise
*/
template <typename config>
void connection<config>::defer_http_response(lib::error_code & ec) {
lib::error_code connection<config>::defer_http_response() {
// Cancel handshake timer, otherwise the connection will time out and we'll
// close the connection before the app has a chance to send a response.
if (m_handshake_timer) {
Expand All @@ -658,10 +658,10 @@ void connection<config>::defer_http_response(lib::error_code & ec) {
// Do something to signal deferral
m_http_state = session::http_state::deferred;

ec = lib::error_code();
return lib::error_code();
}

/// Send deferred HTTP Response
/// Send deferred HTTP Response (exception free)
/**
* Sends an http response to an HTTP connection that was deferred. This will
* send a complete response including all headers, status line, and body
Expand All @@ -687,6 +687,15 @@ void connection<config>::send_http_response(lib::error_code & ec) {
ec = lib::error_code();
}

template <typename config>
void connection<config>::send_http_response() {
lib::error_code ec;
this->send_http_response(ec);
if (ec) {
throw exception(ec);
}
}




Expand Down
14 changes: 14 additions & 0 deletions websocketpp/impl/endpoint_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,21 @@ void endpoint<connection,config>::resume_reading(connection_hdl hdl) {
if (ec) { throw exception(ec); }
}

template <typename connection, typename config>
void endpoint<connection,config>::send_http_response(connection_hdl hdl,
lib::error_code & ec)
{
connection_ptr con = get_con_from_hdl(hdl,ec);
if (ec) {return;}
con->send_http_response(ec);
}

template <typename connection, typename config>
void endpoint<connection,config>::send_http_response(connection_hdl hdl) {
lib::error_code ec;
send_http_response(hdl,ec);
if (ec) { throw exception(ec); }
}

template <typename connection, typename config>
void endpoint<connection,config>::send(connection_hdl hdl, std::string const & payload,
Expand Down

0 comments on commit acb53a3

Please sign in to comment.