impl_base.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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::full, 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. std::size_t
  281. read_size_hint_pmd(
  282. std::size_t initial_size,
  283. bool rd_done,
  284. std::uint64_t rd_remain,
  285. detail::frame_header const& rd_fh) const
  286. {
  287. using beast::detail::clamp;
  288. std::size_t result;
  289. BOOST_ASSERT(initial_size > 0);
  290. if(! pmd_ || (! rd_done && ! pmd_->rd_set))
  291. {
  292. // current message is uncompressed
  293. if(rd_done)
  294. {
  295. // first message frame
  296. result = initial_size;
  297. goto done;
  298. }
  299. else if(rd_fh.fin)
  300. {
  301. // last message frame
  302. BOOST_ASSERT(rd_remain > 0);
  303. result = clamp(rd_remain);
  304. goto done;
  305. }
  306. }
  307. result = (std::max)(
  308. initial_size, clamp(rd_remain));
  309. done:
  310. BOOST_ASSERT(result != 0);
  311. return result;
  312. }
  313. };
  314. //------------------------------------------------------------------------------
  315. template<>
  316. struct impl_base<false>
  317. {
  318. // These stubs are for avoiding linking in the zlib
  319. // code when permessage-deflate is not enabled.
  320. bool
  321. rd_deflated() const
  322. {
  323. return false;
  324. }
  325. bool
  326. rd_deflated(bool rsv1)
  327. {
  328. return ! rsv1;
  329. }
  330. template<class ConstBufferSequence>
  331. bool
  332. deflate(
  333. net::mutable_buffer&,
  334. buffers_suffix<ConstBufferSequence>&,
  335. bool,
  336. std::size_t&,
  337. error_code&)
  338. {
  339. return false;
  340. }
  341. void
  342. do_context_takeover_write(role_type)
  343. {
  344. }
  345. void
  346. inflate(
  347. zlib::z_params&,
  348. zlib::Flush,
  349. error_code&)
  350. {
  351. }
  352. void
  353. do_context_takeover_read(role_type)
  354. {
  355. }
  356. template<class Body, class Allocator>
  357. void
  358. build_response_pmd(
  359. http::response<http::string_body>&,
  360. http::request<Body,
  361. http::basic_fields<Allocator>> const&);
  362. void
  363. on_response_pmd(
  364. http::response<http::string_body> const&)
  365. {
  366. }
  367. template<class Allocator>
  368. void
  369. do_pmd_config(http::basic_fields<Allocator> const&)
  370. {
  371. }
  372. void
  373. set_option_pmd(permessage_deflate const& o)
  374. {
  375. if(o.client_enable || o.server_enable)
  376. {
  377. // Can't enable permessage-deflate
  378. // when deflateSupported == false.
  379. //
  380. BOOST_THROW_EXCEPTION(std::invalid_argument{
  381. "deflateSupported == false"});
  382. }
  383. }
  384. void
  385. get_option_pmd(permessage_deflate& o)
  386. {
  387. o = {};
  388. o.client_enable = false;
  389. o.server_enable = false;
  390. }
  391. void
  392. build_request_pmd(
  393. http::request<http::empty_body>&)
  394. {
  395. }
  396. void open_pmd(role_type)
  397. {
  398. }
  399. void close_pmd()
  400. {
  401. }
  402. bool pmd_enabled() const
  403. {
  404. return false;
  405. }
  406. std::size_t
  407. read_size_hint_pmd(
  408. std::size_t initial_size,
  409. bool rd_done,
  410. std::uint64_t rd_remain,
  411. frame_header const& rd_fh) const
  412. {
  413. using beast::detail::clamp;
  414. std::size_t result;
  415. BOOST_ASSERT(initial_size > 0);
  416. // compression is not supported
  417. if(rd_done)
  418. {
  419. // first message frame
  420. result = initial_size;
  421. }
  422. else if(rd_fh.fin)
  423. {
  424. // last message frame
  425. BOOST_ASSERT(rd_remain > 0);
  426. result = clamp(rd_remain);
  427. }
  428. else
  429. {
  430. result = (std::max)(
  431. initial_size, clamp(rd_remain));
  432. }
  433. BOOST_ASSERT(result != 0);
  434. return result;
  435. }
  436. };
  437. } // detail
  438. } // websocket
  439. } // beast
  440. } // boost
  441. #endif