28 #ifndef WEBSOCKETPP_PROCESSOR_HYBI13_HPP
29 #define WEBSOCKETPP_PROCESSOR_HYBI13_HPP
31 #include <websocketpp/processors/processor.hpp>
33 #include <websocketpp/frame.hpp>
34 #include <websocketpp/http/constants.hpp>
36 #include <websocketpp/utf8_validator.hpp>
37 #include <websocketpp/sha1/sha1.hpp>
38 #include <websocketpp/base64/base64.hpp>
40 #include <websocketpp/common/network.hpp>
41 #include <websocketpp/common/platforms.hpp>
53 template <
typename config>
56 typedef processor<config> base;
58 typedef typename config::request_type request_type;
59 typedef typename config::response_type response_type;
61 typedef typename config::message_type message_type;
62 typedef typename message_type::ptr message_ptr;
64 typedef typename config::con_msg_manager_type msg_manager_type;
65 typedef typename msg_manager_type::ptr msg_manager_ptr;
66 typedef typename config::rng_type rng_type;
68 typedef typename config::permessage_deflate_type permessage_deflate_type;
70 typedef std::pair<lib::error_code,std::string> err_str_pair;
72 explicit hybi13(
bool secure,
bool p_is_server, msg_manager_ptr manager, rng_type& rng)
73 : processor<config>(secure, p_is_server)
74 , m_msg_manager(manager)
84 bool has_permessage_deflate()
const {
85 return m_permessage_deflate.is_implemented();
89 return negotiate_extensions_helper(request);
93 return negotiate_extensions_helper(response);
101 template <
typename header_type>
107 if (!config::enable_extensions) {
108 ret.first = make_error_code(error::extensions_disabled);
112 http::parameter_list p;
114 bool error = header.get_header_as_plist(
"Sec-WebSocket-Extensions",p);
117 ret.first = make_error_code(error::extension_parse_error);
126 http::parameter_list::const_iterator it;
130 if (m_permessage_deflate.is_implemented()) {
131 err_str_pair neg_ret;
132 for (it = p.begin(); it != p.end(); ++it) {
134 if (it->first !=
"permessage-deflate") {
141 if (m_permessage_deflate.is_enabled()) {
146 neg_ret = m_permessage_deflate.negotiate(it->second);
158 lib::error_code ec = m_permessage_deflate.init(base::m_server);
171 ret.second += neg_ret.second;
186 if (r.get_method() !=
"GET") {
187 return make_error_code(error::invalid_http_method);
190 if (r.get_version() !=
"HTTP/1.1") {
191 return make_error_code(error::invalid_http_version);
198 if (r.get_header(
"Sec-WebSocket-Key").empty()) {
199 return make_error_code(error::missing_required_header);
202 return lib::error_code();
212 std::string server_key = request.get_header(
"Sec-WebSocket-Key");
214 lib::error_code ec = process_handshake_key(server_key);
220 response.replace_header(
"Sec-WebSocket-Accept",server_key);
221 response.append_header(
"Upgrade",constants::upgrade_token);
222 response.append_header(
"Connection",constants::connection_token);
224 if (!subprotocol.empty()) {
225 response.replace_header(
"Sec-WebSocket-Protocol",subprotocol);
228 return lib::error_code();
240 req.set_method(
"GET");
241 req.set_uri(uri->get_resource());
242 req.set_version(
"HTTP/1.1");
244 req.append_header(
"Upgrade",
"websocket");
245 req.append_header(
"Connection",
"Upgrade");
246 req.replace_header(
"Sec-WebSocket-Version",
"13");
247 req.replace_header(
"Host",uri->get_host_port());
249 if (!subprotocols.empty()) {
250 std::ostringstream result;
251 std::vector<std::string>::const_iterator it = subprotocols.begin();
253 while (it != subprotocols.end()) {
254 result <<
", " << *it++;
257 req.replace_header(
"Sec-WebSocket-Protocol",result.str());
261 frame::uint32_converter conv;
262 unsigned char raw_key[16];
264 for (
int i = 0; i < 4; i++) {
266 std::copy(conv.c,conv.c+4,&raw_key[i*4]);
269 req.replace_header(
"Sec-WebSocket-Key",base64_encode(raw_key, 16));
271 if (m_permessage_deflate.is_implemented()) {
272 std::string offer = m_permessage_deflate.generate_offer();
273 if (!offer.empty()) {
274 req.replace_header(
"Sec-WebSocket-Extensions",offer);
278 return lib::error_code();
291 if (res.get_status_code() != http::status_code::switching_protocols) {
292 return error::make_error_code(error::invalid_http_status);
296 std::string
const & upgrade_header = res.get_header(
"Upgrade");
297 if (utility::ci_find_substr(upgrade_header, constants::upgrade_token,
298 sizeof(constants::upgrade_token)-1) == upgrade_header.end())
300 return error::make_error_code(error::missing_required_header);
304 std::string
const & con_header = res.get_header(
"Connection");
305 if (utility::ci_find_substr(con_header, constants::connection_token,
306 sizeof(constants::connection_token)-1) == con_header.end())
308 return error::make_error_code(error::missing_required_header);
312 std::string key = req.get_header(
"Sec-WebSocket-Key");
313 lib::error_code ec = process_handshake_key(key);
315 if (ec || key != res.get_header(
"Sec-WebSocket-Accept")) {
316 return error::make_error_code(error::missing_required_header);
321 return lib::error_code();
329 return r.get_header(
"Origin");
335 if (!req.get_header(
"Sec-WebSocket-Protocol").empty()) {
336 http::parameter_list p;
338 if (!req.get_header_as_plist(
"Sec-WebSocket-Protocol",p)) {
339 http::parameter_list::const_iterator it;
341 for (it = p.begin(); it != p.end(); ++it) {
342 subprotocol_list.push_back(it->first);
345 return error::make_error_code(error::subprotocol_parse_error);
348 return lib::error_code();
352 return get_uri_from_host(request,(base::m_secure ?
"wss" :
"ws"));
385 ec = lib::error_code();
391 while (m_state != READY && m_state != FATAL_ERROR &&
392 (p < len || m_bytes_needed == 0))
394 if (m_state == HEADER_BASIC) {
395 p +=
this->copy_basic_header_bytes(buf+p,len-p);
397 if (m_bytes_needed > 0) {
401 ec =
this->validate_incoming_basic_header(
402 m_basic_header, base::m_server, !m_data_msg.msg_ptr
407 m_state = HEADER_EXTENDED;
409 m_bytes_needed = frame::get_header_len(m_basic_header) -
410 frame::BASIC_HEADER_LENGTH;
411 }
else if (m_state == HEADER_EXTENDED) {
412 p +=
this->copy_extended_header_bytes(buf+p,len-p);
414 if (m_bytes_needed > 0) {
418 ec = validate_incoming_extended_header(m_basic_header,m_extended_header);
421 m_state = APPLICATION;
422 m_bytes_needed =
static_cast<size_t>(get_payload_size(m_basic_header,m_extended_header));
426 frame::opcode::value op = frame::get_opcode(m_basic_header);
430 if (frame::opcode::is_control(op)) {
431 m_control_msg = msg_metadata(
432 m_msg_manager->get_message(op,m_bytes_needed),
433 frame::get_masking_key(m_basic_header,m_extended_header)
436 m_current_msg = &m_control_msg;
438 if (!m_data_msg.msg_ptr) {
439 if (m_bytes_needed > base::m_max_message_size) {
440 ec = make_error_code(error::message_too_big);
444 m_data_msg = msg_metadata(
445 m_msg_manager->get_message(op,m_bytes_needed),
446 frame::get_masking_key(m_basic_header,m_extended_header)
449 if (m_permessage_deflate.is_enabled()) {
450 m_data_msg.msg_ptr->set_compressed(frame::get_rsv1(m_basic_header));
455 std::string & out = m_data_msg.msg_ptr->get_raw_payload();
457 if (out.size() + m_bytes_needed > base::m_max_message_size) {
458 ec = make_error_code(error::message_too_big);
464 m_data_msg.prepared_key = prepare_masking_key(
465 frame::get_masking_key(
471 out.reserve(out.size() + m_bytes_needed);
473 m_current_msg = &m_data_msg;
475 }
else if (m_state == EXTENSION) {
476 m_state = APPLICATION;
477 }
else if (m_state == APPLICATION) {
478 size_t bytes_to_process = (std::min)(m_bytes_needed,len-p);
480 if (bytes_to_process > 0) {
481 p +=
this->process_payload_bytes(buf+p,bytes_to_process,ec);
486 if (m_bytes_needed > 0) {
492 if (frame::get_fin(m_basic_header)) {
493 ec = finalize_message();
498 this->reset_headers();
502 ec = make_error_code(error::general);
519 std::string & out = m_current_msg->msg_ptr->get_raw_payload();
523 if (m_permessage_deflate.is_enabled()
524 && m_current_msg->msg_ptr->get_compressed())
526 uint8_t trailer[4] = {0x00, 0x00, 0xff, 0xff};
530 ec = m_permessage_deflate.decompress(trailer,4,out);
537 if (frame::get_opcode(m_basic_header) == frame::opcode::TEXT) {
538 if (!m_current_msg->validator.complete()) {
539 return make_error_code(error::invalid_utf8);
545 return lib::error_code();
548 void reset_headers() {
549 m_state = HEADER_BASIC;
550 m_bytes_needed = frame::BASIC_HEADER_LENGTH;
552 m_basic_header.b0 = 0x00;
553 m_basic_header.b1 = 0x00;
556 m_extended_header.bytes,
557 frame::MAX_EXTENDED_HEADER_LENGTH,
564 return (m_state == READY);
569 return message_ptr();
571 message_ptr ret = m_current_msg->msg_ptr;
572 m_current_msg->msg_ptr.reset();
574 if (frame::opcode::is_control(ret->get_opcode())) {
575 m_control_msg.msg_ptr.reset();
577 m_data_msg.msg_ptr.reset();
580 this->reset_headers();
587 return m_state == FATAL_ERROR;
591 return m_bytes_needed;
608 return make_error_code(error::invalid_arguments);
611 frame::opcode::value op = in->get_opcode();
614 if (frame::opcode::is_control(op)) {
615 return make_error_code(error::invalid_opcode);
618 std::string& i = in->get_raw_payload();
619 std::string& o = out->get_raw_payload();
622 if (op == frame::opcode::TEXT && !utf8_validator::validate(i)) {
623 return make_error_code(error::invalid_payload);
626 frame::masking_key_type key;
627 bool masked = !base::m_server;
628 bool compressed = m_permessage_deflate.is_enabled()
629 && in->get_compressed();
630 bool fin = in->get_fin();
642 m_permessage_deflate.compress(i,o);
645 return make_error_code(error::general);
650 o.resize(o.size()-4);
654 this->masked_copy(o,o,key);
664 this->masked_copy(i,o,key);
666 std::copy(i.begin(),i.end(),o.begin());
671 frame::basic_header h(op,o.size(),fin,masked,compressed);
674 frame::extended_header e(o.size(),key.i);
675 out->set_header(frame::prepare_header(h,e));
677 frame::extended_header e(o.size());
678 out->set_header(frame::prepare_header(h,e));
681 out->set_prepared(
true);
684 return lib::error_code();
689 return this->prepare_control(frame::opcode::PING,in,out);
692 lib::error_code prepare_pong(std::string
const & in, message_ptr out)
const {
693 return this->prepare_control(frame::opcode::PONG,in,out);
696 virtual lib::error_code prepare_close(close::status::value code,
697 std::string
const & reason, message_ptr out)
const
699 if (close::status::reserved(code)) {
700 return make_error_code(error::reserved_close_code);
703 if (close::status::invalid(code) && code != close::status::no_status) {
704 return make_error_code(error::invalid_close_code);
707 if (code == close::status::no_status && reason.size() > 0) {
708 return make_error_code(error::reason_requires_code);
711 if (reason.size() > frame:: limits::payload_size_basic-2) {
712 return make_error_code(error::control_too_big);
717 if (code != close::status::no_status) {
718 close::code_converter val;
721 payload.resize(reason.size()+2);
723 payload[0] = val.c[0];
724 payload[1] = val.c[1];
726 std::copy(reason.begin(),reason.end(),payload.begin()+2);
729 return this->prepare_control(frame::opcode::CLOSE,payload,out);
734 key.append(constants::handshake_guid);
736 unsigned char message_digest[20];
737 sha1::calc(key.c_str(),key.length(),message_digest);
738 key = base64_encode(message_digest,20);
740 return lib::error_code();
745 if (len == 0 || m_bytes_needed == 0) {
751 if (m_bytes_needed == 2) {
752 m_basic_header.b0 = buf[0];
753 m_basic_header.b1 = buf[1];
757 m_basic_header.b1 = buf[0];
763 if (m_bytes_needed == 2) {
764 m_basic_header.b0 = buf[0];
768 m_basic_header.b1 = buf[0];
777 size_t bytes_to_read = (std::min)(m_bytes_needed,len);
779 std::copy(buf,buf+bytes_to_read,m_extended_header.bytes+m_cursor);
780 m_cursor += bytes_to_read;
781 m_bytes_needed -= bytes_to_read;
783 return bytes_to_read;
802 if (frame::get_masked(m_basic_header)) {
803 m_current_msg->prepared_key = frame::byte_mask_circ(
804 buf, len, m_current_msg->prepared_key);
808 std::string & out = m_current_msg->msg_ptr->get_raw_payload();
809 size_t offset = out.size();
812 if (m_permessage_deflate.is_enabled()
813 && m_current_msg->msg_ptr->get_compressed())
816 ec = m_permessage_deflate.decompress(buf,len,out);
822 out.append(
reinterpret_cast<
char *>(buf),len);
826 if (m_current_msg->msg_ptr->get_opcode() == frame::opcode::TEXT) {
827 if (!m_current_msg->validator.decode(out.begin()+offset,out.end())) {
828 ec = make_error_code(error::invalid_utf8);
833 m_bytes_needed -= len;
851 frame::opcode::value op = frame::get_opcode(h);
854 if (frame::opcode::is_control(op) &&
855 frame::get_basic_size(h) > frame::limits::payload_size_basic)
857 return make_error_code(error::control_too_big);
866 if (frame::get_rsv1(h) && (!m_permessage_deflate.is_enabled()
867 || frame::opcode::is_control(op)))
869 return make_error_code(error::invalid_rsv_bit);
872 if (frame::get_rsv2(h) || frame::get_rsv3(h)) {
873 return make_error_code(error::invalid_rsv_bit);
877 if (frame::opcode::reserved(op)) {
878 return make_error_code(error::invalid_opcode);
883 if (frame::opcode::invalid(op)) {
884 return make_error_code(error::invalid_opcode);
888 if (frame::opcode::is_control(op) && !frame::get_fin(h)) {
889 return make_error_code(error::fragmented_control);
893 if (new_msg && op == frame::opcode::CONTINUATION) {
894 return make_error_code(error::invalid_continuation);
898 if (!new_msg && !frame::opcode::is_control(op) &&
899 op != frame::opcode::CONTINUATION)
901 return make_error_code(error::invalid_continuation);
906 if (is_server && !frame::get_masked(h)) {
907 return make_error_code(error::masking_required);
908 }
else if (!is_server && frame::get_masked(h)) {
909 return make_error_code(error::masking_forbidden);
912 return lib::error_code();
929 uint8_t basic_size = frame::get_basic_size(h);
930 uint64_t payload_size = frame::get_payload_size(h,e);
933 if (basic_size == frame::payload_size_code_16bit &&
934 payload_size <= frame::limits::payload_size_basic)
936 return make_error_code(error::non_minimal_encoding);
939 if (basic_size == frame::payload_size_code_64bit &&
940 payload_size <= frame::limits::payload_size_extended)
942 return make_error_code(error::non_minimal_encoding);
946 if (
sizeof(size_t) == 4 && (payload_size >> 32)) {
947 return make_error_code(error::requires_64bit);
950 return lib::error_code();
962 frame::masking_key_type key)
const
964 frame::byte_mask(i.begin(),i.end(),o.begin(),key);
981 return make_error_code(error::invalid_arguments);
984 if (!frame::opcode::is_control(op)) {
985 return make_error_code(error::invalid_opcode);
988 if (payload.size() > frame::limits::payload_size_basic) {
989 return make_error_code(error::control_too_big);
992 frame::masking_key_type key;
993 bool masked = !base::m_server;
995 frame::basic_header h(op,payload.size(),
true,masked);
997 std::string & o = out->get_raw_payload();
998 o.resize(payload.size());
1004 frame::extended_header e(payload.size(),key.i);
1005 out->set_header(frame::prepare_header(h,e));
1006 this->masked_copy(payload,o,key);
1008 frame::extended_header e(payload.size());
1009 out->set_header(frame::prepare_header(h,e));
1010 std::copy(payload.begin(),payload.end(),o.begin());
1013 out->set_opcode(op);
1014 out->set_prepared(
true);
1016 return lib::error_code();
1021 HEADER_EXTENDED = 1,
1033 msg_metadata(message_ptr m, size_t p) : msg_ptr(m),prepared_key(p) {}
1034 msg_metadata(message_ptr m, frame::masking_key_type p)
1036 , prepared_key(prepare_masking_key(p)) {}
1038 message_ptr msg_ptr;
1039 size_t prepared_key;
1040 utf8_validator::validator validator;
1044 frame::basic_header m_basic_header;
1047 msg_manager_ptr m_msg_manager;
1050 size_t m_bytes_needed;
1064 frame::extended_header m_extended_header;
1072 permessage_deflate_type m_permessage_deflate;