Skip to content

Commit acb53a3

Browse files
author
Peter Thorson
committed
Refactor deferred http handler support to better match the library conventions
1 parent c18f210 commit acb53a3

File tree

5 files changed

+66
-11
lines changed

5 files changed

+66
-11
lines changed

test/connection/connection.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ void defer_http_func(server* s, bool * deferred, websocketpp::connection_hdl hdl
172172

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

175-
websocketpp::lib::error_code ec;
176-
con->defer_http_response(ec);
175+
websocketpp::lib::error_code ec = con->defer_http_response();
176+
BOOST_CHECK_EQUAL(ec, websocketpp::lib::error_code());
177177
}
178178

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

264264
websocketpp::lib::error_code ec;
265-
con->send_http_response(ec);
265+
s.send_http_response(con->get_handle(),ec);
266266
BOOST_CHECK_EQUAL(ec, websocketpp::lib::error_code());
267267
BOOST_CHECK_EQUAL(ostream.str(), output);
268268
con->send_http_response(ec);

websocketpp/connection.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ class connection
10761076
return m_request;
10771077
}
10781078

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

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

1106+
/// Send deferred HTTP Response
1107+
void send_http_response();
1108+
11061109
// TODO HTTPNBIO: write_headers
11071110
// function that processes headers + status so far and writes it to the wire
11081111
// beginning the HTTP response body state. This method will ignore anything

websocketpp/endpoint.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,35 @@ class endpoint : public config::transport_type, public config::endpoint_base {
516516
/// Resume reading of new data
517517
void resume_reading(connection_hdl hdl);
518518

519+
/// Send deferred HTTP Response
520+
/**
521+
* Sends an http response to an HTTP connection that was deferred. This will
522+
* send a complete response including all headers, status line, and body
523+
* text. The connection will be closed afterwards.
524+
*
525+
* Exception free variant
526+
*
527+
* @since 0.6.0
528+
*
529+
* @param hdl The connection to send the response on
530+
* @param ec A status code, zero on success, non-zero otherwise
531+
*/
532+
void send_http_response(connection_hdl hdl, lib::error_code & ec);
533+
534+
/// Send deferred HTTP Response (exception free)
535+
/**
536+
* Sends an http response to an HTTP connection that was deferred. This will
537+
* send a complete response including all headers, status line, and body
538+
* text. The connection will be closed afterwards.
539+
*
540+
* Exception variant
541+
*
542+
* @since 0.6.0
543+
*
544+
* @param hdl The connection to send the response on
545+
*/
546+
void send_http_response(connection_hdl hdl);
547+
519548
/// Create a message and add it to the outgoing send queue (exception free)
520549
/**
521550
* Convenience method to send a message given a payload string and an opcode

websocketpp/impl/connection_impl.hpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -644,10 +644,10 @@ void connection<config>::remove_header(std::string const & key)
644644
* Warning: deferred connections won't time out and as a result can tie up
645645
* resources.
646646
*
647-
* @param ec A status code, zero on success, non-zero otherwise
647+
* @return A status code, zero on success, non-zero otherwise
648648
*/
649649
template <typename config>
650-
void connection<config>::defer_http_response(lib::error_code & ec) {
650+
lib::error_code connection<config>::defer_http_response() {
651651
// Cancel handshake timer, otherwise the connection will time out and we'll
652652
// close the connection before the app has a chance to send a response.
653653
if (m_handshake_timer) {
@@ -658,10 +658,10 @@ void connection<config>::defer_http_response(lib::error_code & ec) {
658658
// Do something to signal deferral
659659
m_http_state = session::http_state::deferred;
660660

661-
ec = lib::error_code();
661+
return lib::error_code();
662662
}
663663

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

690+
template <typename config>
691+
void connection<config>::send_http_response() {
692+
lib::error_code ec;
693+
this->send_http_response(ec);
694+
if (ec) {
695+
throw exception(ec);
696+
}
697+
}
698+
690699

691700

692701

websocketpp/impl/endpoint_impl.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,21 @@ void endpoint<connection,config>::resume_reading(connection_hdl hdl) {
142142
if (ec) { throw exception(ec); }
143143
}
144144

145+
template <typename connection, typename config>
146+
void endpoint<connection,config>::send_http_response(connection_hdl hdl,
147+
lib::error_code & ec)
148+
{
149+
connection_ptr con = get_con_from_hdl(hdl,ec);
150+
if (ec) {return;}
151+
con->send_http_response(ec);
152+
}
145153

154+
template <typename connection, typename config>
155+
void endpoint<connection,config>::send_http_response(connection_hdl hdl) {
156+
lib::error_code ec;
157+
send_http_response(hdl,ec);
158+
if (ec) { throw exception(ec); }
159+
}
146160

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

0 commit comments

Comments
 (0)