28 #ifndef WEBSOCKETPP_PROCESSOR_EXTENSION_PERMESSAGEDEFLATE_HPP 29 #define WEBSOCKETPP_PROCESSOR_EXTENSION_PERMESSAGEDEFLATE_HPP 48 namespace extensions {
88 namespace permessage_deflate {
124 return "websocketpp.extension.permessage-deflate";
130 return "Generic permessage-compress error";
132 return "Invalid extension attributes";
134 return "Invalid extension attribute value";
136 return "Invalid permessage-deflate negotiation mode";
138 return "Unsupported extension attributes";
140 return "Invalid value for max_window_bits";
142 return "A zlib function returned an error";
144 return "Deflate extension must be initialized before use";
146 return "Unknown permessage-compress error";
159 return lib::error_code(static_cast<int>(e),
get_category());
168 template<>
struct is_error_code_enum
175 namespace extensions {
176 namespace permessage_deflate {
217 template <
typename config>
222 , m_server_no_context_takeover(false)
223 , m_client_no_context_takeover(false)
224 , m_server_max_window_bits(15)
225 , m_client_max_window_bits(15)
226 , m_server_max_window_bits_mode(mode::
accept)
227 , m_client_max_window_bits_mode(mode::
accept)
228 , m_initialized(false)
229 , m_compress_buffer_size(8192)
231 m_dstate.zalloc = Z_NULL;
232 m_dstate.zfree = Z_NULL;
233 m_dstate.opaque = Z_NULL;
235 m_istate.zalloc = Z_NULL;
236 m_istate.zfree = Z_NULL;
237 m_istate.opaque = Z_NULL;
238 m_istate.avail_in = 0;
239 m_istate.next_in = Z_NULL;
243 if (!m_initialized) {
247 int ret = deflateEnd(&m_dstate);
254 ret = inflateEnd(&m_istate);
273 lib::error_code
init(
bool is_server) {
274 uint8_t deflate_bits;
275 uint8_t inflate_bits;
278 deflate_bits = m_server_max_window_bits;
279 inflate_bits = m_client_max_window_bits;
281 deflate_bits = m_client_max_window_bits;
282 inflate_bits = m_server_max_window_bits;
285 int ret = deflateInit2(
287 Z_DEFAULT_COMPRESSION,
307 m_compress_buffer.reset(
new unsigned char[m_compress_buffer_size]);
308 m_decompress_buffer.reset(
new unsigned char[m_compress_buffer_size]);
309 if ((m_server_no_context_takeover && is_server) ||
310 (m_client_no_context_takeover && !is_server))
312 m_flush = Z_FULL_FLUSH;
314 m_flush = Z_SYNC_FLUSH;
316 m_initialized =
true;
317 return lib::error_code();
363 m_server_no_context_takeover =
true;
382 m_client_no_context_takeover =
true;
416 if (bits < min_server_max_window_bits || bits > max_server_max_window_bits) {
425 m_server_max_window_bits = bits;
426 m_server_max_window_bits_mode = mode;
428 return lib::error_code();
461 if (bits < min_client_max_window_bits || bits > max_client_max_window_bits) {
470 m_client_max_window_bits = bits;
471 m_client_max_window_bits_mode = mode;
473 return lib::error_code();
485 return "permessage-deflate; client_no_context_takeover; client_max_window_bits";
497 return lib::error_code();
512 http::attribute_list::const_iterator it;
513 for (it = offer.begin(); it != offer.end(); ++it) {
514 if (it->first ==
"server_no_context_takeover") {
515 negotiate_server_no_context_takeover(it->second,ret.first);
516 }
else if (it->first ==
"client_no_context_takeover") {
517 negotiate_client_no_context_takeover(it->second,ret.first);
518 }
else if (it->first ==
"server_max_window_bits") {
519 negotiate_server_max_window_bits(it->second,ret.first);
520 }
else if (it->first ==
"client_max_window_bits") {
521 negotiate_client_max_window_bits(it->second,ret.first);
531 if (ret.first == lib::error_code()) {
533 ret.second = generate_response();
548 lib::error_code
compress(std::string
const & in, std::string & out) {
549 if (!m_initialized) {
556 uint8_t buf[6] = {0x02, 0x00, 0x00, 0x00, 0xff, 0xff};
557 out.append((
char *)(buf),6);
558 return lib::error_code();
561 m_dstate.avail_in = in.size();
562 m_dstate.next_in = (
unsigned char *)(const_cast<char *>(in.data()));
566 m_dstate.avail_out = m_compress_buffer_size;
567 m_dstate.next_out = m_compress_buffer.get();
569 deflate(&m_dstate, m_flush);
571 output = m_compress_buffer_size - m_dstate.avail_out;
573 out.append((
char *)(m_compress_buffer.get()),output);
574 }
while (m_dstate.avail_out == 0);
576 return lib::error_code();
586 lib::error_code
decompress(uint8_t
const * buf,
size_t len, std::string &
589 if (!m_initialized) {
595 m_istate.avail_in = len;
596 m_istate.next_in =
const_cast<unsigned char *
>(buf);
599 m_istate.avail_out = m_compress_buffer_size;
600 m_istate.next_out = m_decompress_buffer.get();
602 ret = inflate(&m_istate, Z_SYNC_FLUSH);
604 if (ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) {
609 reinterpret_cast<char *>(m_decompress_buffer.get()),
610 m_compress_buffer_size - m_istate.avail_out
612 }
while (m_istate.avail_out == 0);
614 return lib::error_code();
621 std::string generate_response() {
622 std::string ret =
"permessage-deflate";
624 if (m_server_no_context_takeover) {
625 ret +=
"; server_no_context_takeover";
628 if (m_client_no_context_takeover) {
629 ret +=
"; client_no_context_takeover";
632 if (m_server_max_window_bits < default_server_max_window_bits) {
634 s << int(m_server_max_window_bits);
635 ret +=
"; server_max_window_bits="+s.str();
638 if (m_client_max_window_bits < default_client_max_window_bits) {
640 s << int(m_client_max_window_bits);
641 ret +=
"; client_max_window_bits="+s.str();
652 void negotiate_server_no_context_takeover(std::string
const &
value,
653 lib::error_code & ec)
655 if (!value.empty()) {
660 m_server_no_context_takeover =
true;
668 void negotiate_client_no_context_takeover(std::string
const & value,
669 lib::error_code & ec)
671 if (!value.empty()) {
676 m_client_no_context_takeover =
true;
701 void negotiate_server_max_window_bits(std::string
const & value,
702 lib::error_code & ec)
704 uint8_t bits = uint8_t(atoi(value.c_str()));
706 if (bits < min_server_max_window_bits || bits > max_server_max_window_bits) {
712 switch (m_server_max_window_bits_mode) {
717 m_server_max_window_bits = bits;
720 m_server_max_window_bits = std::min(bits,m_server_max_window_bits);
731 if (m_server_max_window_bits == 8) {
732 m_server_max_window_bits = 9;
757 void negotiate_client_max_window_bits(std::string
const & value,
758 lib::error_code & ec)
760 uint8_t bits = uint8_t(atoi(value.c_str()));
764 }
else if (bits < min_client_max_window_bits ||
765 bits > max_client_max_window_bits)
772 switch (m_client_max_window_bits_mode) {
777 m_client_max_window_bits = bits;
780 m_client_max_window_bits = std::min(bits,m_client_max_window_bits);
791 if (m_client_max_window_bits == 8) {
792 m_client_max_window_bits = 9;
797 bool m_server_no_context_takeover;
798 bool m_client_no_context_takeover;
799 uint8_t m_server_max_window_bits;
800 uint8_t m_client_max_window_bits;
806 size_t m_compress_buffer_size;
817 #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.
void enable_client_no_context_takeover()
Reset client's outgoing LZ77 sliding window for each new message.
static uint8_t const default_server_max_window_bits
Default value for server_max_window_bits as defined by RFC 7692.
Decline any value the remote endpoint offers. Insist on defaults.
lib::error_category const & get_category()
Get a reference to a static copy of the permessage-deflate error category.
Accept any value the remote endpoint offers.
std::pair< lib::error_code, std::string > err_str_pair
Combination error code / string type for returning two values.
#define _WEBSOCKETPP_NOEXCEPT_TOKEN_
Invalid value for max_window_bits.
Invalid megotiation mode.
Invalid extension attribute value.
char const * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_
lib::error_code init(bool is_server)
Initialize zlib state.
Use the smallest value common to both offers.
lib::error_code set_client_max_window_bits(uint8_t bits, mode::value mode)
Limit client LZ77 sliding window size.
lib::error_code validate_offer(http::attribute_list const &)
Validate extension response.
lib::error_code decompress(uint8_t const *buf, size_t len, std::string &out)
Decompress bytes.
Use the largest value common to both offers.
bool is_implemented() const
Test if this object implements the permessage-deflate specification.
lib::error_code make_error_code(error::value e)
Create an error code in the permessage-deflate category.
static uint8_t const min_client_max_window_bits
Minimum value for client_max_window_bits as defined by RFC 7692.
bool is_enabled() const
Test if the extension was negotiated for this connection.
Namespace for the WebSocket++ project.
static uint8_t const max_server_max_window_bits
Maximum value for server_max_window_bits as defined by RFC 7692.
#define _WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_
err_str_pair negotiate(http::attribute_list const &offer)
Negotiate extension.
std::map< std::string, std::string > attribute_list
The type of an HTTP attribute list.
Unsupported extension attributes.
void enable_server_no_context_takeover()
Reset server's outgoing LZ77 sliding window for each new message.
std::string generate_offer() const
Generate extension offer.
static uint8_t const min_server_max_window_bits
Minimum value for server_max_window_bits as defined by RFC 7692.
lib::error_code compress(std::string const &in, std::string &out)
Compress bytes.
#define _WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
static uint8_t const max_client_max_window_bits
Maximum value for client_max_window_bits as defined by RFC 7692.
Invalid extension attributes.
boost::scoped_array< unsigned char > unique_ptr_uchar_array
static uint8_t const default_client_max_window_bits
Default value for client_max_window_bits as defined by RFC 7692.
Permessage-deflate error category.
std::string message(int value) const