From acb53a379cadf50791f4049c49fa09b63aea32df Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Wed, 29 Apr 2015 20:00:20 -0400 Subject: [PATCH] Refactor deferred http handler support to better match the library conventions --- test/connection/connection.cpp | 6 +++--- websocketpp/connection.hpp | 11 +++++++---- websocketpp/endpoint.hpp | 29 ++++++++++++++++++++++++++++ websocketpp/impl/connection_impl.hpp | 17 ++++++++++++---- websocketpp/impl/endpoint_impl.hpp | 14 ++++++++++++++ 5 files changed, 66 insertions(+), 11 deletions(-) diff --git a/test/connection/connection.cpp b/test/connection/connection.cpp index 6f3b26a..5491ab1 100644 --- a/test/connection/connection.cpp +++ b/test/connection/connection.cpp @@ -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, @@ -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); diff --git a/websocketpp/connection.hpp b/websocketpp/connection.hpp index 4eab294..203de6f 100644 --- a/websocketpp/connection.hpp +++ b/websocketpp/connection.hpp @@ -1076,7 +1076,7 @@ public: 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 @@ -1087,11 +1087,11 @@ public: * * @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 @@ -1103,6 +1103,9 @@ public: */ 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 diff --git a/websocketpp/endpoint.hpp b/websocketpp/endpoint.hpp index 91e17ea..e4c480d 100644 --- a/websocketpp/endpoint.hpp +++ b/websocketpp/endpoint.hpp @@ -516,6 +516,35 @@ public: /// 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 diff --git a/websocketpp/impl/connection_impl.hpp b/websocketpp/impl/connection_impl.hpp index 2d06183..30af1dd 100644 --- a/websocketpp/impl/connection_impl.hpp +++ b/websocketpp/impl/connection_impl.hpp @@ -644,10 +644,10 @@ void connection::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 -void connection::defer_http_response(lib::error_code & ec) { +lib::error_code connection::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) { @@ -658,10 +658,10 @@ void connection::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 @@ -687,6 +687,15 @@ void connection::send_http_response(lib::error_code & ec) { ec = lib::error_code(); } +template +void connection::send_http_response() { + lib::error_code ec; + this->send_http_response(ec); + if (ec) { + throw exception(ec); + } +} + diff --git a/websocketpp/impl/endpoint_impl.hpp b/websocketpp/impl/endpoint_impl.hpp index 5895199..e09cda9 100644 --- a/websocketpp/impl/endpoint_impl.hpp +++ b/websocketpp/impl/endpoint_impl.hpp @@ -142,7 +142,21 @@ void endpoint::resume_reading(connection_hdl hdl) { if (ec) { throw exception(ec); } } +template +void endpoint::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 +void endpoint::send_http_response(connection_hdl hdl) { + lib::error_code ec; + send_http_response(hdl,ec); + if (ec) { throw exception(ec); } +} template void endpoint::send(connection_hdl hdl, std::string const & payload,