WebSocket++  0.7.0
C++ websocket client/server library
enabled.hpp
1 /*
2  * Copyright (c) 2015, 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_PROCESSOR_EXTENSION_PERMESSAGEDEFLATE_HPP
29 #define WEBSOCKETPP_PROCESSOR_EXTENSION_PERMESSAGEDEFLATE_HPP
30 
31 
32 #include <websocketpp/common/cpp11.hpp>
33 #include <websocketpp/common/memory.hpp>
34 #include <websocketpp/common/platforms.hpp>
35 #include <websocketpp/common/system_error.hpp>
36 #include <websocketpp/error.hpp>
37 
38 #include <websocketpp/extensions/extension.hpp>
39 
40 #include "zlib.h"
41 
42 #include <algorithm>
43 #include <string>
44 #include <vector>
45 
46 namespace websocketpp {
47 namespace extensions {
48 
49 /// Implementation of RFC 7692, the permessage-deflate WebSocket extension
50 /**
51  * ### permessage-deflate interface
52  *
53  * **init**\n
54  * `lib::error_code init(bool is_server)`\n
55  * Performs initialization
56  *
57  * **is_implimented**\n
58  * `bool is_implimented()`\n
59  * Returns whether or not the object impliments the extension or not
60  *
61  * **is_enabled**\n
62  * `bool is_enabled()`\n
63  * Returns whether or not the extension was negotiated for the current
64  * connection
65  *
66  * **generate_offer**\n
67  * `std::string generate_offer() const`\n
68  * Create an extension offer string based on local policy
69  *
70  * **validate_response**\n
71  * `lib::error_code validate_response(http::attribute_list const & response)`\n
72  * Negotiate the parameters of extension use
73  *
74  * **negotiate**\n
75  * `err_str_pair negotiate(http::attribute_list const & attributes)`\n
76  * Negotiate the parameters of extension use
77  *
78  * **compress**\n
79  * `lib::error_code compress(std::string const & in, std::string & out)`\n
80  * Compress the bytes in `in` and append them to `out`
81  *
82  * **decompress**\n
83  * `lib::error_code decompress(uint8_t const * buf, size_t len, std::string &
84  * out)`\n
85  * Decompress `len` bytes from `buf` and append them to string `out`
86  */
87 namespace permessage_deflate {
88 
89 /// Permessage deflate error values
90 namespace error {
91 enum value {
92  /// Catch all
93  general = 1,
94 
95  /// Invalid extension attributes
97 
98  /// Invalid extension attribute value
100 
101  /// Invalid megotiation mode
103 
104  /// Unsupported extension attributes
106 
107  /// Invalid value for max_window_bits
109 
110  /// ZLib Error
112 
113  /// Uninitialized
115 };
116 
117 /// Permessage-deflate error category
118 class category : public lib::error_category {
119 public:
120  category() {}
121 
122  char const * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_ {
123  return "websocketpp.extension.permessage-deflate";
124  }
125 
126  std::string message(int value) const {
127  switch(value) {
128  case general:
129  return "Generic permessage-compress error";
130  case invalid_attributes:
131  return "Invalid extension attributes";
132  case invalid_attribute_value:
133  return "Invalid extension attribute value";
134  case invalid_mode:
135  return "Invalid permessage-deflate negotiation mode";
136  case unsupported_attributes:
137  return "Unsupported extension attributes";
138  case invalid_max_window_bits:
139  return "Invalid value for max_window_bits";
140  case zlib_error:
141  return "A zlib function returned an error";
142  case uninitialized:
143  return "Deflate extension must be initialized before use";
144  default:
145  return "Unknown permessage-compress error";
146  }
147  }
148 };
149 
150 /// Get a reference to a static copy of the permessage-deflate error category
151 inline lib::error_category const & get_category() {
152  static category instance;
153  return instance;
154 }
155 
156 /// Create an error code in the permessage-deflate category
158  return lib::error_code(static_cast<int>(e), get_category());
159 }
160 
161 } // namespace error
162 } // namespace permessage_deflate
163 } // namespace extensions
164 } // namespace websocketpp
165 
166 _WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_
167 template<> struct is_error_code_enum
168  <websocketpp::extensions::permessage_deflate::error::value>
169 {
170  static bool const value = true;
171 };
172 _WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
173 namespace websocketpp {
174 namespace extensions {
175 namespace permessage_deflate {
176 
177 /// Default value for server_max_window_bits as defined by RFC 7692
179 /// Minimum value for server_max_window_bits as defined by RFC 7692
180 /**
181  * NOTE: A value of 8 is not actually supported by zlib, the deflate
182  * library that WebSocket++ uses. To preserve backwards compatibility
183  * with RFC 7692 and previous versions of the library a value of 8
184  * is accepted by the library but will always be negotiated as 9.
185  */
187 /// Maximum value for server_max_window_bits as defined by RFC 7692
189 
190 /// Default value for client_max_window_bits as defined by RFC 7692
192 /// Minimum value for client_max_window_bits as defined by RFC 7692
193 /**
194  * NOTE: A value of 8 is not actually supported by zlib, the deflate
195  * library that WebSocket++ uses. To preserve backwards compatibility
196  * with RFC 7692 and previous versions of the library a value of 8
197  * is accepted by the library but will always be negotiated as 9.
198  */
200 /// Maximum value for client_max_window_bits as defined by RFC 7692
202 
203 namespace mode {
204 enum value {
205  /// Accept any value the remote endpoint offers
206  accept = 1,
207  /// Decline any value the remote endpoint offers. Insist on defaults.
208  decline,
209  /// Use the largest value common to both offers
210  largest,
211  /// Use the smallest value common to both offers
212  smallest
213 };
214 } // namespace mode
215 
216 template <typename config>
217 class enabled {
218 public:
219  enabled()
220  : m_enabled(false)
221  , m_server_no_context_takeover(false)
222  , m_client_no_context_takeover(false)
223  , m_server_max_window_bits(15)
224  , m_client_max_window_bits(15)
225  , m_server_max_window_bits_mode(mode::accept)
226  , m_client_max_window_bits_mode(mode::accept)
227  , m_initialized(false)
228  , m_compress_buffer_size(16384)
229  {
230  m_dstate.zalloc = Z_NULL;
231  m_dstate.zfree = Z_NULL;
232  m_dstate.opaque = Z_NULL;
233 
234  m_istate.zalloc = Z_NULL;
235  m_istate.zfree = Z_NULL;
236  m_istate.opaque = Z_NULL;
237  m_istate.avail_in = 0;
238  m_istate.next_in = Z_NULL;
239  }
240 
241  ~enabled() {
242  if (!m_initialized) {
243  return;
244  }
245 
246  int ret = deflateEnd(&m_dstate);
247 
248  if (ret != Z_OK) {
249  //std::cout << "error cleaning up zlib compression state"
250  // << std::endl;
251  }
252 
253  ret = inflateEnd(&m_istate);
254 
255  if (ret != Z_OK) {
256  //std::cout << "error cleaning up zlib decompression state"
257  // << std::endl;
258  }
259  }
260 
261  /// Initialize zlib state
262  /**
263  * Note: this should be called *after* the negotiation methods. It will use
264  * information from the negotiation to determine how to initialize the zlib
265  * data structures.
266  *
267  * @todo memory level, strategy, etc are hardcoded
268  *
269  * @param is_server True to initialize as a server, false for a client.
270  * @return A code representing the error that occurred, if any
271  */
273  uint8_t deflate_bits;
274  uint8_t inflate_bits;
275 
276  if (is_server) {
277  deflate_bits = m_server_max_window_bits;
278  inflate_bits = m_client_max_window_bits;
279  } else {
280  deflate_bits = m_client_max_window_bits;
281  inflate_bits = m_server_max_window_bits;
282  }
283 
284  int ret = deflateInit2(
285  &m_dstate,
286  Z_DEFAULT_COMPRESSION,
287  Z_DEFLATED,
288  -1*deflate_bits,
289  4, // memory level 1-9
290  Z_DEFAULT_STRATEGY
291  );
292 
293  if (ret != Z_OK) {
295  }
296 
297  ret = inflateInit2(
298  &m_istate,
299  -1*inflate_bits
300  );
301 
302  if (ret != Z_OK) {
304  }
305 
306  m_compress_buffer.reset(new unsigned char[m_compress_buffer_size]);
307  if ((m_server_no_context_takeover && is_server) ||
308  (m_client_no_context_takeover && !is_server))
309  {
310  m_flush = Z_FULL_FLUSH;
311  } else {
312  m_flush = Z_SYNC_FLUSH;
313  }
314  m_initialized = true;
315  return lib::error_code();
316  }
317 
318  /// Test if this object implements the permessage-deflate specification
319  /**
320  * Because this object does implieent it, it will always return true.
321  *
322  * @return Whether or not this object implements permessage-deflate
323  */
324  bool is_implemented() const {
325  return true;
326  }
327 
328  /// Test if the extension was negotiated for this connection
329  /**
330  * Retrieves whether or not this extension is in use based on the initial
331  * handshake extension negotiations.
332  *
333  * @return Whether or not the extension is in use
334  */
335  bool is_enabled() const {
336  return m_enabled;
337  }
338 
339  /// Reset server's outgoing LZ77 sliding window for each new message
340  /**
341  * Enabling this setting will cause the server's compressor to reset the
342  * compression state (the LZ77 sliding window) for every message. This
343  * means that the compressor will not look back to patterns in previous
344  * messages to improve compression. This will reduce the compression
345  * efficiency for large messages somewhat and small messages drastically.
346  *
347  * This option may reduce server compressor memory usage and client
348  * decompressor memory usage.
349  * @todo Document to what extent memory usage will be reduced
350  *
351  * For clients, this option is dependent on server support. Enabling it
352  * via this method does not guarantee that it will be successfully
353  * negotiated, only that it will be requested.
354  *
355  * For servers, no client support is required. Enabling this option on a
356  * server will result in its use. The server will signal to clients that
357  * the option will be in use so they can optimize resource usage if they
358  * are able.
359  */
361  m_server_no_context_takeover = true;
362  }
363 
364  /// Reset client's outgoing LZ77 sliding window for each new message
365  /**
366  * Enabling this setting will cause the client's compressor to reset the
367  * compression state (the LZ77 sliding window) for every message. This
368  * means that the compressor will not look back to patterns in previous
369  * messages to improve compression. This will reduce the compression
370  * efficiency for large messages somewhat and small messages drastically.
371  *
372  * This option may reduce client compressor memory usage and server
373  * decompressor memory usage.
374  * @todo Document to what extent memory usage will be reduced
375  *
376  * This option is supported by all compliant clients and servers. Enabling
377  * it via either endpoint should be sufficient to ensure it is used.
378  */
380  m_client_no_context_takeover = true;
381  }
382 
383  /// Limit server LZ77 sliding window size
384  /**
385  * The bits setting is the base 2 logarithm of the maximum window size that
386  * the server must use to compress outgoing messages. The permitted range
387  * is 9 to 15 inclusive. 9 represents a 512 byte window and 15 a 32KiB
388  * window. The default setting is 15.
389  *
390  * Mode Options:
391  * - accept: Accept whatever the remote endpoint offers.
392  * - decline: Decline any offers to deviate from the defaults
393  * - largest: Accept largest window size acceptable to both endpoints
394  * - smallest: Accept smallest window size acceptiable to both endpoints
395  *
396  * This setting is dependent on server support. A client requesting this
397  * setting may be rejected by the server or have the exact value used
398  * adjusted by the server. A server may unilaterally set this value without
399  * client support.
400  *
401  * NOTE: The permessage-deflate spec specifies that a value of 8 is allowed.
402  * Prior to version 0.8.0 a value of 8 was also allowed by this library.
403  * zlib, the deflate compression library that WebSocket++ uses has always
404  * silently adjusted a value of 8 to 9. In recent versions of zlib (1.2.9
405  * and greater) a value of 8 is now explicitly rejected. WebSocket++ 0.8.0
406  * continues to perform the 8->9 conversion for backwards compatibility
407  * purposes but this should be considered deprecated functionality.
408  *
409  * @param bits The size to request for the outgoing window size
410  * @param mode The mode to use for negotiating this parameter
411  * @return A status code
412  */
414  if (bits < min_server_max_window_bits || bits > max_server_max_window_bits) {
415  return error::make_error_code(error::invalid_max_window_bits);
416  }
417 
418  // See note in doc comment above about what is happening here
419  if (bits == 8) {
420  bits = 9;
421  }
422 
423  m_server_max_window_bits = bits;
424  m_server_max_window_bits_mode = mode;
425 
426  return lib::error_code();
427  }
428 
429  /// Limit client LZ77 sliding window size
430  /**
431  * The bits setting is the base 2 logarithm of the window size that the
432  * client must use to compress outgoing messages. The permitted range is 9
433  * to 15 inclusive. 9 represents a 512 byte window and 15 a 32KiB window.
434  * The default setting is 15.
435  *
436  * Mode Options:
437  * - accept: Accept whatever the remote endpoint offers.
438  * - decline: Decline any offers to deviate from the defaults
439  * - largest: Accept largest window size acceptable to both endpoints
440  * - smallest: Accept smallest window size acceptiable to both endpoints
441  *
442  * This setting is dependent on client support. A client may limit its own
443  * outgoing window size unilaterally. A server may only limit the client's
444  * window size if the remote client supports that feature.
445  *
446  * NOTE: The permessage-deflate spec specifies that a value of 8 is allowed.
447  * Prior to version 0.8.0 a value of 8 was also allowed by this library.
448  * zlib, the deflate compression library that WebSocket++ uses has always
449  * silently adjusted a value of 8 to 9. In recent versions of zlib (1.2.9
450  * and greater) a value of 8 is now explicitly rejected. WebSocket++ 0.8.0
451  * continues to perform the 8->9 conversion for backwards compatibility
452  * purposes but this should be considered deprecated functionality.
453  *
454  * @param bits The size to request for the outgoing window size
455  * @param mode The mode to use for negotiating this parameter
456  * @return A status code
457  */
459  if (bits < min_client_max_window_bits || bits > max_client_max_window_bits) {
460  return error::make_error_code(error::invalid_max_window_bits);
461  }
462 
463  // See note in doc comment above about what is happening here
464  if (bits == 8) {
465  bits = 9;
466  }
467 
468  m_client_max_window_bits = bits;
469  m_client_max_window_bits_mode = mode;
470 
471  return lib::error_code();
472  }
473 
474  /// Generate extension offer
475  /**
476  * Creates an offer string to include in the Sec-WebSocket-Extensions
477  * header of outgoing client requests.
478  *
479  * @return A WebSocket extension offer string for this extension
480  */
481  std::string generate_offer() const {
482  // TODO: this should be dynamically generated based on user settings
483  return "permessage-deflate; client_no_context_takeover; client_max_window_bits";
484  }
485 
486  /// Validate extension response
487  /**
488  * Confirm that the server has negotiated settings compatible with our
489  * original offer and apply those settings to the extension state.
490  *
491  * @param response The server response attribute list to validate
492  * @return Validation error or 0 on success
493  */
495  return lib::error_code();
496  }
497 
498  /// Negotiate extension
499  /**
500  * Confirm that the client's extension negotiation offer has settings
501  * compatible with local policy. If so, generate a reply and apply those
502  * settings to the extension state.
503  *
504  * @param offer Attribute from client's offer
505  * @return Status code and value to return to remote endpoint
506  */
508  err_str_pair ret;
509 
510  http::attribute_list::const_iterator it;
511  for (it = offer.begin(); it != offer.end(); ++it) {
512  if (it->first == "server_no_context_takeover") {
513  negotiate_server_no_context_takeover(it->second,ret.first);
514  } else if (it->first == "client_no_context_takeover") {
515  negotiate_client_no_context_takeover(it->second,ret.first);
516  } else if (it->first == "server_max_window_bits") {
517  negotiate_server_max_window_bits(it->second,ret.first);
518  } else if (it->first == "client_max_window_bits") {
519  negotiate_client_max_window_bits(it->second,ret.first);
520  } else {
521  ret.first = make_error_code(error::invalid_attributes);
522  }
523 
524  if (ret.first) {
525  break;
526  }
527  }
528 
529  if (ret.first == lib::error_code()) {
530  m_enabled = true;
531  ret.second = generate_response();
532  }
533 
534  return ret;
535  }
536 
537  /// Compress bytes
538  /**
539  * @todo: avail_in/out is 32 bit, need to fix for cases of >32 bit frames
540  * on 64 bit machines.
541  *
542  * @param [in] in String to compress
543  * @param [out] out String to append compressed bytes to
544  * @return Error or status code
545  */
547  if (!m_initialized) {
549  }
550 
551  size_t output;
552 
553  if (in.empty()) {
554  uint8_t buf[6] = {0x02, 0x00, 0x00, 0x00, 0xff, 0xff};
555  out.append((char *)(buf),6);
556  return lib::error_code();
557  }
558 
559  m_dstate.avail_in = in.size();
560  m_dstate.next_in = (unsigned char *)(const_cast<char *>(in.data()));
561 
562  do {
563  // Output to local buffer
564  m_dstate.avail_out = m_compress_buffer_size;
565  m_dstate.next_out = m_compress_buffer.get();
566 
567  deflate(&m_dstate, m_flush);
568 
569  output = m_compress_buffer_size - m_dstate.avail_out;
570 
571  out.append((char *)(m_compress_buffer.get()),output);
572  } while (m_dstate.avail_out == 0);
573 
574  return lib::error_code();
575  }
576 
577  /// Decompress bytes
578  /**
579  * @param buf Byte buffer to decompress
580  * @param len Length of buf
581  * @param out String to append decompressed bytes to
582  * @return Error or status code
583  */
585  out)
586  {
587  if (!m_initialized) {
589  }
590 
591  int ret;
592 
593  m_istate.avail_in = len;
594  m_istate.next_in = const_cast<unsigned char *>(buf);
595 
596  do {
597  m_istate.avail_out = m_compress_buffer_size;
598  m_istate.next_out = m_compress_buffer.get();
599 
600  ret = inflate(&m_istate, Z_SYNC_FLUSH);
601 
602  if (ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) {
604  }
605 
606  out.append(
607  reinterpret_cast<char *>(m_compress_buffer.get()),
608  m_compress_buffer_size - m_istate.avail_out
609  );
610  } while (m_istate.avail_out == 0);
611 
612  return lib::error_code();
613  }
614 private:
615  /// Generate negotiation response
616  /**
617  * @return Generate extension negotiation reponse string to send to client
618  */
619  std::string generate_response() {
620  std::string ret = "permessage-deflate";
621 
622  if (m_server_no_context_takeover) {
623  ret += "; server_no_context_takeover";
624  }
625 
626  if (m_client_no_context_takeover) {
627  ret += "; client_no_context_takeover";
628  }
629 
630  if (m_server_max_window_bits < default_server_max_window_bits) {
631  std::stringstream s;
632  s << int(m_server_max_window_bits);
633  ret += "; server_max_window_bits="+s.str();
634  }
635 
636  if (m_client_max_window_bits < default_client_max_window_bits) {
637  std::stringstream s;
638  s << int(m_client_max_window_bits);
639  ret += "; client_max_window_bits="+s.str();
640  }
641 
642  return ret;
643  }
644 
645  /// Negotiate server_no_context_takeover attribute
646  /**
647  * @param [in] value The value of the attribute from the offer
648  * @param [out] ec A reference to the error code to return errors via
649  */
650  void negotiate_server_no_context_takeover(std::string const & value,
651  lib::error_code & ec)
652  {
653  if (!value.empty()) {
654  ec = make_error_code(error::invalid_attribute_value);
655  return;
656  }
657 
658  m_server_no_context_takeover = true;
659  }
660 
661  /// Negotiate client_no_context_takeover attribute
662  /**
663  * @param [in] value The value of the attribute from the offer
664  * @param [out] ec A reference to the error code to return errors via
665  */
666  void negotiate_client_no_context_takeover(std::string const & value,
667  lib::error_code & ec)
668  {
669  if (!value.empty()) {
670  ec = make_error_code(error::invalid_attribute_value);
671  return;
672  }
673 
674  m_client_no_context_takeover = true;
675  }
676 
677  /// Negotiate server_max_window_bits attribute
678  /**
679  * When this method starts, m_server_max_window_bits will contain the server's
680  * preferred value and m_server_max_window_bits_mode will contain the mode the
681  * server wants to use to for negotiation. `value` contains the value the
682  * client requested that we use.
683  *
684  * options:
685  * - decline (ignore value, offer our default instead)
686  * - accept (use the value requested by the client)
687  * - largest (use largest value acceptable to both)
688  * - smallest (use smallest possible value)
689  *
690  * NOTE: As a value of 8 is no longer explicitly supported by zlib but might
691  * be requested for negotiation by an older client/server, if the result of
692  * the negotiation would be to send a value of 8, a value of 9 is offered
693  * instead. This ensures that WebSocket++ will only ever negotiate connections
694  * with compression settings explicitly supported by zlib.
695  *
696  * @param [in] value The value of the attribute from the offer
697  * @param [out] ec A reference to the error code to return errors via
698  */
699  void negotiate_server_max_window_bits(std::string const & value,
700  lib::error_code & ec)
701  {
702  uint8_t bits = uint8_t(atoi(value.c_str()));
703 
704  if (bits < min_server_max_window_bits || bits > max_server_max_window_bits) {
705  ec = make_error_code(error::invalid_attribute_value);
706  m_server_max_window_bits = default_server_max_window_bits;
707  return;
708  }
709 
710  switch (m_server_max_window_bits_mode) {
711  case mode::decline:
712  m_server_max_window_bits = default_server_max_window_bits;
713  break;
714  case mode::accept:
715  m_server_max_window_bits = bits;
716  break;
717  case mode::largest:
718  m_server_max_window_bits = std::min(bits,m_server_max_window_bits);
719  break;
720  case mode::smallest:
721  m_server_max_window_bits = min_server_max_window_bits;
722  break;
723  default:
724  ec = make_error_code(error::invalid_mode);
725  m_server_max_window_bits = default_server_max_window_bits;
726  }
727 
728  // See note in doc comment
729  if (m_server_max_window_bits == 8) {
730  m_server_max_window_bits = 9;
731  }
732  }
733 
734  /// Negotiate client_max_window_bits attribute
735  /**
736  * When this method starts, m_client_max_window_bits and m_c2s_max_window_mode
737  * will contain the server's preferred values for window size and
738  * negotiation mode.
739  *
740  * options:
741  * - decline (ignore value, offer our default instead)
742  * - accept (use the value requested by the client)
743  * - largest (use largest value acceptable to both)
744  * - smallest (use smallest possible value)
745  *
746  * NOTE: As a value of 8 is no longer explicitly supported by zlib but might
747  * be requested for negotiation by an older client/server, if the result of
748  * the negotiation would be to send a value of 8, a value of 9 is offered
749  * instead. This ensures that WebSocket++ will only ever negotiate connections
750  * with compression settings explicitly supported by zlib.
751  *
752  * @param [in] value The value of the attribute from the offer
753  * @param [out] ec A reference to the error code to return errors via
754  */
755  void negotiate_client_max_window_bits(std::string const & value,
756  lib::error_code & ec)
757  {
758  uint8_t bits = uint8_t(atoi(value.c_str()));
759 
760  if (value.empty()) {
761  bits = default_client_max_window_bits;
762  } else if (bits < min_client_max_window_bits ||
763  bits > max_client_max_window_bits)
764  {
765  ec = make_error_code(error::invalid_attribute_value);
766  m_client_max_window_bits = default_client_max_window_bits;
767  return;
768  }
769 
770  switch (m_client_max_window_bits_mode) {
771  case mode::decline:
772  m_client_max_window_bits = default_client_max_window_bits;
773  break;
774  case mode::accept:
775  m_client_max_window_bits = bits;
776  break;
777  case mode::largest:
778  m_client_max_window_bits = std::min(bits,m_client_max_window_bits);
779  break;
780  case mode::smallest:
781  m_client_max_window_bits = min_client_max_window_bits;
782  break;
783  default:
784  ec = make_error_code(error::invalid_mode);
785  m_client_max_window_bits = default_client_max_window_bits;
786  }
787 
788  // See note in doc comment
789  if (m_client_max_window_bits == 8) {
790  m_client_max_window_bits = 9;
791  }
792  }
793 
794  bool m_enabled;
795  bool m_server_no_context_takeover;
796  bool m_client_no_context_takeover;
797  uint8_t m_server_max_window_bits;
798  uint8_t m_client_max_window_bits;
799  mode::value m_server_max_window_bits_mode;
800  mode::value m_client_max_window_bits_mode;
801 
802  bool m_initialized;
803  int m_flush;
804  size_t m_compress_buffer_size;
805  lib::unique_ptr_uchar_array m_compress_buffer;
806  z_stream m_dstate;
807  z_stream m_istate;
808 };
809 
810 } // namespace permessage_deflate
811 } // namespace extensions
812 } // namespace websocketpp
813 
814 #endif // WEBSOCKETPP_PROCESSOR_EXTENSION_PERMESSAGEDEFLATE_HPP
lib::error_code set_server_max_window_bits(uint8_t bits, mode::value mode)
Limit server LZ77 sliding window size.
Definition: enabled.hpp:413
void enable_client_no_context_takeover()
Reset client&#39;s outgoing LZ77 sliding window for each new message.
Definition: enabled.hpp:379
lib::error_category const & get_category()
Get a reference to a static copy of the permessage-deflate error category.
Definition: enabled.hpp:151
lib::error_code init(bool is_server)
Initialize zlib state.
Definition: enabled.hpp:272
lib::error_code set_client_max_window_bits(uint8_t bits, mode::value mode)
Limit client LZ77 sliding window size.
Definition: enabled.hpp:458
lib::error_code validate_offer(http::attribute_list const &)
Validate extension response.
Definition: enabled.hpp:494
lib::error_code decompress(uint8_t const *buf, size_t len, std::string &out)
Decompress bytes.
Definition: enabled.hpp:584
bool is_implemented() const
Test if this object implements the permessage-deflate specification.
Definition: enabled.hpp:324
lib::error_code make_error_code(error::value e)
Create an error code in the permessage-deflate category.
Definition: enabled.hpp:157
bool is_enabled() const
Test if the extension was negotiated for this connection.
Definition: enabled.hpp:335
err_str_pair negotiate(http::attribute_list const &offer)
Negotiate extension.
Definition: enabled.hpp:507
void handle_accept(connection_ptr con, lib::error_code const &ec)
Handler callback for start_accept.
void enable_server_no_context_takeover()
Reset server&#39;s outgoing LZ77 sliding window for each new message.
Definition: enabled.hpp:360
std::string generate_offer() const
Generate extension offer.
Definition: enabled.hpp:481
lib::error_code compress(std::string const &in, std::string &out)
Compress bytes.
Definition: enabled.hpp:546
static uint8_t const max_client_max_window_bits
Maximum value for client_max_window_bits as defined by RFC 7692.
Definition: enabled.hpp:201