impl_base.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_WEBSOCKET_DETAIL_IMPL_BASE_HPP
  10. #define BOOST_BEAST_WEBSOCKET_DETAIL_IMPL_BASE_HPP
  11. #include <boost/beast/websocket/option.hpp>
  12. #include <boost/beast/websocket/detail/frame.hpp>
  13. #include <boost/beast/websocket/detail/pmd_extension.hpp>
  14. #include <boost/beast/core/buffer_traits.hpp>
  15. #include <boost/beast/core/role.hpp>
  16. #include <boost/beast/http/empty_body.hpp>
  17. #include <boost/beast/http/message.hpp>
  18. #include <boost/beast/http/string_body.hpp>
  19. #include <boost/beast/zlib/deflate_stream.hpp>
  20. #include <boost/beast/zlib/inflate_stream.hpp>
  21. #include <boost/beast/core/buffers_suffix.hpp>
  22. #include <boost/beast/core/error.hpp>
  23. #include <boost/beast/core/detail/clamp.hpp>
  24. #include <boost/asio/buffer.hpp>
  25. #include <cstdint>
  26. #include <memory>
  27. #include <stdexcept>
  28. namespace boost {
  29. namespace beast {
  30. namespace websocket {
  31. namespace detail {
  32. //------------------------------------------------------------------------------
  33. template<bool deflateSupported>
  34. struct impl_base;
  35. template<>
  36. struct impl_base<true>
  37. {
  38. // State information for the permessage-deflate extension
  39. struct pmd_type
  40. {
  41. // `true` if current read message is compressed
  42. bool rd_set = false;
  43. zlib::deflate_stream zo;
  44. zlib::inflate_stream zi;
  45. };
  46. std::unique_ptr<pmd_type> pmd_; // pmd settings or nullptr
  47. permessage_deflate pmd_opts_; // local pmd options
  48. detail::pmd_offer pmd_config_; // offer (client) or negotiation (server)
  49. // return `true` if current message is deflated
  50. bool
  51. rd_deflated() const
  52. {
  53. return pmd_ && pmd_->rd_set;
  54. }
  55. // set whether current message is deflated
  56. // returns `false` on protocol violation
  57. bool
  58. rd_deflated(bool rsv1)
  59. {
  60. if(pmd_)
  61. {
  62. pmd_->rd_set = rsv1;
  63. return true;
  64. }
  65. return ! rsv1; // pmd not negotiated
  66. }
  67. // Compress a buffer sequence
  68. // Returns: `true` if more calls are needed
  69. //
  70. template<class ConstBufferSequence>
  71. bool
  72. deflate(
  73. net::mutable_buffer& out,
  74. buffers_suffix<ConstBufferSequence>& cb,
  75. bool fin,
  76. std::size_t& total_in,
  77. error_code& ec)
  78. {
  79. BOOST_ASSERT(out.size() >= 6);
  80. auto& zo = this->pmd_->zo;
  81. zlib::z_params zs;
  82. zs.avail_in = 0;
  83. zs.next_in = nullptr;
  84. zs.avail_out = out.size();
  85. zs.next_out = out.data();
  86. for(auto in : beast::buffers_range_ref(cb))
  87. {
  88. zs.avail_in = in.size();
  89. if(zs.avail_in == 0)
  90. continue;
  91. zs.next_in = in.data();
  92. zo.write(zs, zlib::Flush::none, ec);
  93. if(ec)
  94. {
  95. if(ec != zlib::error::need_buffers)
  96. return false;
  97. BOOST_ASSERT(zs.avail_out == 0);
  98. BOOST_ASSERT(zs.total_out == out.size());
  99. ec = {};
  100. break;
  101. }
  102. if(zs.avail_out == 0)
  103. {
  104. BOOST_ASSERT(zs.total_out == out.size());
  105. break;
  106. }
  107. BOOST_ASSERT(zs.avail_in == 0);
  108. }
  109. total_in = zs.total_in;
  110. cb.consume(zs.total_in);
  111. if(zs.avail_out > 0 && fin)
  112. {
  113. auto const remain = buffer_bytes(cb);
  114. if(remain == 0)
  115. {
  116. // Inspired by Mark Adler
  117. // https://github.com/madler/zlib/issues/149
  118. //
  119. // VFALCO We could do this flush twice depending
  120. // on how much space is in the output.
  121. zo.write(zs, zlib::Flush::block, ec);
  122. BOOST_ASSERT(! ec || ec == zlib::error::need_buffers);
  123. if(ec == zlib::error::need_buffers)
  124. ec = {};
  125. if(ec)
  126. return false;
  127. if(zs.avail_out >= 6)
  128. {
  129. zo.write(zs, zlib::Flush::sync, ec);
  130. BOOST_ASSERT(! ec);
  131. // remove flush marker
  132. zs.total_out -= 4;
  133. out = net::buffer(out.data(), zs.total_out);
  134. return false;
  135. }
  136. }
  137. }
  138. ec = {};
  139. out = net::buffer(out.data(), zs.total_out);
  140. return true;
  141. }
  142. void
  143. do_context_takeover_write(role_type role)
  144. {
  145. if((role == role_type::client &&
  146. this->pmd_config_.client_no_context_takeover) ||
  147. (role == role_type::server &&
  148. this->pmd_config_.server_no_context_takeover))
  149. {
  150. this->pmd_->zo.reset();
  151. }
  152. }
  153. void
  154. inflate(
  155. zlib::z_params& zs,
  156. zlib::Flush flush,
  157. error_code& ec)
  158. {
  159. pmd_->zi.write(zs, flush, ec);
  160. }
  161. void
  162. do_context_takeover_read(role_type role)
  163. {
  164. if((role == role_type::client &&
  165. pmd_config_.server_no_context_takeover) ||
  166. (role == role_type::server &&
  167. pmd_config_.client_no_context_takeover))
  168. {
  169. pmd_->zi.clear();
  170. }
  171. }
  172. template<class Body, class Allocator>
  173. void
  174. build_response_pmd(
  175. http::response<http::string_body>& res,
  176. http::request<Body,
  177. http::basic_fields<Allocator>> const& req);
  178. void
  179. on_response_pmd(
  180. http::response<http::string_body> const& res)
  181. {
  182. detail::pmd_offer offer;
  183. detail::pmd_read(offer, res);
  184. // VFALCO see if offer satisfies pmd_config_,
  185. // return an error if not.
  186. pmd_config_ = offer; // overwrite for now
  187. }
  188. template<class Allocator>
  189. void
  190. do_pmd_config(
  191. http::basic_fields<Allocator> const& h)
  192. {
  193. detail::pmd_read(pmd_config_, h);
  194. }
  195. void
  196. set_option_pmd(permessage_deflate const& o)
  197. {
  198. if( o.server_max_window_bits > 15 ||
  199. o.server_max_window_bits < 9)
  200. BOOST_THROW_EXCEPTION(std::invalid_argument{
  201. "invalid server_max_window_bits"});
  202. if( o.client_max_window_bits > 15 ||
  203. o.client_max_window_bits < 9)
  204. BOOST_THROW_EXCEPTION(std::invalid_argument{
  205. "invalid client_max_window_bits"});
  206. if( o.compLevel < 0 ||
  207. o.compLevel > 9)
  208. BOOST_THROW_EXCEPTION(std::invalid_argument{
  209. "invalid compLevel"});
  210. if( o.memLevel < 1 ||
  211. o.memLevel > 9)
  212. BOOST_THROW_EXCEPTION(std::invalid_argument{
  213. "invalid memLevel"});
  214. pmd_opts_ = o;
  215. }
  216. void
  217. get_option_pmd(permessage_deflate& o)
  218. {
  219. o = pmd_opts_;
  220. }
  221. void
  222. build_request_pmd(http::request<http::empty_body>& req)
  223. {
  224. if(pmd_opts_.client_enable)
  225. {
  226. detail::pmd_offer config;
  227. config.accept = true;
  228. config.server_max_window_bits =
  229. pmd_opts_.server_max_window_bits;
  230. config.client_max_window_bits =
  231. pmd_opts_.client_max_window_bits;
  232. config.server_no_context_takeover =
  233. pmd_opts_.server_no_context_takeover;
  234. config.client_no_context_takeover =
  235. pmd_opts_.client_no_context_takeover;
  236. detail::pmd_write(req, config);
  237. }
  238. }
  239. void
  240. open_pmd(role_type role)
  241. {
  242. if(((role == role_type::client &&
  243. pmd_opts_.client_enable) ||
  244. (role == role_type::server &&
  245. pmd_opts_.server_enable)) &&
  246. pmd_config_.accept)
  247. {
  248. detail::pmd_normalize(pmd_config_);
  249. pmd_.reset(::new pmd_type);
  250. if(role == role_type::client)
  251. {
  252. pmd_->zi.reset(
  253. pmd_config_.server_max_window_bits);
  254. pmd_->zo.reset(
  255. pmd_opts_.compLevel,
  256. pmd_config_.client_max_window_bits,
  257. pmd_opts_.memLevel,
  258. zlib::Strategy::normal);
  259. }
  260. else
  261. {
  262. pmd_->zi.reset(
  263. pmd_config_.client_max_window_bits);
  264. pmd_->zo.reset(
  265. pmd_opts_.compLevel,
  266. pmd_config_.server_max_window_bits,
  267. pmd_opts_.memLevel,
  268. zlib::Strategy::normal);
  269. }
  270. }
  271. }
  272. void close_pmd()
  273. {
  274. pmd_.reset();
  275. }
  276. bool pmd_enabled() const
  277. {
  278. return pmd_ != nullptr;
  279. }
  280. bool should_compress(std::size_t n_bytes) const
  281. {
  282. return n_bytes >= pmd_opts_.msg_size_threshold;
  283. }
  284. std::size_t
  285. read_size_hint_pmd(
  286. std::size_t initial_size,
  287. bool rd_done,
  288. std::size_t rd_msg_max,
  289. std::uint64_t rd_remain,
  290. frame_header const& rd_fh) const
  291. {
  292. using beast::detail::clamp;
  293. std::size_t result;
  294. BOOST_ASSERT(initial_size > 0);
  295. if(! pmd_ || (! rd_done && ! pmd_->rd_set))
  296. {
  297. // current message is uncompressed
  298. if(rd_done)
  299. {
  300. // first message frame
  301. result = initial_size;
  302. goto done;
  303. }
  304. else if(rd_fh.fin)
  305. {
  306. // last message frame
  307. BOOST_ASSERT(rd_remain > 0);
  308. result = clamp(rd_remain);
  309. goto done;
  310. }
  311. }
  312. result = (std::max)(
  313. initial_size, clamp(rd_remain));
  314. done:
  315. BOOST_ASSERT(result != 0);
  316. // Ensure offered size does not exceed rd_msg_max
  317. if(rd_msg_max)
  318. result = clamp(result, rd_msg_max);
  319. return result;
  320. }
  321. };
  322. //------------------------------------------------------------------------------
  323. template<>
  324. struct impl_base<false>
  325. {
  326. // These stubs are for avoiding linking in the zlib
  327. // code when permessage-deflate is not enabled.
  328. bool
  329. rd_deflated() const
  330. {
  331. return false;
  332. }
  333. bool
  334. rd_deflated(bool rsv1)
  335. {
  336. return ! rsv1;
  337. }
  338. template<class ConstBufferSequence>
  339. bool
  340. deflate(
  341. net::mutable_buffer&,
  342. buffers_suffix<ConstBufferSequence>&,
  343. bool,
  344. std::size_t&,
  345. error_code&)
  346. {
  347. return false;
  348. }
  349. void
  350. do_context_takeover_write(role_type)
  351. {
  352. }
  353. void
  354. inflate(
  355. zlib::z_params&,
  356. zlib::Flush,
  357. error_code&)
  358. {
  359. }
  360. void
  361. do_context_takeover_read(role_type)
  362. {
  363. }
  364. template<class Body, class Allocator>
  365. void
  366. build_response_pmd(
  367. http::response<http::string_body>&,
  368. http::request<Body,
  369. http::basic_fields<Allocator>> const&);
  370. void
  371. on_response_pmd(
  372. http::response<http::string_body> const&)
  373. {
  374. }
  375. template<class Allocator>
  376. void
  377. do_pmd_config(http::basic_fields<Allocator> const&)
  378. {
  379. }
  380. void
  381. set_option_pmd(permessage_deflate const& o)
  382. {
  383. if(o.client_enable || o.server_enable)
  384. {
  385. // Can't enable permessage-deflate
  386. // when deflateSupported == false.
  387. //
  388. BOOST_THROW_EXCEPTION(std::invalid_argument{
  389. "deflateSupported == false"});
  390. }
  391. }
  392. void
  393. get_option_pmd(permessage_deflate& o)
  394. {
  395. o = {};
  396. o.client_enable = false;
  397. o.server_enable = false;
  398. }
  399. void
  400. build_request_pmd(
  401. http::request<http::empty_body>&)
  402. {
  403. }
  404. void open_pmd(role_type)
  405. {
  406. }
  407. void close_pmd()
  408. {
  409. }
  410. bool pmd_enabled() const
  411. {
  412. return false;
  413. }
  414. bool should_compress(std::size_t) const
  415. {
  416. return false;
  417. }
  418. std::size_t
  419. read_size_hint_pmd(
  420. std::size_t initial_size,
  421. bool rd_done,
  422. std::size_t rd_msg_max,
  423. std::uint64_t rd_remain,
  424. frame_header const& rd_fh) const
  425. {
  426. using beast::detail::clamp;
  427. std::size_t result;
  428. BOOST_ASSERT(initial_size > 0);
  429. // compression is not supported
  430. if(rd_done)
  431. {
  432. // first message frame
  433. result = initial_size;
  434. }
  435. else if(rd_fh.fin)
  436. {
  437. // last message frame
  438. BOOST_ASSERT(rd_remain > 0);
  439. result = clamp(rd_remain);
  440. }
  441. else
  442. {
  443. result = (std::max)(
  444. initial_size, clamp(rd_remain));
  445. }
  446. BOOST_ASSERT(result != 0);
  447. // Ensure offered size does not exceed rd_msg_max
  448. if(rd_msg_max)
  449. result = clamp(result, rd_msg_max);
  450. return result;
  451. }
  452. };
  453. } // detail
  454. } // websocket
  455. } // beast
  456. } // boost
  457. #endif