Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
connection_impl.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014, Peter Thorson. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of the WebSocket++ Project nor the
12 * names of its contributors may be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
27
28#ifndef WEBSOCKETPP_CONNECTION_IMPL_HPP
29#define WEBSOCKETPP_CONNECTION_IMPL_HPP
30
35
37
40
41#include <algorithm>
42#include <exception>
43#include <sstream>
44#include <string>
45#include <utility>
46#include <vector>
47
48namespace websocketpp {
49
50namespace istate = session::internal_state;
51
52template <typename config>
54 termination_handler new_handler)
55{
56 m_alog.write(log::alevel::devel,
57 "connection set_termination_handler");
58
59 //scoped_lock_type lock(m_connection_state_lock);
60
61 m_termination_handler = new_handler;
62}
63
64template <typename config>
65std::string const & connection<config>::get_origin() const {
66 //scoped_lock_type lock(m_connection_state_lock);
67 return m_processor->get_origin(m_request);
68}
69
70template <typename config>
72 //scoped_lock_type lock(m_connection_state_lock);
73 return m_send_buffer_size;
74}
75
76template <typename config>
78 //scoped_lock_type lock(m_connection_state_lock);
79 return m_state;
80}
81
82template <typename config>
83lib::error_code connection<config>::send(std::string const & payload,
85{
86 message_ptr msg = m_msg_manager->get_message(op,payload.size());
87 msg->append_payload(payload);
88 msg->set_compressed(true);
89
90 return send(msg);
91}
92
93template <typename config>
94lib::error_code connection<config>::send(void const * payload, size_t len,
96{
97 message_ptr msg = m_msg_manager->get_message(op,len);
98 msg->append_payload(payload,len);
99
100 return send(msg);
101}
102
103template <typename config>
104lib::error_code connection<config>::send(typename config::message_type::ptr msg)
105{
106 if (m_alog.static_test(log::alevel::devel)) {
107 m_alog.write(log::alevel::devel,"connection send");
108 }
109
110 {
111 scoped_lock_type lock(m_connection_state_lock);
112 if (m_state != session::state::open) {
114 }
115 }
116
117 message_ptr outgoing_msg;
118 bool needs_writing = false;
119
120 if (msg->get_prepared()) {
121 outgoing_msg = msg;
122
123 scoped_lock_type lock(m_write_lock);
124 write_push(outgoing_msg);
125 needs_writing = !m_write_flag && !m_send_queue.empty();
126 } else {
127 outgoing_msg = m_msg_manager->get_message();
128
129 if (!outgoing_msg) {
131 }
132
133 scoped_lock_type lock(m_write_lock);
134 lib::error_code ec = m_processor->prepare_data_frame(msg,outgoing_msg);
135
136 if (ec) {
137 return ec;
138 }
139
140 write_push(outgoing_msg);
141 needs_writing = !m_write_flag && !m_send_queue.empty();
142 }
143
144 if (needs_writing) {
145 transport_con_type::dispatch(lib::bind(
146 &type::write_frame,
147 type::get_shared()
148 ));
149 }
150
151 return lib::error_code();
152}
153
154template <typename config>
155void connection<config>::ping(std::string const& payload, lib::error_code& ec) {
156 if (m_alog.static_test(log::alevel::devel)) {
157 m_alog.write(log::alevel::devel,"connection ping");
158 }
159
160 {
161 scoped_lock_type lock(m_connection_state_lock);
162 if (m_state != session::state::open) {
163 std::stringstream ss;
164 ss << "connection::ping called from invalid state " << m_state;
165 m_alog.write(log::alevel::devel,ss.str());
167 return;
168 }
169 }
170
171 message_ptr msg = m_msg_manager->get_message();
172 if (!msg) {
174 return;
175 }
176
177 ec = m_processor->prepare_ping(payload,msg);
178 if (ec) {return;}
179
180 // set ping timer if we are listening for one
181 if (m_pong_timeout_handler) {
182 // Cancel any existing timers
183 if (m_ping_timer) {
184 m_ping_timer->cancel();
185 }
186
187 if (m_pong_timeout_dur > 0) {
188 m_ping_timer = transport_con_type::set_timer(
189 m_pong_timeout_dur,
190 lib::bind(
191 &type::handle_pong_timeout,
192 type::get_shared(),
193 payload,
194 lib::placeholders::_1
195 )
196 );
197 }
198
199 if (!m_ping_timer) {
200 // Our transport doesn't support timers
201 m_elog.write(log::elevel::warn,"Warning: a pong_timeout_handler is \
202 set but the transport in use does not support timeouts.");
203 }
204 }
205
206 bool needs_writing = false;
207 {
208 scoped_lock_type lock(m_write_lock);
209 write_push(msg);
210 needs_writing = !m_write_flag && !m_send_queue.empty();
211 }
212
213 if (needs_writing) {
214 transport_con_type::dispatch(lib::bind(
215 &type::write_frame,
216 type::get_shared()
217 ));
218 }
219
220 ec = lib::error_code();
221}
222
223template<typename config>
224void connection<config>::ping(std::string const & payload) {
225 lib::error_code ec;
226 ping(payload,ec);
227 if (ec) {
228 throw exception(ec);
229 }
230}
231
232template<typename config>
234 lib::error_code const & ec)
235{
236 if (ec) {
238 // ignore, this is expected
239 return;
240 }
241
242 m_elog.write(log::elevel::devel,"pong_timeout error: "+ec.message());
243 return;
244 }
245
246 if (m_pong_timeout_handler) {
247 m_pong_timeout_handler(m_connection_hdl,payload);
248 }
249}
250
251template <typename config>
252void connection<config>::pong(std::string const& payload, lib::error_code& ec) {
253 if (m_alog.static_test(log::alevel::devel)) {
254 m_alog.write(log::alevel::devel,"connection pong");
255 }
256
257 {
258 scoped_lock_type lock(m_connection_state_lock);
259 if (m_state != session::state::open) {
260 std::stringstream ss;
261 ss << "connection::pong called from invalid state " << m_state;
262 m_alog.write(log::alevel::devel,ss.str());
264 return;
265 }
266 }
267
268 message_ptr msg = m_msg_manager->get_message();
269 if (!msg) {
271 return;
272 }
273
274 ec = m_processor->prepare_pong(payload,msg);
275 if (ec) {return;}
276
277 bool needs_writing = false;
278 {
279 scoped_lock_type lock(m_write_lock);
280 write_push(msg);
281 needs_writing = !m_write_flag && !m_send_queue.empty();
282 }
283
284 if (needs_writing) {
285 transport_con_type::dispatch(lib::bind(
286 &type::write_frame,
287 type::get_shared()
288 ));
289 }
290
291 ec = lib::error_code();
292}
293
294template<typename config>
295void connection<config>::pong(std::string const & payload) {
296 lib::error_code ec;
297 pong(payload,ec);
298 if (ec) {
299 throw exception(ec);
300 }
301}
302
303template <typename config>
305 std::string const & reason, lib::error_code & ec)
306{
307 if (m_alog.static_test(log::alevel::devel)) {
308 m_alog.write(log::alevel::devel,"connection close");
309 }
310
311 // Truncate reason to maximum size allowable in a close frame.
312 std::string tr(reason,0,std::min<size_t>(reason.size(),
313 frame::limits::close_reason_size));
314
315 scoped_lock_type lock(m_connection_state_lock);
316
317 if (m_state != session::state::open) {
319 return;
320 }
321
322 ec = this->send_close_frame(code,tr,false,close::status::terminal(code));
323}
324
325template<typename config>
327 std::string const & reason)
328{
329 lib::error_code ec;
330 close(code,reason,ec);
331 if (ec) {
332 throw exception(ec);
333 }
334}
335
337
340template <typename config>
342 m_alog.write(log::alevel::devel,"connection connection::interrupt");
343 return transport_con_type::interrupt(
344 lib::bind(
345 &type::handle_interrupt,
346 type::get_shared()
347 )
348 );
349}
350
351
352template <typename config>
354 if (m_interrupt_handler) {
355 m_interrupt_handler(m_connection_hdl);
356 }
357}
358
359template <typename config>
361 m_alog.write(log::alevel::devel,"connection connection::pause_reading");
362 return transport_con_type::dispatch(
363 lib::bind(
364 &type::handle_pause_reading,
365 type::get_shared()
366 )
367 );
368}
369
371template <typename config>
373 m_alog.write(log::alevel::devel,"connection connection::handle_pause_reading");
374 m_read_flag = false;
375}
376
377template <typename config>
379 m_alog.write(log::alevel::devel,"connection connection::resume_reading");
380 return transport_con_type::dispatch(
381 lib::bind(
382 &type::handle_resume_reading,
383 type::get_shared()
384 )
385 );
386}
387
389template <typename config>
391 m_read_flag = true;
392 read_frame();
393}
394
395
396
397
398
399
400
401
402
403
404
405template <typename config>
407 //scoped_lock_type lock(m_connection_state_lock);
408 return m_uri->get_secure();
409}
410
411template <typename config>
412std::string const & connection<config>::get_host() const {
413 //scoped_lock_type lock(m_connection_state_lock);
414 return m_uri->get_host();
415}
416
417template <typename config>
418std::string const & connection<config>::get_resource() const {
419 //scoped_lock_type lock(m_connection_state_lock);
420 return m_uri->get_resource();
421}
422
423template <typename config>
425 //scoped_lock_type lock(m_connection_state_lock);
426 return m_uri->get_port();
427}
428
429template <typename config>
431 //scoped_lock_type lock(m_connection_state_lock);
432 return m_uri;
433}
434
435template <typename config>
437 //scoped_lock_type lock(m_connection_state_lock);
438 m_uri = uri;
439}
440
441
442
443
444
445
446template <typename config>
447std::string const & connection<config>::get_subprotocol() const {
448 return m_subprotocol;
449}
450
451template <typename config>
452std::vector<std::string> const &
454 return m_requested_subprotocols;
455}
456
457template <typename config>
459 lib::error_code & ec)
460{
461 if (m_is_server) {
463 return;
464 }
465
466 // If the value is empty or has a non-RFC2616 token character it is invalid.
467 if (value.empty() || std::find_if(value.begin(),value.end(),
468 http::is_not_token_char) != value.end())
469 {
471 return;
472 }
473
474 m_requested_subprotocols.push_back(value);
475}
476
477template <typename config>
479 lib::error_code ec;
480 this->add_subprotocol(value,ec);
481 if (ec) {
482 throw exception(ec);
483 }
484}
485
486
487template <typename config>
489 lib::error_code & ec)
490{
491 if (!m_is_server) {
493 return;
494 }
495
496 if (value.empty()) {
497 ec = lib::error_code();
498 return;
499 }
500
501 std::vector<std::string>::iterator it;
502
503 it = std::find(m_requested_subprotocols.begin(),
504 m_requested_subprotocols.end(),
505 value);
506
507 if (it == m_requested_subprotocols.end()) {
509 return;
510 }
511
512 m_subprotocol = value;
513}
514
515template <typename config>
517 lib::error_code ec;
518 this->select_subprotocol(value,ec);
519 if (ec) {
520 throw exception(ec);
521 }
522}
523
524
525template <typename config>
526std::string const &
527connection<config>::get_request_header(std::string const & key) const {
528 return m_request.get_header(key);
529}
530
531template <typename config>
532std::string const &
534 return m_request.get_body();
535}
536
537template <typename config>
538std::string const &
539connection<config>::get_response_header(std::string const & key) const {
540 return m_response.get_header(key);
541}
542
543// TODO: EXCEPTION_FREE
544template <typename config>
546{
547 if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
548 throw exception("Call to set_status from invalid state",
550 }
551 m_response.set_status(code);
552}
553
554// TODO: EXCEPTION_FREE
555template <typename config>
557 std::string const & msg)
558{
559 if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
560 throw exception("Call to set_status from invalid state",
562 }
563
564 m_response.set_status(code,msg);
565}
566
567// TODO: EXCEPTION_FREE
568template <typename config>
569void connection<config>::set_body(std::string const & value) {
570 if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
571 throw exception("Call to set_status from invalid state",
573 }
574
575 m_response.set_body(value);
576}
577
578template <typename config>
580 if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
581 throw exception("Call to set_status from invalid state",
583 }
584
585 m_response.set_body(std::move(value));
586}
587
588// TODO: EXCEPTION_FREE
589template <typename config>
590void connection<config>::append_header(std::string const & key,
591 std::string const & val)
592{
593 if (m_is_server) {
594 if (m_internal_state == istate::PROCESS_HTTP_REQUEST) {
595 // we are setting response headers for an incoming server connection
596 m_response.append_header(key,val);
597 } else {
598 throw exception("Call to append_header from invalid state",
600 }
601 } else {
602 if (m_internal_state == istate::USER_INIT) {
603 // we are setting initial headers for an outgoing client connection
604 m_request.append_header(key,val);
605 } else {
606 throw exception("Call to append_header from invalid state",
608 }
609 }
610}
611
612// TODO: EXCEPTION_FREE
613template <typename config>
614void connection<config>::replace_header(std::string const & key,
615 std::string const & val)
616{
617 if (m_is_server) {
618 if (m_internal_state == istate::PROCESS_HTTP_REQUEST) {
619 // we are setting response headers for an incoming server connection
620 m_response.replace_header(key,val);
621 } else {
622 throw exception("Call to replace_header from invalid state",
624 }
625 } else {
626 if (m_internal_state == istate::USER_INIT) {
627 // we are setting initial headers for an outgoing client connection
628 m_request.replace_header(key,val);
629 } else {
630 throw exception("Call to replace_header from invalid state",
632 }
633 }
634}
635
636// TODO: EXCEPTION_FREE
637template <typename config>
638void connection<config>::remove_header(std::string const & key)
639{
640 if (m_is_server) {
641 if (m_internal_state == istate::PROCESS_HTTP_REQUEST) {
642 // we are setting response headers for an incoming server connection
643 m_response.remove_header(key);
644 } else {
645 throw exception("Call to remove_header from invalid state",
647 }
648 } else {
649 if (m_internal_state == istate::USER_INIT) {
650 // we are setting initial headers for an outgoing client connection
651 m_request.remove_header(key);
652 } else {
653 throw exception("Call to remove_header from invalid state",
656 }
657}
658
660
670template <typename config>
672 // Cancel handshake timer, otherwise the connection will time out and we'll
673 // close the connection before the app has a chance to send a response.
674 if (m_handshake_timer) {
675 m_handshake_timer->cancel();
676 m_handshake_timer.reset();
677 }
678
679 // Do something to signal deferral
680 m_http_state = session::http_state::deferred;
681
682 return lib::error_code();
683}
684
686
695template <typename config>
696void connection<config>::send_http_response(lib::error_code & ec) {
697 {
698 scoped_lock_type lock(m_connection_state_lock);
699 if (m_http_state != session::http_state::deferred) {
701 return;
702 }
705 }
707 this->write_http_response(lib::error_code());
708 ec = lib::error_code();
709}
710
711template <typename config>
713 lib::error_code ec;
714 this->send_http_response(ec);
715 if (ec) {
716 throw exception(ec);
717 }
718}
719
720
721
722
723/******** logic thread ********/
724
725template <typename config>
727 m_alog.write(log::alevel::devel,"connection start");
729 if (m_internal_state != istate::USER_INIT) {
730 m_alog.write(log::alevel::devel,"Start called in invalid state");
732 return;
733 }
734
735 m_internal_state = istate::TRANSPORT_INIT;
736
737 // Depending on how the transport implements init this function may return
738 // immediately and call handle_transport_init later or call
739 // handle_transport_init from this function.
740 transport_con_type::init(
741 lib::bind(
742 &type::handle_transport_init,
743 type::get_shared(),
744 lib::placeholders::_1
745 )
746 );
747}
748
749template <typename config>
750void connection<config>::handle_transport_init(lib::error_code const & ec) {
751 m_alog.write(log::alevel::devel,"connection handle_transport_init");
752
753 lib::error_code ecm = ec;
754
755 if (m_internal_state != istate::TRANSPORT_INIT) {
756 m_alog.write(log::alevel::devel,
757 "handle_transport_init must be called from transport init state");
759 }
761 if (ecm) {
762 std::stringstream s;
763 s << "handle_transport_init received error: "<< ecm.message();
764 m_elog.write(log::elevel::rerror,s.str());
765
766 this->terminate(ecm);
767 return;
768 }
769
770 // At this point the transport is ready to read and write bytes.
771 if (m_is_server) {
772 m_internal_state = istate::READ_HTTP_REQUEST;
773 this->read_handshake(1);
774 } else {
775 // We are a client. Set the processor to the version specified in the
776 // config file and send a handshake request.
777 m_internal_state = istate::WRITE_HTTP_REQUEST;
778 m_processor = get_processor(config::client_version);
779 this->send_http_request();
780 }
781}
782
783template <typename config>
785 m_alog.write(log::alevel::devel,"connection read_handshake");
786
787 if (m_open_handshake_timeout_dur > 0) {
788 m_handshake_timer = transport_con_type::set_timer(
789 m_open_handshake_timeout_dur,
790 lib::bind(
791 &type::handle_open_handshake_timeout,
792 type::get_shared(),
793 lib::placeholders::_1
794 )
795 );
796 }
797
798 transport_con_type::async_read_at_least(
799 num_bytes,
800 m_buf,
801 config::connection_read_buffer_size,
802 lib::bind(
803 &type::handle_read_handshake,
804 type::get_shared(),
805 lib::placeholders::_1,
806 lib::placeholders::_2
807 )
808 );
809}
810
811// All exit paths for this function need to call write_http_response() or submit
812// a new read request with this function as the handler.
813template <typename config>
814void connection<config>::handle_read_handshake(lib::error_code const & ec,
815 size_t bytes_transferred)
816{
817 m_alog.write(log::alevel::devel,"connection handle_read_handshake");
818
819 lib::error_code ecm = ec;
820
821 if (!ecm) {
822 scoped_lock_type lock(m_connection_state_lock);
823
824 if (m_state == session::state::connecting) {
825 if (m_internal_state != istate::READ_HTTP_REQUEST) {
827 }
828 } else if (m_state == session::state::closed) {
829 // The connection was canceled while the response was being sent,
830 // usually by the handshake timer. This is basically expected
831 // (though hopefully rare) and there is nothing we can do so ignore.
832 m_alog.write(log::alevel::devel,
833 "handle_read_handshake invoked after connection was closed");
834 return;
835 } else {
837 }
838 }
839
840 if (ecm) {
841 if (ecm == transport::error::eof && m_state == session::state::closed) {
842 // we expect to get eof if the connection is closed already
843 m_alog.write(log::alevel::devel,
844 "got (expected) eof/state error from closed con");
845 return;
846 }
847
848 log_err(log::elevel::rerror,"handle_read_handshake",ecm);
849 this->terminate(ecm);
850 return;
851 }
852
853 // Boundaries checking. TODO: How much of this should be done?
854 if (bytes_transferred > config::connection_read_buffer_size) {
855 m_elog.write(log::elevel::fatal,"Fatal boundaries checking error.");
856 this->terminate(make_error_code(error::general));
857 return;
858 }
859
860 size_t bytes_processed = 0;
861 try {
862 bytes_processed = m_request.consume(m_buf,bytes_transferred);
863 } catch (http::exception &e) {
864 // All HTTP exceptions will result in this request failing and an error
865 // response being returned. No more bytes will be read in this con.
866 m_response.set_status(e.m_error_code,e.m_error_msg);
867 this->write_http_response_error(error::make_error_code(error::http_parse_error));
868 return;
869 }
870
871 // More paranoid boundaries checking.
872 // TODO: Is this overkill?
873 if (bytes_processed > bytes_transferred) {
874 m_elog.write(log::elevel::fatal,"Fatal boundaries checking error.");
875 this->terminate(make_error_code(error::general));
876 return;
877 }
878
879 if (m_alog.static_test(log::alevel::devel)) {
880 std::stringstream s;
881 s << "bytes_transferred: " << bytes_transferred
882 << " bytes, bytes processed: " << bytes_processed << " bytes";
883 m_alog.write(log::alevel::devel,s.str());
885
886 if (m_request.ready()) {
887 lib::error_code processor_ec = this->initialize_processor();
888 if (processor_ec) {
889 this->write_http_response_error(processor_ec);
890 return;
891 }
892
893 if (m_processor && m_processor->get_version() == 0) {
894 // Version 00 has an extra requirement to read some bytes after the
895 // handshake
896 if (bytes_transferred-bytes_processed >= 8) {
897 m_request.replace_header(
898 "Sec-WebSocket-Key3",
899 std::string(m_buf+bytes_processed,m_buf+bytes_processed+8)
900 );
901 bytes_processed += 8;
902 } else {
903 // TODO: need more bytes
904 m_alog.write(log::alevel::devel,"short key3 read");
905 m_response.set_status(http::status_code::internal_server_error);
906 this->write_http_response_error(processor::error::make_error_code(processor::error::short_key3));
907 return;
908 }
909 }
910
911 if (m_alog.static_test(log::alevel::devel)) {
912 m_alog.write(log::alevel::devel,m_request.raw());
913 if (!m_request.get_header("Sec-WebSocket-Key3").empty()) {
914 m_alog.write(log::alevel::devel,
915 utility::to_hex(m_request.get_header("Sec-WebSocket-Key3")));
916 }
917 }
918
919 // The remaining bytes in m_buf are frame data. Copy them to the
920 // beginning of the buffer and note the length. They will be read after
921 // the handshake completes and before more bytes are read.
922 std::copy(m_buf+bytes_processed,m_buf+bytes_transferred,m_buf);
923 m_buf_cursor = bytes_transferred-bytes_processed;
924
925
926 m_internal_state = istate::PROCESS_HTTP_REQUEST;
928 // We have the complete request. Process it.
929 lib::error_code handshake_ec = this->process_handshake_request();
930
931 // Write a response if this is a websocket connection or if it is an
932 // HTTP connection for which the response has not been deferred or
933 // started yet by a different system (i.e. still in init state).
934 if (!m_is_http || m_http_state == session::http_state::init) {
935 this->write_http_response(handshake_ec);
936 }
937 } else {
938 // read at least 1 more byte
939 transport_con_type::async_read_at_least(
940 1,
941 m_buf,
942 config::connection_read_buffer_size,
943 lib::bind(
944 &type::handle_read_handshake,
945 type::get_shared(),
946 lib::placeholders::_1,
947 lib::placeholders::_2
948 )
949 );
950 }
951}
952
953// write_http_response requires the request to be fully read and the connection
954// to be in the PROCESS_HTTP_REQUEST state. In some cases we can detect errors
955// before the request is fully read (specifically at a point where we aren't
956// sure if the hybi00 key3 bytes need to be read). This method sets the correct
957// state and calls write_http_response
958template <typename config>
959void connection<config>::write_http_response_error(lib::error_code const & ec) {
960 if (m_internal_state != istate::READ_HTTP_REQUEST) {
961 m_alog.write(log::alevel::devel,
962 "write_http_response_error called in invalid state");
964 return;
965 }
966
967 m_internal_state = istate::PROCESS_HTTP_REQUEST;
968
969 this->write_http_response(ec);
970}
971
972// All exit paths for this function need to call write_http_response() or submit
973// a new read request with this function as the handler.
974template <typename config>
975void connection<config>::handle_read_frame(lib::error_code const & ec,
976 size_t bytes_transferred)
977{
978 //m_alog.write(log::alevel::devel,"connection handle_read_frame");
979
980 lib::error_code ecm = ec;
981
982 if (!ecm && m_internal_state != istate::PROCESS_CONNECTION) {
984 }
985
986 if (ecm) {
988
989 if (ecm == transport::error::eof) {
990 if (m_state == session::state::closed) {
991 // we expect to get eof if the connection is closed already
992 // just ignore it
993 m_alog.write(log::alevel::devel,"got eof from closed con");
994 return;
995 } else if (m_state == session::state::closing && !m_is_server) {
996 // If we are a client we expect to get eof in the closing state,
997 // this is a signal to terminate our end of the connection after
998 // the closing handshake
999 terminate(lib::error_code());
1000 return;
1001 }
1002 } else if (ecm == error::invalid_state) {
1003 // In general, invalid state errors in the closed state are the
1004 // result of handlers that were in the system already when the state
1005 // changed and should be ignored as they pose no problems and there
1006 // is nothing useful that we can do about them.
1007 if (m_state == session::state::closed) {
1008 m_alog.write(log::alevel::devel,
1009 "handle_read_frame: got invalid istate in closed state");
1010 return;
1011 }
1012 } else if (ecm == transport::error::tls_short_read) {
1013 if (m_state == session::state::closed) {
1014 // We expect to get a TLS short read if we try to read after the
1015 // connection is closed. If this happens ignore and exit the
1016 // read frame path.
1017 terminate(lib::error_code());
1018 return;
1019 }
1020 echannel = log::elevel::rerror;
1021 } else if (ecm == transport::error::action_after_shutdown) {
1022 echannel = log::elevel::info;
1023 }
1024
1025 log_err(echannel, "handle_read_frame", ecm);
1026 this->terminate(ecm);
1027 return;
1029
1030 // Boundaries checking. TODO: How much of this should be done?
1031 /*if (bytes_transferred > config::connection_read_buffer_size) {
1032 m_elog.write(log::elevel::fatal,"Fatal boundaries checking error");
1033 this->terminate(make_error_code(error::general));
1034 return;
1035 }*/
1036
1037 size_t p = 0;
1038
1039 if (m_alog.static_test(log::alevel::devel)) {
1040 std::stringstream s;
1041 s << "p = " << p << " bytes transferred = " << bytes_transferred;
1042 m_alog.write(log::alevel::devel,s.str());
1045 while (p < bytes_transferred) {
1046 if (m_alog.static_test(log::alevel::devel)) {
1047 std::stringstream s;
1048 s << "calling consume with " << bytes_transferred-p << " bytes";
1049 m_alog.write(log::alevel::devel,s.str());
1050 }
1051
1052 lib::error_code consume_ec;
1053
1054 if (m_alog.static_test(log::alevel::devel)) {
1055 std::stringstream s;
1056 s << "Processing Bytes: " << utility::to_hex(reinterpret_cast<uint8_t*>(m_buf)+p,bytes_transferred-p);
1057 m_alog.write(log::alevel::devel,s.str());
1058 }
1059
1060 p += m_processor->consume(
1061 reinterpret_cast<uint8_t*>(m_buf)+p,
1062 bytes_transferred-p,
1063 consume_ec
1064 );
1065
1066 if (m_alog.static_test(log::alevel::devel)) {
1067 std::stringstream s;
1068 s << "bytes left after consume: " << bytes_transferred-p;
1069 m_alog.write(log::alevel::devel,s.str());
1070 }
1071 if (consume_ec) {
1072 log_err(log::elevel::rerror, "consume", consume_ec);
1073
1074 if (config::drop_on_protocol_error) {
1075 this->terminate(consume_ec);
1076 return;
1077 } else {
1078 lib::error_code close_ec;
1079 this->close(
1080 processor::error::to_ws(consume_ec),
1081 consume_ec.message(),
1082 close_ec
1083 );
1084
1085 if (close_ec) {
1086 log_err(log::elevel::fatal, "Protocol error close frame ", close_ec);
1087 this->terminate(close_ec);
1088 return;
1089 }
1090 }
1091 return;
1092 }
1093
1094 if (m_processor->ready()) {
1095 if (m_alog.static_test(log::alevel::devel)) {
1096 std::stringstream s;
1097 s << "Complete message received. Dispatching";
1098 m_alog.write(log::alevel::devel,s.str());
1099 }
1100
1101 message_ptr msg = m_processor->get_message();
1102
1103 if (!msg) {
1104 m_alog.write(log::alevel::devel, "null message from m_processor");
1105 } else if (!is_control(msg->get_opcode())) {
1106 // data message, dispatch to user
1107 if (m_state != session::state::open) {
1108 m_elog.write(log::elevel::warn, "got non-close frame while closing");
1109 } else if (m_message_handler) {
1110 m_message_handler(m_connection_hdl, msg);
1111 }
1112 } else {
1113 process_control_frame(msg);
1114 }
1115 }
1116 }
1117
1118 read_frame();
1119}
1120
1122template <typename config>
1124 if (!m_read_flag) {
1125 return;
1126 }
1127
1128 transport_con_type::async_read_at_least(
1129 // std::min wont work with undefined static const values.
1130 // TODO: is there a more elegant way to do this?
1131 // Need to determine if requesting 1 byte or the exact number of bytes
1132 // is better here. 1 byte lets us be a bit more responsive at a
1133 // potential expense of additional runs through handle_read_frame
1134 /*(m_processor->get_bytes_needed() > config::connection_read_buffer_size ?
1135 config::connection_read_buffer_size : m_processor->get_bytes_needed())*/
1136 1,
1137 m_buf,
1138 config::connection_read_buffer_size,
1139 m_handle_read_frame
1140 );
1141}
1142
1143template <typename config>
1145 m_alog.write(log::alevel::devel,"initialize_processor");
1146
1147 // if it isn't a websocket handshake nothing to do.
1148 if (!processor::is_websocket_handshake(m_request)) {
1149 return lib::error_code();
1150 }
1152 int version = processor::get_websocket_version(m_request);
1153
1154 if (version < 0) {
1155 m_alog.write(log::alevel::devel, "BAD REQUEST: can't determine version");
1156 m_response.set_status(http::status_code::bad_request);
1158 }
1159
1160 m_processor = get_processor(version);
1161
1162 // if the processor is not null we are done
1163 if (m_processor) {
1164 return lib::error_code();
1165 }
1166
1167 // We don't have a processor for this version. Return bad request
1168 // with Sec-WebSocket-Version header filled with values we do accept
1169 m_alog.write(log::alevel::devel, "BAD REQUEST: no processor for version");
1170 m_response.set_status(http::status_code::bad_request);
1171
1172 std::stringstream ss;
1173 std::string sep;
1174 std::vector<int>::const_iterator it;
1175 for (it = versions_supported.begin(); it != versions_supported.end(); it++)
1176 {
1177 ss << sep << *it;
1178 sep = ",";
1179 }
1180
1181 m_response.replace_header("Sec-WebSocket-Version",ss.str());
1183}
1184
1185template <typename config>
1187 m_alog.write(log::alevel::devel,"process handshake request");
1188
1189 if (!processor::is_websocket_handshake(m_request)) {
1190 // this is not a websocket handshake. Process as plain HTTP
1191 m_alog.write(log::alevel::devel,"HTTP REQUEST");
1192
1193 // extract URI from request
1195 m_request,
1196 (transport_con_type::is_secure() ? "https" : "http")
1197 );
1198
1199 if (!m_uri->get_valid()) {
1200 m_alog.write(log::alevel::devel, "Bad request: failed to parse uri");
1201 m_response.set_status(http::status_code::bad_request);
1204
1205 if (m_http_handler) {
1206 m_is_http = true;
1207 m_http_handler(m_connection_hdl);
1208
1209 if (m_state == session::state::closed) {
1212 } else {
1215 }
1216
1217 return lib::error_code();
1218 }
1219
1220 lib::error_code ec = m_processor->validate_handshake(m_request);
1221
1222 // Validate: make sure all required elements are present.
1223 if (ec){
1224 // Not a valid handshake request
1225 m_alog.write(log::alevel::devel, "Bad request " + ec.message());
1226 m_response.set_status(http::status_code::bad_request);
1227 return ec;
1228 }
1229
1230 // Read extension parameters and set up values necessary for the end user
1231 // to complete extension negotiation.
1232 std::pair<lib::error_code,std::string> neg_results;
1233 neg_results = m_processor->negotiate_extensions(m_request);
1234
1235 if (neg_results.first) {
1236 // There was a fatal error in extension parsing that should result in
1237 // a failed connection attempt.
1238 m_alog.write(log::alevel::devel, "Bad request: " + neg_results.first.message());
1239 m_response.set_status(http::status_code::bad_request);
1240 return neg_results.first;
1241 } else {
1242 // extension negotiation succeeded, set response header accordingly
1243 // we don't send an empty extensions header because it breaks many
1244 // clients.
1245 if (neg_results.second.size() > 0) {
1246 m_response.replace_header("Sec-WebSocket-Extensions",
1247 neg_results.second);
1248 }
1249 }
1250
1251 // extract URI from request
1252 m_uri = m_processor->get_uri(m_request);
1253
1254
1255 if (!m_uri->get_valid()) {
1256 m_alog.write(log::alevel::devel, "Bad request: failed to parse uri");
1257 m_response.set_status(http::status_code::bad_request);
1259 }
1260
1261 // extract subprotocols
1262 lib::error_code subp_ec = m_processor->extract_subprotocols(m_request,
1263 m_requested_subprotocols);
1264
1265 if (subp_ec) {
1266 // should we do anything?
1267 }
1268
1269 // Ask application to validate the connection
1270 if (!m_validate_handler || m_validate_handler(m_connection_hdl)) {
1271 m_response.set_status(http::status_code::switching_protocols);
1272
1273 // Write the appropriate response headers based on request and
1274 // processor version
1275 ec = m_processor->process_handshake(m_request,m_subprotocol,m_response);
1276
1277 if (ec) {
1278 std::stringstream s;
1279 s << "Processing error: " << ec << "(" << ec.message() << ")";
1280 m_alog.write(log::alevel::devel, s.str());
1281
1282 m_response.set_status(http::status_code::internal_server_error);
1283 return ec;
1284 }
1285 } else {
1286 // User application has rejected the handshake
1287 m_alog.write(log::alevel::devel, "USER REJECT");
1288
1289 // Use Bad Request if the user handler did not provide a more
1290 // specific http response error code.
1291 // TODO: is there a better default?
1292 if (m_response.get_status_code() == http::status_code::uninitialized) {
1293 m_response.set_status(http::status_code::bad_request);
1295
1297 }
1298
1299 return lib::error_code();
1302template <typename config>
1303void connection<config>::write_http_response(lib::error_code const & ec) {
1304 m_alog.write(log::alevel::devel,"connection write_http_response");
1305
1307 m_alog.write(log::alevel::http,"An HTTP handler took over the connection.");
1308 return;
1309 }
1311 if (m_response.get_status_code() == http::status_code::uninitialized) {
1312 m_response.set_status(http::status_code::internal_server_error);
1314 } else {
1315 m_ec = ec;
1318 m_response.set_version("HTTP/1.1");
1319
1320 // Set server header based on the user agent settings
1321 if (m_response.get_header("Server").empty()) {
1322 if (!m_user_agent.empty()) {
1323 m_response.replace_header("Server",m_user_agent);
1324 } else {
1325 m_response.remove_header("Server");
1326 }
1327 }
1328
1329 m_response.replace_header("Connection", "close");
1330
1331 // have the processor generate the raw bytes for the wire (if it exists)
1332 if (m_processor) {
1333 m_handshake_buffer = m_processor->get_raw(m_response);
1334 } else {
1335 // a processor wont exist for raw HTTP responses.
1336 m_handshake_buffer = m_response.raw();
1337 }
1338
1339 if (m_alog.static_test(log::alevel::devel)) {
1340 m_alog.write(log::alevel::devel,"Raw Handshake response:\n"+m_handshake_buffer);
1341 if (!m_response.get_header("Sec-WebSocket-Key3").empty()) {
1342 m_alog.write(log::alevel::devel,
1343 utility::to_hex(m_response.get_header("Sec-WebSocket-Key3")));
1344 }
1345 }
1347 // write raw bytes
1348 transport_con_type::async_write(
1349 m_handshake_buffer.data(),
1350 m_handshake_buffer.size(),
1351 lib::bind(
1352 &type::handle_write_http_response,
1353 type::get_shared(),
1354 lib::placeholders::_1
1355 )
1356 );
1357}
1358
1359template <typename config>
1360void connection<config>::handle_write_http_response(lib::error_code const & ec) {
1361 m_alog.write(log::alevel::devel,"handle_write_http_response");
1363 lib::error_code ecm = ec;
1364
1365 if (!ecm) {
1366 scoped_lock_type lock(m_connection_state_lock);
1367
1368 if (m_state == session::state::connecting) {
1369 if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
1371 }
1372 } else if (m_state == session::state::closed) {
1373 // The connection was canceled while the response was being sent,
1374 // usually by the handshake timer. This is basically expected
1375 // (though hopefully rare) and there is nothing we can do so ignore.
1376 m_alog.write(log::alevel::devel,
1377 "handle_write_http_response invoked after connection was closed");
1378 return;
1379 } else {
1381 }
1382 }
1383
1384 if (ecm) {
1385 if (ecm == transport::error::eof && m_state == session::state::closed) {
1386 // we expect to get eof if the connection is closed already
1387 m_alog.write(log::alevel::devel,
1388 "got (expected) eof/state error from closed con");
1389 return;
1390 }
1391
1392 log_err(log::elevel::rerror,"handle_write_http_response",ecm);
1393 this->terminate(ecm);
1394 return;
1395 }
1396
1397 if (m_handshake_timer) {
1398 m_handshake_timer->cancel();
1399 m_handshake_timer.reset();
1400 }
1401
1402 if (m_response.get_status_code() != http::status_code::switching_protocols)
1403 {
1404 /*if (m_processor || m_ec == error::http_parse_error ||
1405 m_ec == error::invalid_version || m_ec == error::unsupported_version
1406 || m_ec == error::upgrade_required)
1407 {*/
1408 if (!m_is_http) {
1409 std::stringstream s;
1410 s << "Handshake ended with HTTP error: "
1411 << m_response.get_status_code();
1412 m_elog.write(log::elevel::rerror,s.str());
1413 } else {
1414 // if this was not a websocket connection, we have written
1415 // the expected response and the connection can be closed.
1416
1417 this->log_http_result();
1418
1419 if (m_ec) {
1420 m_alog.write(log::alevel::devel,
1421 "got to writing HTTP results with m_ec set: "+m_ec.message());
1422 }
1423 m_ec = make_error_code(error::http_connection_ended);
1424 }
1425
1426 this->terminate(m_ec);
1427 return;
1428 }
1429
1430 this->log_open_result();
1431
1432 m_internal_state = istate::PROCESS_CONNECTION;
1433 m_state = session::state::open;
1434
1435 if (m_open_handler) {
1436 m_open_handler(m_connection_hdl);
1437 }
1438
1439 this->handle_read_frame(lib::error_code(), m_buf_cursor);
1440}
1441
1442template <typename config>
1443void connection<config>::send_http_request() {
1444 m_alog.write(log::alevel::devel,"connection send_http_request");
1445
1446 // TODO: origin header?
1447
1448 // Have the protocol processor fill in the appropriate fields based on the
1449 // selected client version
1450 if (m_processor) {
1451 lib::error_code ec;
1452 ec = m_processor->client_handshake_request(m_request,m_uri,
1453 m_requested_subprotocols);
1454
1455 if (ec) {
1456 log_err(log::elevel::fatal,"Internal library error: Processor",ec);
1457 return;
1458 }
1459 } else {
1460 m_elog.write(log::elevel::fatal,"Internal library error: missing processor");
1461 return;
1462 }
1463
1464 // Unless the user has overridden the user agent, send generic WS++ UA.
1465 if (m_request.get_header("User-Agent").empty()) {
1466 if (!m_user_agent.empty()) {
1467 m_request.replace_header("User-Agent",m_user_agent);
1468 } else {
1469 m_request.remove_header("User-Agent");
1470 }
1471 }
1472
1473 m_handshake_buffer = m_request.raw();
1474
1475 if (m_alog.static_test(log::alevel::devel)) {
1476 m_alog.write(log::alevel::devel,"Raw Handshake request:\n"+m_handshake_buffer);
1477 }
1478
1479 if (m_open_handshake_timeout_dur > 0) {
1480 m_handshake_timer = transport_con_type::set_timer(
1481 m_open_handshake_timeout_dur,
1482 lib::bind(
1483 &type::handle_open_handshake_timeout,
1484 type::get_shared(),
1485 lib::placeholders::_1
1486 )
1487 );
1488 }
1489
1490 transport_con_type::async_write(
1491 m_handshake_buffer.data(),
1492 m_handshake_buffer.size(),
1493 lib::bind(
1494 &type::handle_send_http_request,
1495 type::get_shared(),
1496 lib::placeholders::_1
1497 )
1498 );
1499}
1500
1501template <typename config>
1502void connection<config>::handle_send_http_request(lib::error_code const & ec) {
1503 m_alog.write(log::alevel::devel,"handle_send_http_request");
1504
1505 lib::error_code ecm = ec;
1506
1507 if (!ecm) {
1508 scoped_lock_type lock(m_connection_state_lock);
1509
1510 if (m_state == session::state::connecting) {
1511 if (m_internal_state != istate::WRITE_HTTP_REQUEST) {
1513 } else {
1514 m_internal_state = istate::READ_HTTP_RESPONSE;
1515 }
1516 } else if (m_state == session::state::closed) {
1517 // The connection was canceled while the response was being sent,
1518 // usually by the handshake timer. This is basically expected
1519 // (though hopefully rare) and there is nothing we can do so ignore.
1520 m_alog.write(log::alevel::devel,
1521 "handle_send_http_request invoked after connection was closed");
1522 return;
1523 } else {
1525 }
1526 }
1527
1528 if (ecm) {
1529 if (ecm == transport::error::eof && m_state == session::state::closed) {
1530 // we expect to get eof if the connection is closed already
1531 m_alog.write(log::alevel::devel,
1532 "got (expected) eof/state error from closed con");
1533 return;
1534 }
1535
1536 log_err(log::elevel::rerror,"handle_send_http_request",ecm);
1537 this->terminate(ecm);
1538 return;
1539 }
1540
1541 transport_con_type::async_read_at_least(
1542 1,
1543 m_buf,
1544 config::connection_read_buffer_size,
1545 lib::bind(
1546 &type::handle_read_http_response,
1547 type::get_shared(),
1548 lib::placeholders::_1,
1549 lib::placeholders::_2
1550 )
1551 );
1552}
1553
1554template <typename config>
1556 size_t bytes_transferred)
1557{
1558 m_alog.write(log::alevel::devel,"handle_read_http_response");
1559
1560 lib::error_code ecm = ec;
1561
1562 if (!ecm) {
1563 scoped_lock_type lock(m_connection_state_lock);
1564
1565 if (m_state == session::state::connecting) {
1566 if (m_internal_state != istate::READ_HTTP_RESPONSE) {
1568 }
1569 } else if (m_state == session::state::closed) {
1570 // The connection was canceled while the response was being sent,
1571 // usually by the handshake timer. This is basically expected
1572 // (though hopefully rare) and there is nothing we can do so ignore.
1573 m_alog.write(log::alevel::devel,
1574 "handle_read_http_response invoked after connection was closed");
1575 return;
1576 } else {
1578 }
1579 }
1580
1581 if (ecm) {
1582 if (ecm == transport::error::eof && m_state == session::state::closed) {
1583 // we expect to get eof if the connection is closed already
1584 m_alog.write(log::alevel::devel,
1585 "got (expected) eof/state error from closed con");
1586 return;
1587 }
1588
1589 log_err(log::elevel::rerror,"handle_read_http_response",ecm);
1590 this->terminate(ecm);
1591 return;
1592 }
1593
1594 size_t bytes_processed = 0;
1595 // TODO: refactor this to use error codes rather than exceptions
1596 try {
1597 bytes_processed = m_response.consume(m_buf,bytes_transferred);
1598 } catch (http::exception & e) {
1599 m_elog.write(log::elevel::rerror,
1600 std::string("error in handle_read_http_response: ")+e.what());
1601 this->terminate(make_error_code(error::general));
1602 return;
1603 }
1604
1605 m_alog.write(log::alevel::devel,std::string("Raw response: ")+m_response.raw());
1606
1607 if (m_response.headers_ready()) {
1608 if (m_handshake_timer) {
1609 m_handshake_timer->cancel();
1610 m_handshake_timer.reset();
1611 }
1612
1613 lib::error_code validate_ec = m_processor->validate_server_handshake_response(
1614 m_request,
1615 m_response
1616 );
1617 if (validate_ec) {
1618 log_err(log::elevel::rerror,"Server handshake response",validate_ec);
1619 this->terminate(validate_ec);
1620 return;
1621 }
1622
1623 // Read extension parameters and set up values necessary for the end
1624 // user to complete extension negotiation.
1625 std::pair<lib::error_code,std::string> neg_results;
1626 neg_results = m_processor->negotiate_extensions(m_response);
1627
1628 if (neg_results.first) {
1629 // There was a fatal error in extension negotiation. For the moment
1630 // kill all connections that fail extension negotiation.
1631
1632 // TODO: deal with cases where the response is well formed but
1633 // doesn't match the options requested by the client. Its possible
1634 // that the best behavior in this cases is to log and continue with
1635 // an unextended connection.
1636 m_alog.write(log::alevel::devel, "Extension negotiation failed: "
1637 + neg_results.first.message());
1638 this->terminate(make_error_code(error::extension_neg_failed));
1639 // TODO: close connection with reason 1010 (and list extensions)
1640 }
1641
1642 // response is valid, connection can now be assumed to be open
1643 m_internal_state = istate::PROCESS_CONNECTION;
1644 m_state = session::state::open;
1645
1646 this->log_open_result();
1647
1648 if (m_open_handler) {
1649 m_open_handler(m_connection_hdl);
1650 }
1651
1652 // The remaining bytes in m_buf are frame data. Copy them to the
1653 // beginning of the buffer and note the length. They will be read after
1654 // the handshake completes and before more bytes are read.
1655 std::copy(m_buf+bytes_processed,m_buf+bytes_transferred,m_buf);
1656 m_buf_cursor = bytes_transferred-bytes_processed;
1657
1658 this->handle_read_frame(lib::error_code(), m_buf_cursor);
1659 } else {
1660 transport_con_type::async_read_at_least(
1661 1,
1662 m_buf,
1663 config::connection_read_buffer_size,
1664 lib::bind(
1665 &type::handle_read_http_response,
1666 type::get_shared(),
1667 lib::placeholders::_1,
1668 lib::placeholders::_2
1669 )
1670 );
1671 }
1672}
1673
1674template <typename config>
1676 lib::error_code const & ec)
1677{
1679 m_alog.write(log::alevel::devel,"open handshake timer cancelled");
1680 } else if (ec) {
1681 m_alog.write(log::alevel::devel,
1682 "open handle_open_handshake_timeout error: "+ec.message());
1683 // TODO: ignore or fail here?
1684 } else {
1685 m_alog.write(log::alevel::devel,"open handshake timer expired");
1686 terminate(make_error_code(error::open_handshake_timeout));
1687 }
1688}
1689
1690template <typename config>
1692 lib::error_code const & ec)
1693{
1695 m_alog.write(log::alevel::devel,"asio close handshake timer cancelled");
1696 } else if (ec) {
1697 m_alog.write(log::alevel::devel,
1698 "asio open handle_close_handshake_timeout error: "+ec.message());
1699 // TODO: ignore or fail here?
1700 } else {
1701 m_alog.write(log::alevel::devel, "asio close handshake timer expired");
1702 terminate(make_error_code(error::close_handshake_timeout));
1703 }
1704}
1705
1706template <typename config>
1707void connection<config>::terminate(lib::error_code const & ec) {
1708 if (m_alog.static_test(log::alevel::devel)) {
1709 m_alog.write(log::alevel::devel,"connection terminate");
1710 }
1711
1712 // Cancel close handshake timer
1713 if (m_handshake_timer) {
1714 m_handshake_timer->cancel();
1715 m_handshake_timer.reset();
1716 }
1717
1718 terminate_status tstat = unknown;
1719 if (ec) {
1720 m_ec = ec;
1721 m_local_close_code = close::status::abnormal_close;
1722 m_local_close_reason = ec.message();
1723 }
1724
1725 // TODO: does any of this need a mutex?
1726 if (m_is_http) {
1727 m_http_state = session::http_state::closed;
1728 }
1729 if (m_state == session::state::connecting) {
1730 m_state = session::state::closed;
1731 tstat = failed;
1732
1733 // Log fail result here before socket is shut down and we can't get
1734 // the remote address, etc anymore
1735 if (m_ec != error::http_connection_ended) {
1736 log_fail_result();
1737 }
1738 } else if (m_state != session::state::closed) {
1739 m_state = session::state::closed;
1740 tstat = closed;
1741 } else {
1742 m_alog.write(log::alevel::devel,
1743 "terminate called on connection that was already terminated");
1744 return;
1745 }
1746
1747 // TODO: choose between shutdown and close based on error code sent
1748
1749 transport_con_type::async_shutdown(
1750 lib::bind(
1751 &type::handle_terminate,
1752 type::get_shared(),
1753 tstat,
1754 lib::placeholders::_1
1755 )
1756 );
1757}
1758
1759template <typename config>
1760void connection<config>::handle_terminate(terminate_status tstat,
1761 lib::error_code const & ec)
1762{
1763 if (m_alog.static_test(log::alevel::devel)) {
1764 m_alog.write(log::alevel::devel,"connection handle_terminate");
1765 }
1766
1767 if (ec) {
1768 // there was an error actually shutting down the connection
1769 log_err(log::elevel::devel,"handle_terminate",ec);
1770 }
1771
1772 // clean shutdown
1773 if (tstat == failed) {
1774 if (m_ec != error::http_connection_ended) {
1775 if (m_fail_handler) {
1776 m_fail_handler(m_connection_hdl);
1777 }
1778 }
1779 } else if (tstat == closed) {
1780 if (m_close_handler) {
1781 m_close_handler(m_connection_hdl);
1782 }
1783 log_close_result();
1784 } else {
1785 m_elog.write(log::elevel::rerror,"Unknown terminate_status");
1786 }
1787
1788 // call the termination handler if it exists
1789 // if it exists it might (but shouldn't) refer to a bad memory location.
1790 // If it does, we don't care and should catch and ignore it.
1791 if (m_termination_handler) {
1792 try {
1793 m_termination_handler(type::get_shared());
1794 } catch (std::exception const & e) {
1795 m_elog.write(log::elevel::warn,
1796 std::string("termination_handler call failed. Reason was: ")+e.what());
1797 }
1798 }
1799}
1800
1801template <typename config>
1803 //m_alog.write(log::alevel::devel,"connection write_frame");
1804
1805 {
1806 scoped_lock_type lock(m_write_lock);
1807
1808 // Check the write flag. If true, there is an outstanding transport
1809 // write already. In this case we just return. The write handler will
1810 // start a new write if the write queue isn't empty. If false, we set
1811 // the write flag and proceed to initiate a transport write.
1812 if (m_write_flag) {
1813 return;
1814 }
1815
1816 // pull off all the messages that are ready to write.
1817 // stop if we get a message marked terminal
1818 message_ptr next_message = write_pop();
1819 while (next_message) {
1820 m_current_msgs.push_back(next_message);
1821 if (!next_message->get_terminal()) {
1822 next_message = write_pop();
1823 } else {
1824 next_message = message_ptr();
1825 }
1826 }
1827
1828 if (m_current_msgs.empty()) {
1829 // there was nothing to send
1830 return;
1831 } else {
1832 // At this point we own the next messages to be sent and are
1833 // responsible for holding the write flag until they are
1834 // successfully sent or there is some error
1835 m_write_flag = true;
1836 }
1837 }
1838
1839 typename std::vector<message_ptr>::iterator it;
1840 for (it = m_current_msgs.begin(); it != m_current_msgs.end(); ++it) {
1841 std::string const & header = (*it)->get_header();
1842 std::string const & payload = (*it)->get_payload();
1843
1844 m_send_buffer.push_back(transport::buffer(header.c_str(),header.size()));
1845 m_send_buffer.push_back(transport::buffer(payload.c_str(),payload.size()));
1846 }
1847
1848 // Print detailed send stats if those log levels are enabled
1849 if (m_alog.static_test(log::alevel::frame_header)) {
1850 if (m_alog.dynamic_test(log::alevel::frame_header)) {
1851 std::stringstream general,header,payload;
1852
1853 general << "Dispatching write containing " << m_current_msgs.size()
1854 <<" message(s) containing ";
1855 header << "Header Bytes: \n";
1856 payload << "Payload Bytes: \n";
1857
1858 size_t hbytes = 0;
1859 size_t pbytes = 0;
1860
1861 for (size_t i = 0; i < m_current_msgs.size(); i++) {
1862 hbytes += m_current_msgs[i]->get_header().size();
1863 pbytes += m_current_msgs[i]->get_payload().size();
1864
1865
1866 header << "[" << i << "] ("
1867 << m_current_msgs[i]->get_header().size() << ") "
1868 << utility::to_hex(m_current_msgs[i]->get_header()) << "\n";
1869
1870 if (m_alog.static_test(log::alevel::frame_payload)) {
1871 if (m_alog.dynamic_test(log::alevel::frame_payload)) {
1872 payload << "[" << i << "] ("
1873 << m_current_msgs[i]->get_payload().size() << ") ["<<m_current_msgs[i]->get_opcode()<<"] "
1874 << (m_current_msgs[i]->get_opcode() == frame::opcode::text ?
1875 m_current_msgs[i]->get_payload() :
1876 utility::to_hex(m_current_msgs[i]->get_payload())
1877 )
1878 << "\n";
1879 }
1880 }
1881 }
1882
1883 general << hbytes << " header bytes and " << pbytes << " payload bytes";
1884
1885 m_alog.write(log::alevel::frame_header,general.str());
1886 m_alog.write(log::alevel::frame_header,header.str());
1887 m_alog.write(log::alevel::frame_payload,payload.str());
1888 }
1889 }
1890
1891 transport_con_type::async_write(
1892 m_send_buffer,
1893 m_write_frame_handler
1894 );
1895}
1896
1897template <typename config>
1898void connection<config>::handle_write_frame(lib::error_code const & ec)
1899{
1900 if (m_alog.static_test(log::alevel::devel)) {
1901 m_alog.write(log::alevel::devel,"connection handle_write_frame");
1902 }
1903
1904 bool terminal = m_current_msgs.back()->get_terminal();
1905
1906 m_send_buffer.clear();
1907 m_current_msgs.clear();
1908 // TODO: recycle instead of deleting
1909
1910 if (ec) {
1911 log_err(log::elevel::fatal,"handle_write_frame",ec);
1912 this->terminate(ec);
1913 return;
1914 }
1915
1916 if (terminal) {
1917 this->terminate(lib::error_code());
1918 return;
1919 }
1920
1921 bool needs_writing = false;
1922 {
1923 scoped_lock_type lock(m_write_lock);
1924
1925 // release write flag
1926 m_write_flag = false;
1927
1928 needs_writing = !m_send_queue.empty();
1929 }
1930
1931 if (needs_writing) {
1932 transport_con_type::dispatch(lib::bind(
1933 &type::write_frame,
1934 type::get_shared()
1935 ));
1936 }
1937}
1938
1939template <typename config>
1940std::vector<int> const & connection<config>::get_supported_versions() const
1941{
1942 return versions_supported;
1943}
1944
1945template <typename config>
1946void connection<config>::process_control_frame(typename config::message_type::ptr msg)
1947{
1948 m_alog.write(log::alevel::devel,"process_control_frame");
1949
1950 frame::opcode::value op = msg->get_opcode();
1951 lib::error_code ec;
1952
1953 std::stringstream s;
1954 s << "Control frame received with opcode " << op;
1955 m_alog.write(log::alevel::control,s.str());
1956
1957 if (m_state == session::state::closed) {
1958 m_elog.write(log::elevel::warn,"got frame in state closed");
1959 return;
1960 }
1961 if (op != frame::opcode::CLOSE && m_state != session::state::open) {
1962 m_elog.write(log::elevel::warn,"got non-close frame in state closing");
1963 return;
1964 }
1965
1966 if (op == frame::opcode::PING) {
1967 bool should_reply = true;
1968
1969 if (m_ping_handler) {
1970 should_reply = m_ping_handler(m_connection_hdl, msg->get_payload());
1971 }
1972
1973 if (should_reply) {
1974 this->pong(msg->get_payload(),ec);
1975 if (ec) {
1976 log_err(log::elevel::devel,"Failed to send response pong",ec);
1977 }
1978 }
1979 } else if (op == frame::opcode::PONG) {
1980 if (m_pong_handler) {
1981 m_pong_handler(m_connection_hdl, msg->get_payload());
1982 }
1983 if (m_ping_timer) {
1984 m_ping_timer->cancel();
1985 }
1986 } else if (op == frame::opcode::CLOSE) {
1987 m_alog.write(log::alevel::devel,"got close frame");
1988 // record close code and reason somewhere
1989
1990 m_remote_close_code = close::extract_code(msg->get_payload(),ec);
1991 if (ec) {
1992 s.str("");
1993 if (config::drop_on_protocol_error) {
1994 s << "Received invalid close code " << m_remote_close_code
1995 << " dropping connection per config.";
1996 m_elog.write(log::elevel::devel,s.str());
1997 this->terminate(ec);
1998 } else {
1999 s << "Received invalid close code " << m_remote_close_code
2000 << " sending acknowledgement and closing";
2001 m_elog.write(log::elevel::devel,s.str());
2002 ec = send_close_ack(close::status::protocol_error,
2003 "Invalid close code");
2004 if (ec) {
2005 log_err(log::elevel::devel,"send_close_ack",ec);
2006 }
2007 }
2008 return;
2009 }
2010
2011 m_remote_close_reason = close::extract_reason(msg->get_payload(),ec);
2012 if (ec) {
2013 if (config::drop_on_protocol_error) {
2014 m_elog.write(log::elevel::devel,
2015 "Received invalid close reason. Dropping connection per config");
2016 this->terminate(ec);
2017 } else {
2018 m_elog.write(log::elevel::devel,
2019 "Received invalid close reason. Sending acknowledgement and closing");
2020 ec = send_close_ack(close::status::protocol_error,
2021 "Invalid close reason");
2022 if (ec) {
2023 log_err(log::elevel::devel,"send_close_ack",ec);
2024 }
2025 }
2026 return;
2027 }
2028
2029 if (m_state == session::state::open) {
2030 s.str("");
2031 s << "Received close frame with code " << m_remote_close_code
2032 << " and reason " << m_remote_close_reason;
2033 m_alog.write(log::alevel::devel,s.str());
2034
2035 ec = send_close_ack();
2036 if (ec) {
2037 log_err(log::elevel::devel,"send_close_ack",ec);
2038 }
2039 } else if (m_state == session::state::closing && !m_was_clean) {
2040 // ack of our close
2041 m_alog.write(log::alevel::devel, "Got acknowledgement of close");
2042
2043 m_was_clean = true;
2044
2045 // If we are a server terminate the connection now. Clients should
2046 // leave the connection open to give the server an opportunity to
2047 // initiate the TCP close. The client's timer will handle closing
2048 // its side of the connection if the server misbehaves.
2049 //
2050 // TODO: different behavior if the underlying transport doesn't
2051 // support timers?
2052 if (m_is_server) {
2053 terminate(lib::error_code());
2054 }
2055 } else {
2056 // spurious, ignore
2057 m_elog.write(log::elevel::devel, "Got close frame in wrong state");
2058 }
2059 } else {
2060 // got an invalid control opcode
2061 m_elog.write(log::elevel::devel, "Got control frame with invalid opcode");
2062 // initiate protocol error shutdown
2063 }
2064}
2065
2066template <typename config>
2067lib::error_code connection<config>::send_close_ack(close::status::value code,
2068 std::string const & reason)
2069{
2070 return send_close_frame(code,reason,true,m_is_server);
2071}
2072
2073template <typename config>
2074lib::error_code connection<config>::send_close_frame(close::status::value code,
2075 std::string const & reason, bool ack, bool terminal)
2076{
2077 m_alog.write(log::alevel::devel,"send_close_frame");
2078
2079 // check for special codes
2080
2081 // If silent close is set, respect it and blank out close information
2082 // Otherwise use whatever has been specified in the parameters. If
2083 // parameters specifies close::status::blank then determine what to do
2084 // based on whether or not this is an ack. If it is not an ack just
2085 // send blank info. If it is an ack then echo the close information from
2086 // the remote endpoint.
2087 if (config::silent_close) {
2088 m_alog.write(log::alevel::devel,"closing silently");
2089 m_local_close_code = close::status::no_status;
2090 m_local_close_reason.clear();
2091 } else if (code != close::status::blank) {
2092 m_alog.write(log::alevel::devel,"closing with specified codes");
2093 m_local_close_code = code;
2094 m_local_close_reason = reason;
2095 } else if (!ack) {
2096 m_alog.write(log::alevel::devel,"closing with no status code");
2097 m_local_close_code = close::status::no_status;
2098 m_local_close_reason.clear();
2099 } else if (m_remote_close_code == close::status::no_status) {
2100 m_alog.write(log::alevel::devel,
2101 "acknowledging a no-status close with normal code");
2102 m_local_close_code = close::status::normal;
2103 m_local_close_reason.clear();
2104 } else {
2105 m_alog.write(log::alevel::devel,"acknowledging with remote codes");
2106 m_local_close_code = m_remote_close_code;
2107 m_local_close_reason = m_remote_close_reason;
2108 }
2109
2110 std::stringstream s;
2111 s << "Closing with code: " << m_local_close_code << ", and reason: "
2112 << m_local_close_reason;
2113 m_alog.write(log::alevel::devel,s.str());
2114
2115 message_ptr msg = m_msg_manager->get_message();
2116 if (!msg) {
2118 }
2119
2120 lib::error_code ec = m_processor->prepare_close(m_local_close_code,
2121 m_local_close_reason,msg);
2122 if (ec) {
2123 return ec;
2124 }
2125
2126 // Messages flagged terminal will result in the TCP connection being dropped
2127 // after the message has been written. This is typically used when servers
2128 // send an ack and when any endpoint encounters a protocol error
2129 if (terminal) {
2130 msg->set_terminal(true);
2131 }
2132
2133 m_state = session::state::closing;
2134
2135 if (ack) {
2136 m_was_clean = true;
2137 }
2138
2139 // Start a timer so we don't wait forever for the acknowledgement close
2140 // frame
2141 if (m_close_handshake_timeout_dur > 0) {
2142 m_handshake_timer = transport_con_type::set_timer(
2143 m_close_handshake_timeout_dur,
2144 lib::bind(
2145 &type::handle_close_handshake_timeout,
2146 type::get_shared(),
2147 lib::placeholders::_1
2148 )
2149 );
2150 }
2151
2152 bool needs_writing = false;
2153 {
2154 scoped_lock_type lock(m_write_lock);
2155 write_push(msg);
2156 needs_writing = !m_write_flag && !m_send_queue.empty();
2157 }
2158
2159 if (needs_writing) {
2160 transport_con_type::dispatch(lib::bind(
2161 &type::write_frame,
2162 type::get_shared()
2163 ));
2164 }
2165
2166 return lib::error_code();
2167}
2168
2169template <typename config>
2171connection<config>::get_processor(int version) const {
2172 // TODO: allow disabling certain versions
2173
2174 processor_ptr p;
2175
2176 switch (version) {
2177 case 0:
2178 p = lib::make_shared<processor::hybi00<config> >(
2179 transport_con_type::is_secure(),
2180 m_is_server,
2181 m_msg_manager
2182 );
2183 break;
2184 case 7:
2185 p = lib::make_shared<processor::hybi07<config> >(
2186 transport_con_type::is_secure(),
2187 m_is_server,
2188 m_msg_manager,
2189 lib::ref(m_rng)
2190 );
2191 break;
2192 case 8:
2193 p = lib::make_shared<processor::hybi08<config> >(
2194 transport_con_type::is_secure(),
2195 m_is_server,
2196 m_msg_manager,
2197 lib::ref(m_rng)
2198 );
2199 break;
2200 case 13:
2201 p = lib::make_shared<processor::hybi13<config> >(
2202 transport_con_type::is_secure(),
2203 m_is_server,
2204 m_msg_manager,
2205 lib::ref(m_rng)
2206 );
2207 break;
2208 default:
2209 return p;
2210 }
2211
2212 // Settings not configured by the constructor
2213 p->set_max_message_size(m_max_message_size);
2214
2215 return p;
2216}
2217
2218template <typename config>
2219void connection<config>::write_push(typename config::message_type::ptr msg)
2220{
2221 if (!msg) {
2222 return;
2223 }
2224
2225 m_send_buffer_size += msg->get_payload().size();
2226 m_send_queue.push(msg);
2227
2228 if (m_alog.static_test(log::alevel::devel)) {
2229 std::stringstream s;
2230 s << "write_push: message count: " << m_send_queue.size()
2231 << " buffer size: " << m_send_buffer_size;
2232 m_alog.write(log::alevel::devel,s.str());
2233 }
2234}
2235
2236template <typename config>
2237typename config::message_type::ptr connection<config>::write_pop()
2238{
2239 message_ptr msg;
2240
2241 if (m_send_queue.empty()) {
2242 return msg;
2243 }
2244
2245 msg = m_send_queue.front();
2246
2247 m_send_buffer_size -= msg->get_payload().size();
2248 m_send_queue.pop();
2249
2250 if (m_alog.static_test(log::alevel::devel)) {
2251 std::stringstream s;
2252 s << "write_pop: message count: " << m_send_queue.size()
2253 << " buffer size: " << m_send_buffer_size;
2254 m_alog.write(log::alevel::devel,s.str());
2255 }
2256 return msg;
2257}
2258
2259template <typename config>
2260void connection<config>::log_open_result()
2261{
2262 std::stringstream s;
2263
2264 int version;
2265 if (!processor::is_websocket_handshake(m_request)) {
2266 version = -1;
2267 } else {
2269 }
2270
2271 // Connection Type
2272 s << (version == -1 ? "HTTP" : "WebSocket") << " Connection ";
2273
2274 // Remote endpoint address
2275 s << transport_con_type::get_remote_endpoint() << " ";
2276
2277 // Version string if WebSocket
2278 if (version != -1) {
2279 s << "v" << version << " ";
2280 }
2281
2282 // User Agent
2283 std::string ua = m_request.get_header("User-Agent");
2284 if (ua.empty()) {
2285 s << "\"\" ";
2286 } else {
2287 // check if there are any quotes in the user agent
2288 s << "\"" << utility::string_replace_all(ua,"\"","\\\"") << "\" ";
2289 }
2290
2291 // URI
2292 s << (m_uri ? m_uri->get_resource() : "NULL") << " ";
2293
2294 // Status code
2295 s << m_response.get_status_code();
2296
2297 m_alog.write(log::alevel::connect,s.str());
2298}
2299
2300template <typename config>
2301void connection<config>::log_close_result()
2302{
2303 std::stringstream s;
2304
2305 s << "Disconnect "
2306 << "close local:[" << m_local_close_code
2307 << (m_local_close_reason.empty() ? "" : ","+m_local_close_reason)
2308 << "] remote:[" << m_remote_close_code
2309 << (m_remote_close_reason.empty() ? "" : ","+m_remote_close_reason) << "]";
2310
2311 m_alog.write(log::alevel::disconnect,s.str());
2312}
2313
2314template <typename config>
2315void connection<config>::log_fail_result()
2316{
2317 std::stringstream s;
2318
2320
2321 // Connection Type
2322 s << "WebSocket Connection ";
2323
2324 // Remote endpoint address & WebSocket version
2325 s << transport_con_type::get_remote_endpoint();
2326 if (version < 0) {
2327 s << " -";
2328 } else {
2329 s << " v" << version;
2330 }
2331
2332 // User Agent
2333 std::string ua = m_request.get_header("User-Agent");
2334 if (ua.empty()) {
2335 s << " \"\" ";
2336 } else {
2337 // check if there are any quotes in the user agent
2338 s << " \"" << utility::string_replace_all(ua,"\"","\\\"") << "\" ";
2339 }
2340
2341 // URI
2342 s << (m_uri ? m_uri->get_resource() : "-");
2343
2344 // HTTP Status code
2345 s << " " << m_response.get_status_code();
2346
2347 // WebSocket++ error code & reason
2348 s << " " << m_ec << " " << m_ec.message();
2349
2350 m_alog.write(log::alevel::fail,s.str());
2351}
2352
2353template <typename config>
2354void connection<config>::log_http_result() {
2355 std::stringstream s;
2356
2357 if (processor::is_websocket_handshake(m_request)) {
2358 m_alog.write(log::alevel::devel,"Call to log_http_result for WebSocket");
2359 return;
2360 }
2361
2362 // Connection Type
2363 s << (m_request.get_header("host").empty() ? "-" : m_request.get_header("host"))
2364 << " " << transport_con_type::get_remote_endpoint()
2365 << " \"" << m_request.get_method()
2366 << " " << (m_uri ? m_uri->get_resource() : "-")
2367 << " " << m_request.get_version() << "\" " << m_response.get_status_code()
2368 << " " << m_response.get_body().size();
2369
2370 // User Agent
2371 std::string ua = m_request.get_header("User-Agent");
2372 if (ua.empty()) {
2373 s << " \"\" ";
2374 } else {
2375 // check if there are any quotes in the user agent
2376 s << " \"" << utility::string_replace_all(ua,"\"","\\\"") << "\" ";
2377 }
2378
2379 m_alog.write(log::alevel::http,s.str());
2380}
2381
2382} // namespace websocketpp
2383
2384#endif // WEBSOCKETPP_CONNECTION_IMPL_HPP
const mie::Vuint & p
Definition bn.cpp:27
Represents an individual WebSocket connection.
void handle_interrupt()
Transport inturrupt callback.
lib::function< void(ptr)> termination_handler
void handle_close_handshake_timeout(lib::error_code const &ec)
lib::error_code interrupt()
Asyncronously invoke handler::on_inturrupt.
void start()
Start the connection state machine.
std::string const & get_request_body() const
Retrieve a request body.
void ping(std::string const &payload)
Send a ping.
void handle_read_http_response(lib::error_code const &ec, size_t bytes_transferred)
lib::error_code defer_http_response()
Defer HTTP Response until later (Exception free)
lib::error_code resume_reading()
Resume reading of new data.
void add_subprotocol(std::string const &request, lib::error_code &ec)
Adds the given subprotocol string to the request list (exception free)
bool get_secure() const
Returns the secure flag from the connection URI.
lib::shared_ptr< processor_type > processor_ptr
void set_body(std::string const &value)
Set response body content.
size_t get_buffered_amount() const
Get the size of the outgoing write buffer (in payload bytes)
std::string const & get_origin() const
Return the same origin policy origin value from the opening request.
message_type::ptr message_ptr
std::string const & get_host() const
Returns the host component of the connection URI.
void handle_terminate(terminate_status tstat, lib::error_code const &ec)
std::string const & get_resource() const
Returns the resource component of the connection URI.
void select_subprotocol(std::string const &value, lib::error_code &ec)
Select a subprotocol to use (exception free)
std::string const & get_request_header(std::string const &key) const
Retrieve a request header.
lib::error_code process_handshake_request()
void handle_pause_reading()
Pause reading callback.
std::vector< int > const & get_supported_versions() const
Get array of WebSocket protocol versions that this connection supports.
void remove_header(std::string const &key)
Remove a header.
uri_ptr get_uri() const
Gets the connection URI.
void handle_write_http_response(lib::error_code const &ec)
void terminate(lib::error_code const &ec)
std::string const & get_response_header(std::string const &key) const
Retrieve a response header.
void handle_write_frame(lib::error_code const &ec)
Process the results of a frame write operation and start the next write.
void write_frame()
Checks if there are frames in the send queue and if there are sends one.
session::state::value get_state() const
Return the connection state.
void set_status(http::status_code::value code)
Set response status code and message.
void replace_header(std::string const &key, std::string const &val)
Replace a header.
concurrency_type::scoped_lock_type scoped_lock_type
void handle_read_handshake(lib::error_code const &ec, size_t bytes_transferred)
void read_handshake(size_t num_bytes)
void pong(std::string const &payload)
Send a pong.
void send_http_response()
Send deferred HTTP Response.
void handle_read_frame(lib::error_code const &ec, size_t bytes_transferred)
lib::error_code initialize_processor()
void set_uri(uri_ptr uri)
Sets the connection URI.
void set_termination_handler(termination_handler new_handler)
void handle_transport_init(lib::error_code const &ec)
lib::error_code pause_reading()
Pause reading of new data.
void handle_resume_reading()
Resume reading callback.
std::vector< std::string > const & get_requested_subprotocols() const
Gets all of the subprotocols requested by the client.
void close(close::status::value const code, std::string const &reason)
Close the connection.
std::string const & get_subprotocol() const
Gets the negotated subprotocol.
void handle_open_handshake_timeout(lib::error_code const &ec)
void handle_pong_timeout(std::string payload, lib::error_code const &ec)
Utility method that gets called back when the ping timer expires.
void handle_send_http_request(lib::error_code const &ec)
lib::error_code send(std::string const &payload, frame::opcode::value op=frame::opcode::text)
Create a message and then add it to the outgoing send queue.
void read_frame()
Issue a new transport read unless reading is paused.
uint16_t get_port() const
Returns the port component of the connection URI.
void append_header(std::string const &key, std::string const &val)
Append a header.
websocketpp::config::asio_tls_client::message_type::ptr message_ptr
void close(T *e, websocketpp::connection_hdl hdl)
static const Segment ss(Segment::ss)
bool terminal(value code)
Determine if the code represents an unrecoverable error.
Definition close.hpp:212
uint16_t value
The type of a close code value.
Definition close.hpp:49
std::string extract_reason(std::string const &payload, lib::error_code &ec)
Extract the reason string from a close payload.
Definition close.hpp:322
status::value extract_code(std::string const &payload, lib::error_code &ec)
Extract a close code value from a close payload.
Definition close.hpp:283
@ general
Catch-all library error.
Definition error.hpp:47
@ unrequested_subprotocol
Selected subprotocol was not requested by the client.
Definition error.hpp:102
@ client_only
Attempted to use a client specific feature on a server endpoint.
Definition error.hpp:105
@ http_connection_ended
HTTP connection ended.
Definition error.hpp:111
@ no_outgoing_buffers
The endpoint is out of outgoing message buffers.
Definition error.hpp:68
@ http_parse_error
HTTP parse error.
Definition error.hpp:143
@ invalid_state
The connection was in the wrong state for this operation.
Definition error.hpp:74
@ extension_neg_failed
Extension negotiation failed.
Definition error.hpp:146
@ rejected
Connection rejected.
Definition error.hpp:130
@ unsupported_version
Unsupported WebSocket protocol version.
Definition error.hpp:140
@ server_only
Attempted to use a server specific feature on a client endpoint.
Definition error.hpp:108
@ close_handshake_timeout
WebSocket close handshake timed out.
Definition error.hpp:117
@ invalid_subprotocol
Invalid subprotocol.
Definition error.hpp:89
@ open_handshake_timeout
WebSocket opening handshake timed out.
Definition error.hpp:114
@ invalid_version
Invalid WebSocket protocol version.
Definition error.hpp:137
@ invalid_uri
An invalid uri was supplied.
Definition error.hpp:65
lib::error_code make_error_code(error::value e)
Definition error.hpp:235
bool is_control(value v)
Check if an opcode is for a control frame.
Definition frame.hpp:139
uint32_t level
Type of a channel package.
Definition levels.hpp:37
close::status::value to_ws(lib::error_code ec)
Converts a processor error_code into a websocket close code.
Definition base.hpp:261
lib::error_code make_error_code(error::processor_errors e)
Create an error code with the given value and the processor category.
Definition base.hpp:244
int get_websocket_version(request_type &r)
Extract the version from a WebSocket handshake request.
uri_ptr get_uri_from_host(request_type &request, std::string scheme)
Extract a URI ptr from the host header of the request.
bool is_websocket_handshake(request_type &r)
Determine whether or not a generic HTTP request is a WebSocket handshake.
Definition processor.hpp:68
@ operation_aborted
Operation aborted.
@ action_after_shutdown
read or write after shutdown
@ tls_short_read
TLS short read.
std::string to_hex(std::string const &input)
Convert std::string to ascii printed string of hex digits.
std::string string_replace_all(std::string subject, std::string const &search, std::string const &replace)
Replace all occurrances of a substring with another.
Namespace for the WebSocket++ project.
Definition base64.hpp:41
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition uri.hpp:351
#define value
Definition pkcs11.h:157
unsigned short uint16_t
Definition stdint.h:125
unsigned char uint8_t
Definition stdint.h:124
size_t size() const
Definition zm.h:519
static level const fail
One line for each failed WebSocket connection with details.
Definition levels.hpp:147
static level const devel
Development messages (warning: very chatty)
Definition levels.hpp:141
static level const frame_payload
One line per frame, includes the full message payload (warning: chatty)
Definition levels.hpp:129
static level const connect
Information about new connections.
Definition levels.hpp:121
static level const frame_header
One line per frame, includes the full frame header.
Definition levels.hpp:127
static level const control
One line per control frame.
Definition levels.hpp:125
static level const disconnect
One line for each closed connection. Includes closing codes and reasons.
Definition levels.hpp:123
static level const http
Access related to HTTP requests.
Definition levels.hpp:145
static level const devel
Low level debugging information (warning: very chatty)
Definition levels.hpp:63
static level const info
Definition levels.hpp:69
static level const fatal
Definition levels.hpp:78
static level const rerror
Definition levels.hpp:75
static level const warn
Definition levels.hpp:72
A simple utility buffer class.
void lock()
char * s
size_t len
bool terminate
bool unknown