hybi13.hpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. #ifndef WEBSOCKETPP_PROCESSOR_HYBI13_HPP
  28. #define WEBSOCKETPP_PROCESSOR_HYBI13_HPP
  29. #include <websocketpp/processors/processor.hpp>
  30. #include <websocketpp/frame.hpp>
  31. #include <websocketpp/http/constants.hpp>
  32. #include <websocketpp/utf8_validator.hpp>
  33. #include <websocketpp/sha1/sha1.hpp>
  34. #include <websocketpp/base64/base64.hpp>
  35. #include <websocketpp/common/network.hpp>
  36. #include <websocketpp/common/platforms.hpp>
  37. #include <algorithm>
  38. #include <cassert>
  39. #include <string>
  40. #include <vector>
  41. #include <utility>
  42. namespace websocketpp {
  43. namespace processor {
  44. /// Processor for Hybi version 13 (RFC6455)
  45. template <typename config>
  46. class hybi13 : public processor<config> {
  47. public:
  48. typedef processor<config> base;
  49. typedef typename config::request_type request_type;
  50. typedef typename config::response_type response_type;
  51. typedef typename config::message_type message_type;
  52. typedef typename message_type::ptr message_ptr;
  53. typedef typename config::con_msg_manager_type msg_manager_type;
  54. typedef typename msg_manager_type::ptr msg_manager_ptr;
  55. typedef typename config::rng_type rng_type;
  56. typedef typename config::permessage_deflate_type permessage_deflate_type;
  57. typedef std::pair<lib::error_code,std::string> err_str_pair;
  58. explicit hybi13(bool secure, bool p_is_server, msg_manager_ptr manager, rng_type& rng)
  59. : processor<config>(secure, p_is_server)
  60. , m_msg_manager(manager)
  61. , m_rng(rng)
  62. {
  63. reset_headers();
  64. }
  65. int get_version() const {
  66. return 13;
  67. }
  68. bool has_permessage_deflate() const {
  69. return m_permessage_deflate.is_implemented();
  70. }
  71. err_str_pair negotiate_extensions(request_type const & req) {
  72. err_str_pair ret;
  73. // Respect blanket disabling of all extensions and don't even parse
  74. // the extension header
  75. if (!config::enable_extensions) {
  76. ret.first = make_error_code(error::extensions_disabled);
  77. return ret;
  78. }
  79. http::parameter_list p;
  80. bool error = req.get_header_as_plist("Sec-WebSocket-Extensions",p);
  81. if (error) {
  82. ret.first = make_error_code(error::extension_parse_error);
  83. return ret;
  84. }
  85. // If there are no extensions parsed then we are done!
  86. if (p.size() == 0) {
  87. return ret;
  88. }
  89. http::parameter_list::const_iterator it;
  90. if (m_permessage_deflate.is_implemented()) {
  91. err_str_pair neg_ret;
  92. for (it = p.begin(); it != p.end(); ++it) {
  93. // look through each extension, if the key is permessage-deflate
  94. if (it->first == "permessage-deflate") {
  95. neg_ret = m_permessage_deflate.negotiate(it->second);
  96. if (neg_ret.first) {
  97. // Figure out if this is an error that should halt all
  98. // extension negotiations or simply cause negotiation of
  99. // this specific extension to fail.
  100. //std::cout << "permessage-compress negotiation failed: "
  101. // << neg_ret.first.message() << std::endl;
  102. } else {
  103. // Note: this list will need commas if WebSocket++ ever
  104. // supports more than one extension
  105. ret.second += neg_ret.second;
  106. continue;
  107. }
  108. }
  109. }
  110. }
  111. return ret;
  112. }
  113. lib::error_code validate_handshake(request_type const & r) const {
  114. if (r.get_method() != "GET") {
  115. return make_error_code(error::invalid_http_method);
  116. }
  117. if (r.get_version() != "HTTP/1.1") {
  118. return make_error_code(error::invalid_http_version);
  119. }
  120. // required headers
  121. // Host is required by HTTP/1.1
  122. // Connection is required by is_websocket_handshake
  123. // Upgrade is required by is_websocket_handshake
  124. if (r.get_header("Sec-WebSocket-Key") == "") {
  125. return make_error_code(error::missing_required_header);
  126. }
  127. return lib::error_code();
  128. }
  129. /* TODO: the 'subprotocol' parameter may need to be expanded into a more
  130. * generic struct if other user input parameters to the processed handshake
  131. * are found.
  132. */
  133. lib::error_code process_handshake(request_type const & request,
  134. std::string const & subprotocol, response_type & response) const
  135. {
  136. std::string server_key = request.get_header("Sec-WebSocket-Key");
  137. lib::error_code ec = process_handshake_key(server_key);
  138. if (ec) {
  139. return ec;
  140. }
  141. response.replace_header("Sec-WebSocket-Accept",server_key);
  142. response.append_header("Upgrade",constants::upgrade_token);
  143. response.append_header("Connection",constants::connection_token);
  144. if (!subprotocol.empty()) {
  145. response.replace_header("Sec-WebSocket-Protocol",subprotocol);
  146. }
  147. return lib::error_code();
  148. }
  149. /// Fill in a set of request headers for a client connection request
  150. /**
  151. * @param [out] req Set of headers to fill in
  152. * @param [in] uri The uri being connected to
  153. * @param [in] subprotocols The list of subprotocols to request
  154. */
  155. lib::error_code client_handshake_request(request_type & req, uri_ptr
  156. uri, std::vector<std::string> const & subprotocols) const
  157. {
  158. req.set_method("GET");
  159. req.set_uri(uri->get_resource());
  160. req.set_version("HTTP/1.1");
  161. req.append_header("Upgrade","websocket");
  162. req.append_header("Connection","Upgrade");
  163. req.replace_header("Sec-WebSocket-Version","13");
  164. req.replace_header("Host",uri->get_host_port());
  165. if (!subprotocols.empty()) {
  166. std::ostringstream result;
  167. std::vector<std::string>::const_iterator it = subprotocols.begin();
  168. result << *it++;
  169. while (it != subprotocols.end()) {
  170. result << ", " << *it++;
  171. }
  172. req.replace_header("Sec-WebSocket-Protocol",result.str());
  173. }
  174. // Generate handshake key
  175. frame::uint32_converter conv;
  176. unsigned char raw_key[16];
  177. for (int i = 0; i < 4; i++) {
  178. conv.i = m_rng();
  179. std::copy(conv.c,conv.c+4,&raw_key[i*4]);
  180. }
  181. req.replace_header("Sec-WebSocket-Key",base64_encode(raw_key, 16));
  182. return lib::error_code();
  183. }
  184. /// Validate the server's response to an outgoing handshake request
  185. /**
  186. * @param req The original request sent
  187. * @param res The reponse to generate
  188. * @return An error code, 0 on success, non-zero for other errors
  189. */
  190. lib::error_code validate_server_handshake_response(request_type const & req,
  191. response_type& res) const
  192. {
  193. // A valid response has an HTTP 101 switching protocols code
  194. if (res.get_status_code() != http::status_code::switching_protocols) {
  195. return error::make_error_code(error::invalid_http_status);
  196. }
  197. // And the upgrade token in an upgrade header
  198. std::string const & upgrade_header = res.get_header("Upgrade");
  199. if (utility::ci_find_substr(upgrade_header, constants::upgrade_token,
  200. sizeof(constants::upgrade_token)-1) == upgrade_header.end())
  201. {
  202. return error::make_error_code(error::missing_required_header);
  203. }
  204. // And the websocket token in the connection header
  205. std::string const & con_header = res.get_header("Connection");
  206. if (utility::ci_find_substr(con_header, constants::connection_token,
  207. sizeof(constants::connection_token)-1) == con_header.end())
  208. {
  209. return error::make_error_code(error::missing_required_header);
  210. }
  211. // And has a valid Sec-WebSocket-Accept value
  212. std::string key = req.get_header("Sec-WebSocket-Key");
  213. lib::error_code ec = process_handshake_key(key);
  214. if (ec || key != res.get_header("Sec-WebSocket-Accept")) {
  215. return error::make_error_code(error::missing_required_header);
  216. }
  217. return lib::error_code();
  218. }
  219. std::string get_raw(response_type const & res) const {
  220. return res.raw();
  221. }
  222. std::string const & get_origin(request_type const & r) const {
  223. return r.get_header("Origin");
  224. }
  225. lib::error_code extract_subprotocols(request_type const & req,
  226. std::vector<std::string> & subprotocol_list)
  227. {
  228. if (!req.get_header("Sec-WebSocket-Protocol").empty()) {
  229. http::parameter_list p;
  230. if (!req.get_header_as_plist("Sec-WebSocket-Protocol",p)) {
  231. http::parameter_list::const_iterator it;
  232. for (it = p.begin(); it != p.end(); ++it) {
  233. subprotocol_list.push_back(it->first);
  234. }
  235. } else {
  236. return error::make_error_code(error::subprotocol_parse_error);
  237. }
  238. }
  239. return lib::error_code();
  240. }
  241. uri_ptr get_uri(request_type const & request) const {
  242. return get_uri_from_host(request,(base::m_secure ? "wss" : "ws"));
  243. }
  244. /// Process new websocket connection bytes
  245. /**
  246. *
  247. * Hybi 13 data streams represent a series of variable length frames. Each
  248. * frame is made up of a series of fixed length fields. The lengths of later
  249. * fields are contained in earlier fields. The first field length is fixed
  250. * by the spec.
  251. *
  252. * This processor represents a state machine that keeps track of what field
  253. * is presently being read and how many more bytes are needed to complete it
  254. *
  255. *
  256. *
  257. *
  258. * Read two header bytes
  259. * Extract full frame length.
  260. * Read extra header bytes
  261. * Validate frame header (including extension validate)
  262. * Read extension data into extension message state object
  263. * Read payload data into payload
  264. *
  265. * @param buf Input buffer
  266. *
  267. * @param len Length of input buffer
  268. *
  269. * @return Number of bytes processed or zero on error
  270. */
  271. size_t consume(uint8_t * buf, size_t len, lib::error_code & ec) {
  272. size_t p = 0;
  273. ec = lib::error_code();
  274. //std::cout << "consume: " << utility::to_hex(buf,len) << std::endl;
  275. // Loop while we don't have a message ready and we still have bytes
  276. // left to process.
  277. while (m_state != READY && m_state != FATAL_ERROR &&
  278. (p < len || m_bytes_needed == 0))
  279. {
  280. if (m_state == HEADER_BASIC) {
  281. p += this->copy_basic_header_bytes(buf+p,len-p);
  282. if (m_bytes_needed > 0) {
  283. continue;
  284. }
  285. ec = this->validate_incoming_basic_header(
  286. m_basic_header, base::m_server, !m_data_msg.msg_ptr
  287. );
  288. if (ec) {break;}
  289. // extract full header size and adjust consume state accordingly
  290. m_state = HEADER_EXTENDED;
  291. m_cursor = 0;
  292. m_bytes_needed = frame::get_header_len(m_basic_header) -
  293. frame::BASIC_HEADER_LENGTH;
  294. } else if (m_state == HEADER_EXTENDED) {
  295. p += this->copy_extended_header_bytes(buf+p,len-p);
  296. if (m_bytes_needed > 0) {
  297. continue;
  298. }
  299. ec = validate_incoming_extended_header(m_basic_header,m_extended_header);
  300. if (ec){break;}
  301. m_state = APPLICATION;
  302. m_bytes_needed = static_cast<size_t>(get_payload_size(m_basic_header,m_extended_header));
  303. // check if this frame is the start of a new message and set up
  304. // the appropriate message metadata.
  305. frame::opcode::value op = frame::get_opcode(m_basic_header);
  306. // TODO: get_message failure conditions
  307. if (frame::opcode::is_control(op)) {
  308. m_control_msg = msg_metadata(
  309. m_msg_manager->get_message(op,m_bytes_needed),
  310. frame::get_masking_key(m_basic_header,m_extended_header)
  311. );
  312. m_current_msg = &m_control_msg;
  313. } else {
  314. if (!m_data_msg.msg_ptr) {
  315. if (m_bytes_needed > base::m_max_message_size) {
  316. ec = make_error_code(error::message_too_big);
  317. break;
  318. }
  319. m_data_msg = msg_metadata(
  320. m_msg_manager->get_message(op,m_bytes_needed),
  321. frame::get_masking_key(m_basic_header,m_extended_header)
  322. );
  323. } else {
  324. // Fetch the underlying payload buffer from the data message we
  325. // are writing into.
  326. std::string & out = m_data_msg.msg_ptr->get_raw_payload();
  327. if (out.size() + m_bytes_needed > base::m_max_message_size) {
  328. ec = make_error_code(error::message_too_big);
  329. break;
  330. }
  331. // Each frame starts a new masking key. All other state
  332. // remains between frames.
  333. m_data_msg.prepared_key = prepare_masking_key(
  334. frame::get_masking_key(
  335. m_basic_header,
  336. m_extended_header
  337. )
  338. );
  339. out.reserve(out.size() + m_bytes_needed);
  340. }
  341. m_current_msg = &m_data_msg;
  342. }
  343. } else if (m_state == EXTENSION) {
  344. m_state = APPLICATION;
  345. } else if (m_state == APPLICATION) {
  346. size_t bytes_to_process = (std::min)(m_bytes_needed,len-p);
  347. if (bytes_to_process > 0) {
  348. p += this->process_payload_bytes(buf+p,bytes_to_process,ec);
  349. if (ec) {break;}
  350. }
  351. if (m_bytes_needed > 0) {
  352. continue;
  353. }
  354. // If this was the last frame in the message set the ready flag.
  355. // Otherwise, reset processor state to read additional frames.
  356. if (frame::get_fin(m_basic_header)) {
  357. // ensure that text messages end on a valid UTF8 code point
  358. if (frame::get_opcode(m_basic_header) == frame::opcode::TEXT) {
  359. if (!m_current_msg->validator.complete()) {
  360. ec = make_error_code(error::invalid_utf8);
  361. break;
  362. }
  363. }
  364. m_state = READY;
  365. } else {
  366. this->reset_headers();
  367. }
  368. } else {
  369. // shouldn't be here
  370. ec = make_error_code(error::general);
  371. return 0;
  372. }
  373. }
  374. return p;
  375. }
  376. void reset_headers() {
  377. m_state = HEADER_BASIC;
  378. m_bytes_needed = frame::BASIC_HEADER_LENGTH;
  379. m_basic_header.b0 = 0x00;
  380. m_basic_header.b1 = 0x00;
  381. std::fill_n(
  382. m_extended_header.bytes,
  383. frame::MAX_EXTENDED_HEADER_LENGTH,
  384. 0x00
  385. );
  386. }
  387. /// Test whether or not the processor has a message ready
  388. bool ready() const {
  389. return (m_state == READY);
  390. }
  391. message_ptr get_message() {
  392. if (!ready()) {
  393. return message_ptr();
  394. }
  395. message_ptr ret = m_current_msg->msg_ptr;
  396. m_current_msg->msg_ptr.reset();
  397. if (frame::opcode::is_control(ret->get_opcode())) {
  398. m_control_msg.msg_ptr.reset();
  399. } else {
  400. m_data_msg.msg_ptr.reset();
  401. }
  402. this->reset_headers();
  403. return ret;
  404. }
  405. /// Test whether or not the processor is in a fatal error state.
  406. bool get_error() const {
  407. return m_state == FATAL_ERROR;
  408. }
  409. size_t get_bytes_needed() const {
  410. return m_bytes_needed;
  411. }
  412. /// Prepare a user data message for writing
  413. /**
  414. * Performs validation, masking, compression, etc. will return an error if
  415. * there was an error, otherwise msg will be ready to be written
  416. *
  417. * By default WebSocket++ performs block masking/unmasking in a manner that
  418. * makes assumptions about the nature of the machine and STL library used.
  419. * In particular the assumption is either a 32 or 64 bit word size and an
  420. * STL with std::string::data returning a contiguous char array.
  421. *
  422. * This method improves masking performance by 3-8x depending on the ratio
  423. * of small to large messages and the availability of a 64 bit processor.
  424. *
  425. * To disable this optimization (for use with alternative STL
  426. * implementations or processors) define WEBSOCKETPP_STRICT_MASKING when
  427. * compiling the library. This will force the library to perform masking in
  428. * single byte chunks.
  429. *
  430. * TODO: tests
  431. *
  432. * @param in An unprepared message to prepare
  433. * @param out A message to be overwritten with the prepared message
  434. * @return error code
  435. */
  436. virtual lib::error_code prepare_data_frame(message_ptr in, message_ptr out)
  437. {
  438. if (!in || !out) {
  439. return make_error_code(error::invalid_arguments);
  440. }
  441. frame::opcode::value op = in->get_opcode();
  442. // validate opcode: only regular data frames
  443. if (frame::opcode::is_control(op)) {
  444. return make_error_code(error::invalid_opcode);
  445. }
  446. std::string& i = in->get_raw_payload();
  447. std::string& o = out->get_raw_payload();
  448. // validate payload utf8
  449. if (op == frame::opcode::TEXT && !utf8_validator::validate(i)) {
  450. return make_error_code(error::invalid_payload);
  451. }
  452. frame::masking_key_type key;
  453. bool masked = !base::m_server;
  454. bool compressed = m_permessage_deflate.is_enabled()
  455. && in->get_compressed();
  456. bool fin = in->get_fin();
  457. // generate header
  458. frame::basic_header h(op,i.size(),fin,masked,compressed);
  459. if (masked) {
  460. // Generate masking key.
  461. key.i = m_rng();
  462. frame::extended_header e(i.size(),key.i);
  463. out->set_header(frame::prepare_header(h,e));
  464. } else {
  465. frame::extended_header e(i.size());
  466. out->set_header(frame::prepare_header(h,e));
  467. key.i = 0;
  468. }
  469. // prepare payload
  470. if (compressed) {
  471. // compress and store in o after header.
  472. m_permessage_deflate.compress(i,o);
  473. // mask in place if necessary
  474. if (masked) {
  475. this->masked_copy(o,o,key);
  476. }
  477. } else {
  478. // no compression, just copy data into the output buffer
  479. o.resize(i.size());
  480. // if we are masked, have the masking function write to the output
  481. // buffer directly to avoid another copy. If not masked, copy
  482. // directly without masking.
  483. if (masked) {
  484. this->masked_copy(i,o,key);
  485. } else {
  486. std::copy(i.begin(),i.end(),o.begin());
  487. }
  488. }
  489. out->set_prepared(true);
  490. out->set_opcode(op);
  491. return lib::error_code();
  492. }
  493. /// Get URI
  494. lib::error_code prepare_ping(std::string const & in, message_ptr out) const {
  495. return this->prepare_control(frame::opcode::PING,in,out);
  496. }
  497. lib::error_code prepare_pong(std::string const & in, message_ptr out) const {
  498. return this->prepare_control(frame::opcode::PONG,in,out);
  499. }
  500. virtual lib::error_code prepare_close(close::status::value code,
  501. std::string const & reason, message_ptr out) const
  502. {
  503. if (close::status::reserved(code)) {
  504. return make_error_code(error::reserved_close_code);
  505. }
  506. if (close::status::invalid(code) && code != close::status::no_status) {
  507. return make_error_code(error::invalid_close_code);
  508. }
  509. if (code == close::status::no_status && reason.size() > 0) {
  510. return make_error_code(error::reason_requires_code);
  511. }
  512. if (reason.size() > frame:: limits::payload_size_basic-2) {
  513. return make_error_code(error::control_too_big);
  514. }
  515. std::string payload;
  516. if (code != close::status::no_status) {
  517. close::code_converter val;
  518. val.i = htons(code);
  519. payload.resize(reason.size()+2);
  520. payload[0] = val.c[0];
  521. payload[1] = val.c[1];
  522. std::copy(reason.begin(),reason.end(),payload.begin()+2);
  523. }
  524. return this->prepare_control(frame::opcode::CLOSE,payload,out);
  525. }
  526. protected:
  527. /// Convert a client handshake key into a server response key in place
  528. lib::error_code process_handshake_key(std::string & key) const {
  529. key.append(constants::handshake_guid);
  530. unsigned char message_digest[20];
  531. sha1::calc(key.c_str(),key.length(),message_digest);
  532. key = base64_encode(message_digest,20);
  533. return lib::error_code();
  534. }
  535. /// Reads bytes from buf into m_basic_header
  536. size_t copy_basic_header_bytes(uint8_t const * buf, size_t len) {
  537. if (len == 0 || m_bytes_needed == 0) {
  538. return 0;
  539. }
  540. if (len > 1) {
  541. // have at least two bytes
  542. if (m_bytes_needed == 2) {
  543. m_basic_header.b0 = buf[0];
  544. m_basic_header.b1 = buf[1];
  545. m_bytes_needed -= 2;
  546. return 2;
  547. } else {
  548. m_basic_header.b1 = buf[0];
  549. m_bytes_needed--;
  550. return 1;
  551. }
  552. } else {
  553. // have exactly one byte
  554. if (m_bytes_needed == 2) {
  555. m_basic_header.b0 = buf[0];
  556. m_bytes_needed--;
  557. return 1;
  558. } else {
  559. m_basic_header.b1 = buf[0];
  560. m_bytes_needed--;
  561. return 1;
  562. }
  563. }
  564. }
  565. /// Reads bytes from buf into m_extended_header
  566. size_t copy_extended_header_bytes(uint8_t const * buf, size_t len) {
  567. size_t bytes_to_read = (std::min)(m_bytes_needed,len);
  568. std::copy(buf,buf+bytes_to_read,m_extended_header.bytes+m_cursor);
  569. m_cursor += bytes_to_read;
  570. m_bytes_needed -= bytes_to_read;
  571. return bytes_to_read;
  572. }
  573. /// Reads bytes from buf into message payload
  574. /**
  575. * This function performs unmasking and uncompression, validates the
  576. * decoded bytes, and writes them to the appropriate message buffer.
  577. *
  578. * This member function will use the input buffer as stratch space for its
  579. * work. The raw input bytes will not be preserved. This applies only to the
  580. * bytes actually needed. At most min(m_bytes_needed,len) will be processed.
  581. *
  582. * @param buf Input/working buffer
  583. * @param len Length of buf
  584. * @return Number of bytes processed or zero in case of an error
  585. */
  586. size_t process_payload_bytes(uint8_t * buf, size_t len, lib::error_code& ec)
  587. {
  588. // unmask if masked
  589. if (frame::get_masked(m_basic_header)) {
  590. #ifdef WEBSOCKETPP_STRICT_MASKING
  591. m_current_msg->prepared_key = frame::byte_mask_circ(
  592. buf,
  593. len,
  594. m_current_msg->prepared_key
  595. );
  596. #else
  597. m_current_msg->prepared_key = frame::word_mask_circ(
  598. buf,
  599. len,
  600. m_current_msg->prepared_key
  601. );
  602. #endif
  603. }
  604. std::string & out = m_current_msg->msg_ptr->get_raw_payload();
  605. size_t offset = out.size();
  606. // decompress message if needed.
  607. if (m_permessage_deflate.is_enabled()
  608. && frame::get_rsv1(m_basic_header))
  609. {
  610. // Decompress current buffer into the message buffer
  611. m_permessage_deflate.decompress(buf,len,out);
  612. // get the length of the newly uncompressed output
  613. offset = out.size() - offset;
  614. } else {
  615. // No compression, straight copy
  616. out.append(reinterpret_cast<char *>(buf),len);
  617. }
  618. // validate unmasked, decompressed values
  619. if (m_current_msg->msg_ptr->get_opcode() == frame::opcode::TEXT) {
  620. if (!m_current_msg->validator.decode(out.begin()+offset,out.end())) {
  621. ec = make_error_code(error::invalid_utf8);
  622. return 0;
  623. }
  624. }
  625. m_bytes_needed -= len;
  626. return len;
  627. }
  628. /// Validate an incoming basic header
  629. /**
  630. * Validates an incoming hybi13 basic header.
  631. *
  632. * @param h The basic header to validate
  633. * @param is_server Whether or not the endpoint that received this frame
  634. * is a server.
  635. * @param new_msg Whether or not this is the first frame of the message
  636. * @return 0 on success or a non-zero error code on failure
  637. */
  638. lib::error_code validate_incoming_basic_header(frame::basic_header const & h,
  639. bool is_server, bool new_msg) const
  640. {
  641. frame::opcode::value op = frame::get_opcode(h);
  642. // Check control frame size limit
  643. if (frame::opcode::is_control(op) &&
  644. frame::get_basic_size(h) > frame::limits::payload_size_basic)
  645. {
  646. return make_error_code(error::control_too_big);
  647. }
  648. // Check that RSV bits are clear
  649. // The only RSV bits allowed are rsv1 if the permessage_compress
  650. // extension is enabled for this connection and the message is not
  651. // a control message.
  652. //
  653. // TODO: unit tests for this
  654. if (frame::get_rsv1(h) && (!m_permessage_deflate.is_enabled()
  655. || frame::opcode::is_control(op)))
  656. {
  657. return make_error_code(error::invalid_rsv_bit);
  658. }
  659. if (frame::get_rsv2(h) || frame::get_rsv3(h)) {
  660. return make_error_code(error::invalid_rsv_bit);
  661. }
  662. // Check for reserved opcodes
  663. if (frame::opcode::reserved(op)) {
  664. return make_error_code(error::invalid_opcode);
  665. }
  666. // Check for invalid opcodes
  667. // TODO: unit tests for this?
  668. if (frame::opcode::invalid(op)) {
  669. return make_error_code(error::invalid_opcode);
  670. }
  671. // Check for fragmented control message
  672. if (frame::opcode::is_control(op) && !frame::get_fin(h)) {
  673. return make_error_code(error::fragmented_control);
  674. }
  675. // Check for continuation without an active message
  676. if (new_msg && op == frame::opcode::CONTINUATION) {
  677. return make_error_code(error::invalid_continuation);
  678. }
  679. // Check for new data frame when expecting continuation
  680. if (!new_msg && !frame::opcode::is_control(op) &&
  681. op != frame::opcode::CONTINUATION)
  682. {
  683. return make_error_code(error::invalid_continuation);
  684. }
  685. // Servers should reject any unmasked frames from clients.
  686. // Clients should reject any masked frames from servers.
  687. if (is_server && !frame::get_masked(h)) {
  688. return make_error_code(error::masking_required);
  689. } else if (!is_server && frame::get_masked(h)) {
  690. return make_error_code(error::masking_forbidden);
  691. }
  692. return lib::error_code();
  693. }
  694. /// Validate an incoming extended header
  695. /**
  696. * Validates an incoming hybi13 full header.
  697. *
  698. * @todo unit test for the >32 bit frames on 32 bit systems case
  699. *
  700. * @param h The basic header to validate
  701. * @param e The extended header to validate
  702. * @return An error_code, non-zero values indicate why the validation
  703. * failed
  704. */
  705. lib::error_code validate_incoming_extended_header(frame::basic_header h,
  706. frame::extended_header e) const
  707. {
  708. uint8_t basic_size = frame::get_basic_size(h);
  709. uint64_t payload_size = frame::get_payload_size(h,e);
  710. // Check for non-minimally encoded payloads
  711. if (basic_size == frame::payload_size_code_16bit &&
  712. payload_size <= frame::limits::payload_size_basic)
  713. {
  714. return make_error_code(error::non_minimal_encoding);
  715. }
  716. if (basic_size == frame::payload_size_code_64bit &&
  717. payload_size <= frame::limits::payload_size_extended)
  718. {
  719. return make_error_code(error::non_minimal_encoding);
  720. }
  721. // Check for >32bit frames on 32 bit systems
  722. if (sizeof(size_t) == 4 && (payload_size >> 32)) {
  723. return make_error_code(error::requires_64bit);
  724. }
  725. return lib::error_code();
  726. }
  727. /// Copy and mask/unmask in one operation
  728. /**
  729. * Reads input from one string and writes unmasked output to another.
  730. *
  731. * @param [in] i The input string.
  732. * @param [out] o The output string.
  733. * @param [in] key The masking key to use for masking/unmasking
  734. */
  735. void masked_copy (std::string const & i, std::string & o,
  736. frame::masking_key_type key) const
  737. {
  738. #ifdef WEBSOCKETPP_STRICT_MASKING
  739. frame::byte_mask(i.begin(),i.end(),o.begin(),key);
  740. #else
  741. websocketpp::frame::word_mask_exact(
  742. reinterpret_cast<uint8_t *>(const_cast<char *>(i.data())),
  743. reinterpret_cast<uint8_t *>(const_cast<char *>(o.data())),
  744. i.size(),
  745. key
  746. );
  747. #endif
  748. }
  749. /// Generic prepare control frame with opcode and payload.
  750. /**
  751. * Internal control frame building method. Handles validation, masking, etc
  752. *
  753. * @param op The control opcode to use
  754. * @param payload The payload to use
  755. * @param out The message buffer to store the prepared frame in
  756. * @return Status code, zero on success, non-zero on error
  757. */
  758. lib::error_code prepare_control(frame::opcode::value op,
  759. std::string const & payload, message_ptr out) const
  760. {
  761. if (!out) {
  762. return make_error_code(error::invalid_arguments);
  763. }
  764. if (!frame::opcode::is_control(op)) {
  765. return make_error_code(error::invalid_opcode);
  766. }
  767. if (payload.size() > frame::limits::payload_size_basic) {
  768. return make_error_code(error::control_too_big);
  769. }
  770. frame::masking_key_type key;
  771. bool masked = !base::m_server;
  772. frame::basic_header h(op,payload.size(),true,masked);
  773. std::string & o = out->get_raw_payload();
  774. o.resize(payload.size());
  775. if (masked) {
  776. // Generate masking key.
  777. key.i = m_rng();
  778. frame::extended_header e(payload.size(),key.i);
  779. out->set_header(frame::prepare_header(h,e));
  780. this->masked_copy(payload,o,key);
  781. } else {
  782. frame::extended_header e(payload.size());
  783. out->set_header(frame::prepare_header(h,e));
  784. std::copy(payload.begin(),payload.end(),o.begin());
  785. }
  786. out->set_opcode(op);
  787. out->set_prepared(true);
  788. return lib::error_code();
  789. }
  790. enum state {
  791. HEADER_BASIC = 0,
  792. HEADER_EXTENDED = 1,
  793. EXTENSION = 2,
  794. APPLICATION = 3,
  795. READY = 4,
  796. FATAL_ERROR = 5
  797. };
  798. /// This data structure holds data related to processing a message, such as
  799. /// the buffer it is being written to, its masking key, its UTF8 validation
  800. /// state, and sometimes its compression state.
  801. struct msg_metadata {
  802. msg_metadata() {}
  803. msg_metadata(message_ptr m, size_t p) : msg_ptr(m),prepared_key(p) {}
  804. msg_metadata(message_ptr m, frame::masking_key_type p)
  805. : msg_ptr(m)
  806. , prepared_key(prepare_masking_key(p)) {}
  807. message_ptr msg_ptr; // pointer to the message data buffer
  808. size_t prepared_key; // prepared masking key
  809. utf8_validator::validator validator; // utf8 validation state
  810. };
  811. // Basic header of the frame being read
  812. frame::basic_header m_basic_header;
  813. // Pointer to a manager that can create message buffers for us.
  814. msg_manager_ptr m_msg_manager;
  815. // Number of bytes needed to complete the current operation
  816. size_t m_bytes_needed;
  817. // Number of extended header bytes read
  818. size_t m_cursor;
  819. // Metadata for the current data msg
  820. msg_metadata m_data_msg;
  821. // Metadata for the current control msg
  822. msg_metadata m_control_msg;
  823. // Pointer to the metadata associated with the frame being read
  824. msg_metadata * m_current_msg;
  825. // Extended header of current frame
  826. frame::extended_header m_extended_header;
  827. rng_type & m_rng;
  828. // Overall state of the processor
  829. state m_state;
  830. // Extensions
  831. permessage_deflate_type m_permessage_deflate;
  832. };
  833. } // namespace processor
  834. } // namespace websocketpp
  835. #endif //WEBSOCKETPP_PROCESSOR_HYBI13_HPP