You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compress_bzip2.py 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. ###############################################################################
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) Crossbar.io Technologies GmbH
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in
  15. # all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. # THE SOFTWARE.
  24. #
  25. ###############################################################################
  26. from __future__ import absolute_import
  27. import bz2
  28. from autobahn.websocket.compress_base import PerMessageCompressOffer, \
  29. PerMessageCompressOfferAccept, \
  30. PerMessageCompressResponse, \
  31. PerMessageCompressResponseAccept, \
  32. PerMessageCompress
  33. __all__ = (
  34. 'PerMessageBzip2Mixin',
  35. 'PerMessageBzip2Offer',
  36. 'PerMessageBzip2OfferAccept',
  37. 'PerMessageBzip2Response',
  38. 'PerMessageBzip2ResponseAccept',
  39. 'PerMessageBzip2',
  40. )
  41. class PerMessageBzip2Mixin(object):
  42. """
  43. Mixin class for this extension.
  44. """
  45. EXTENSION_NAME = "permessage-bzip2"
  46. """
  47. Name of this WebSocket extension.
  48. """
  49. COMPRESS_LEVEL_PERMISSIBLE_VALUES = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  50. """
  51. Permissible value for compression level parameter.
  52. """
  53. class PerMessageBzip2Offer(PerMessageCompressOffer, PerMessageBzip2Mixin):
  54. """
  55. Set of extension parameters for `permessage-bzip2` WebSocket extension
  56. offered by a client to a server.
  57. """
  58. @classmethod
  59. def parse(cls, params):
  60. """
  61. Parses a WebSocket extension offer for `permessage-bzip2` provided by a client to a server.
  62. :param params: Output from :func:`autobahn.websocket.WebSocketProtocol._parseExtensionsHeader`.
  63. :type params: list
  64. :returns: object -- A new instance of :class:`autobahn.compress.PerMessageBzip2Offer`.
  65. """
  66. # extension parameter defaults
  67. accept_max_compress_level = False
  68. request_max_compress_level = 0
  69. # verify/parse client ("client-to-server direction") parameters of permessage-bzip2 offer
  70. for p in params:
  71. if len(params[p]) > 1:
  72. raise Exception("multiple occurrence of extension parameter '%s' for extension '%s'" % (p, cls.EXTENSION_NAME))
  73. val = params[p][0]
  74. if p == 'client_max_compress_level':
  75. # noinspection PySimplifyBooleanCheck
  76. if val is not True:
  77. raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
  78. else:
  79. accept_max_compress_level = True
  80. elif p == 'server_max_compress_level':
  81. try:
  82. val = int(val)
  83. except:
  84. raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
  85. if val not in PerMessageBzip2Mixin.COMPRESS_LEVEL_PERMISSIBLE_VALUES:
  86. raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
  87. else:
  88. request_max_compress_level = val
  89. else:
  90. raise Exception("illegal extension parameter '%s' for extension '%s'" % (p, cls.EXTENSION_NAME))
  91. offer = cls(accept_max_compress_level,
  92. request_max_compress_level)
  93. return offer
  94. def __init__(self,
  95. accept_max_compress_level=True,
  96. request_max_compress_level=0):
  97. """
  98. Constructor.
  99. :param accept_max_compress_level: Iff true, client accepts "maximum compression level" parameter.
  100. :type accept_max_compress_level: bool
  101. :param request_max_compress_level: Iff non-zero, client requests given "maximum compression level" - must be 1-9.
  102. :type request_max_compress_level: int
  103. """
  104. if type(accept_max_compress_level) != bool:
  105. raise Exception("invalid type %s for accept_max_compress_level" % type(accept_max_compress_level))
  106. self.accept_max_compress_level = accept_max_compress_level
  107. if request_max_compress_level != 0 and request_max_compress_level not in self.COMPRESS_LEVEL_PERMISSIBLE_VALUES:
  108. raise Exception("invalid value %s for request_max_compress_level - permissible values %s" % (request_max_compress_level, self.COMPRESS_LEVEL_PERMISSIBLE_VALUES))
  109. self.request_max_compress_level = request_max_compress_level
  110. def get_extension_string(self):
  111. """
  112. Returns the WebSocket extension configuration string as sent to the server.
  113. :returns: PMCE configuration string.
  114. :rtype: str
  115. """
  116. pmce_string = self.EXTENSION_NAME
  117. if self.accept_max_compress_level:
  118. pmce_string += "; client_max_compress_level"
  119. if self.request_max_compress_level != 0:
  120. pmce_string += "; server_max_compress_level=%d" % self.request_max_compress_level
  121. return pmce_string
  122. def __json__(self):
  123. """
  124. Returns a JSON serializable object representation.
  125. :returns: JSON serializable representation.
  126. :rtype: dict
  127. """
  128. return {'extension': self.EXTENSION_NAME,
  129. 'accept_max_compress_level': self.accept_max_compress_level,
  130. 'request_max_compress_level': self.request_max_compress_level}
  131. def __repr__(self):
  132. """
  133. Returns Python object representation that can be eval'ed to reconstruct the object.
  134. :returns: Python string representation.
  135. :rtype: str
  136. """
  137. return "PerMessageBzip2Offer(accept_max_compress_level = %s, request_max_compress_level = %s)" % (self.accept_max_compress_level, self.request_max_compress_level)
  138. class PerMessageBzip2OfferAccept(PerMessageCompressOfferAccept, PerMessageBzip2Mixin):
  139. """
  140. Set of parameters with which to accept an `permessage-bzip2` offer
  141. from a client by a server.
  142. """
  143. def __init__(self,
  144. offer,
  145. request_max_compress_level=0,
  146. compress_level=None):
  147. """
  148. Constructor.
  149. :param offer: The offer being accepted.
  150. :type offer: Instance of :class:`autobahn.compress.PerMessageBzip2Offer`.
  151. :param request_max_compress_level: Iff non-zero, server requests given "maximum compression level" - must be 1-9.
  152. :type request_max_compress_level: int
  153. :param compress_level: Override server ("server-to-client direction") compress level (this must be compatible with offer).
  154. :type compress_level: int
  155. """
  156. if not isinstance(offer, PerMessageBzip2Offer):
  157. raise Exception("invalid type %s for offer" % type(offer))
  158. self.offer = offer
  159. if request_max_compress_level != 0 and request_max_compress_level not in self.COMPRESS_LEVEL_PERMISSIBLE_VALUES:
  160. raise Exception("invalid value %s for request_max_compress_level - permissible values %s" % (request_max_compress_level, self.COMPRESS_LEVEL_PERMISSIBLE_VALUES))
  161. if request_max_compress_level != 0 and not offer.accept_max_compress_level:
  162. raise Exception("invalid value %s for request_max_compress_level - feature unsupported by client" % request_max_compress_level)
  163. self.request_max_compress_level = request_max_compress_level
  164. if compress_level is not None:
  165. if compress_level not in self.COMPRESS_LEVEL_PERMISSIBLE_VALUES:
  166. raise Exception("invalid value %s for compress_level - permissible values %s" % (compress_level, self.COMPRESS_LEVEL_PERMISSIBLE_VALUES))
  167. if offer.request_max_compress_level != 0 and compress_level > offer.request_max_compress_level:
  168. raise Exception("invalid value %s for compress_level - client requested lower maximum value" % compress_level)
  169. self.compress_level = compress_level
  170. def get_extension_string(self):
  171. """
  172. Returns the WebSocket extension configuration string as sent to the server.
  173. :returns: PMCE configuration string.
  174. :rtype: str
  175. """
  176. pmce_string = self.EXTENSION_NAME
  177. if self.offer.request_max_compress_level != 0:
  178. pmce_string += "; server_max_compress_level=%d" % self.offer.request_max_compress_level
  179. if self.request_max_compress_level != 0:
  180. pmce_string += "; client_max_compress_level=%d" % self.request_max_compress_level
  181. return pmce_string
  182. def __json__(self):
  183. """
  184. Returns a JSON serializable object representation.
  185. :returns: JSON serializable representation.
  186. :rtype: dict
  187. """
  188. return {'extension': self.EXTENSION_NAME,
  189. 'offer': self.offer.__json__(),
  190. 'request_max_compress_level': self.request_max_compress_level,
  191. 'compress_level': self.compress_level}
  192. def __repr__(self):
  193. """
  194. Returns Python object representation that can be eval'ed to reconstruct the object.
  195. :returns: Python string representation.
  196. :rtype: str
  197. """
  198. return "PerMessageBzip2Accept(offer = %s, request_max_compress_level = %s, compress_level = %s)" % (self.offer.__repr__(), self.request_max_compress_level, self.compress_level)
  199. class PerMessageBzip2Response(PerMessageCompressResponse, PerMessageBzip2Mixin):
  200. """
  201. Set of parameters for `permessage-bzip2` responded by server.
  202. """
  203. @classmethod
  204. def parse(cls, params):
  205. """
  206. Parses a WebSocket extension response for `permessage-bzip2` provided by a server to a client.
  207. :param params: Output from :func:`autobahn.websocket.WebSocketProtocol._parseExtensionsHeader`.
  208. :type params: list
  209. :returns: A new instance of :class:`autobahn.compress.PerMessageBzip2Response`.
  210. :rtype: obj
  211. """
  212. client_max_compress_level = 0
  213. server_max_compress_level = 0
  214. for p in params:
  215. if len(params[p]) > 1:
  216. raise Exception("multiple occurrence of extension parameter '%s' for extension '%s'" % (p, cls.EXTENSION_NAME))
  217. val = params[p][0]
  218. if p == 'client_max_compress_level':
  219. try:
  220. val = int(val)
  221. except:
  222. raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
  223. if val not in PerMessageBzip2Mixin.COMPRESS_LEVEL_PERMISSIBLE_VALUES:
  224. raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
  225. else:
  226. client_max_compress_level = val
  227. elif p == 'server_max_compress_level':
  228. try:
  229. val = int(val)
  230. except:
  231. raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
  232. if val not in PerMessageBzip2Mixin.COMPRESS_LEVEL_PERMISSIBLE_VALUES:
  233. raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
  234. else:
  235. server_max_compress_level = val
  236. else:
  237. raise Exception("illegal extension parameter '%s' for extension '%s'" % (p, cls.EXTENSION_NAME))
  238. response = cls(client_max_compress_level,
  239. server_max_compress_level)
  240. return response
  241. def __init__(self,
  242. client_max_compress_level,
  243. server_max_compress_level):
  244. self.client_max_compress_level = client_max_compress_level
  245. self.server_max_compress_level = server_max_compress_level
  246. def __json__(self):
  247. """
  248. Returns a JSON serializable object representation.
  249. :returns: JSON serializable representation.
  250. :rtype: dict
  251. """
  252. return {'extension': self.EXTENSION_NAME,
  253. 'client_max_compress_level': self.client_max_compress_level,
  254. 'server_max_compress_level': self.server_max_compress_level}
  255. def __repr__(self):
  256. """
  257. Returns Python object representation that can be eval'ed to reconstruct the object.
  258. :returns: Python string representation.
  259. :rtype: str
  260. """
  261. return "PerMessageBzip2Response(client_max_compress_level = %s, server_max_compress_level = %s)" % (self.client_max_compress_level, self.server_max_compress_level)
  262. class PerMessageBzip2ResponseAccept(PerMessageCompressResponseAccept, PerMessageBzip2Mixin):
  263. """
  264. Set of parameters with which to accept an `permessage-bzip2` response
  265. from a server by a client.
  266. """
  267. def __init__(self,
  268. response,
  269. compress_level=None):
  270. """
  271. :param response: The response being accepted.
  272. :type response: Instance of :class:`autobahn.compress.PerMessageBzip2Response`.
  273. :param compress_level: Override client ("client-to-server direction") compress level (this must be compatible with response).
  274. :type compress_level: int
  275. """
  276. if not isinstance(response, PerMessageBzip2Response):
  277. raise Exception("invalid type %s for response" % type(response))
  278. self.response = response
  279. if compress_level is not None:
  280. if compress_level not in self.COMPRESS_LEVEL_PERMISSIBLE_VALUES:
  281. raise Exception("invalid value %s for compress_level - permissible values %s" % (compress_level, self.COMPRESS_LEVEL_PERMISSIBLE_VALUES))
  282. if response.client_max_compress_level != 0 and compress_level > response.client_max_compress_level:
  283. raise Exception("invalid value %s for compress_level - server requested lower maximum value" % compress_level)
  284. self.compress_level = compress_level
  285. def __json__(self):
  286. """
  287. Returns a JSON serializable object representation.
  288. :returns: JSON serializable representation.
  289. :rtype: dict
  290. """
  291. return {'extension': self.EXTENSION_NAME,
  292. 'response': self.response.__json__(),
  293. 'compress_level': self.compress_level}
  294. def __repr__(self):
  295. """
  296. Returns Python object representation that can be eval'ed to reconstruct the object.
  297. :returns: Python string representation.
  298. :rtype: str
  299. """
  300. return "PerMessageBzip2ResponseAccept(response = %s, compress_level = %s)" % (self.response.__repr__(), self.compress_level)
  301. class PerMessageBzip2(PerMessageCompress, PerMessageBzip2Mixin):
  302. """
  303. `permessage-bzip2` WebSocket extension processor.
  304. """
  305. DEFAULT_COMPRESS_LEVEL = 9
  306. @classmethod
  307. def create_from_response_accept(cls, is_server, accept):
  308. pmce = cls(is_server,
  309. accept.response.server_max_compress_level,
  310. accept.compress_level if accept.compress_level is not None else accept.response.client_max_compress_level)
  311. return pmce
  312. @classmethod
  313. def create_from_offer_accept(cls, is_server, accept):
  314. pmce = cls(is_server,
  315. accept.compress_level if accept.compress_level is not None else accept.offer.request_max_compress_level,
  316. accept.request_max_compress_level)
  317. return pmce
  318. def __init__(self,
  319. is_server,
  320. server_max_compress_level,
  321. client_max_compress_level):
  322. self._isServer = is_server
  323. self._compressor = None
  324. self._decompressor = None
  325. self.server_max_compress_level = server_max_compress_level if server_max_compress_level != 0 else self.DEFAULT_COMPRESS_LEVEL
  326. self.client_max_compress_level = client_max_compress_level if client_max_compress_level != 0 else self.DEFAULT_COMPRESS_LEVEL
  327. def __json__(self):
  328. return {'extension': self.EXTENSION_NAME,
  329. 'isServer': self._isServer,
  330. 'server_max_compress_level': self.server_max_compress_level,
  331. 'client_max_compress_level': self.client_max_compress_level}
  332. def __repr__(self):
  333. return "PerMessageBzip2(isServer = %s, server_max_compress_level = %s, client_max_compress_level = %s)" % (self._isServer, self.server_max_compress_level, self.client_max_compress_level)
  334. def start_compress_message(self):
  335. if self._isServer:
  336. if self._compressor is None:
  337. self._compressor = bz2.BZ2Compressor(self.server_max_compress_level)
  338. else:
  339. if self._compressor is None:
  340. self._compressor = bz2.BZ2Compressor(self.client_max_compress_level)
  341. def compress_message_data(self, data):
  342. return self._compressor.compress(data)
  343. def end_compress_message(self):
  344. data = self._compressor.flush()
  345. # there seems to be no "flush without close stream", and after
  346. # full flush, compressor must not be reused
  347. self._compressor = None
  348. return data
  349. def start_decompress_message(self):
  350. if self._decompressor is None:
  351. self._decompressor = bz2.BZ2Decompressor()
  352. def decompress_message_data(self, data):
  353. return self._decompressor.decompress(data)
  354. def end_decompress_message(self):
  355. self._decompressor = None