This file is indexed.

/usr/lib/python2.7/dist-packages/autobahn/websocket/compress_deflate.py is in python-autobahn 17.10.1+dfsg1-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
###############################################################################
#
# The MIT License (MIT)
#
# Copyright (c) Crossbar.io Technologies GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
###############################################################################

from __future__ import absolute_import

import zlib

from autobahn.util import public
from autobahn.websocket.compress_base import PerMessageCompressOffer, \
    PerMessageCompressOfferAccept, \
    PerMessageCompressResponse, \
    PerMessageCompressResponseAccept, \
    PerMessageCompress

__all__ = (
    'PerMessageDeflateMixin',
    'PerMessageDeflateOffer',
    'PerMessageDeflateOfferAccept',
    'PerMessageDeflateResponse',
    'PerMessageDeflateResponseAccept',
    'PerMessageDeflate',
)


class PerMessageDeflateMixin(object):
    """
    Mixin class for this extension.
    """

    EXTENSION_NAME = "permessage-deflate"
    """
    Name of this WebSocket extension.
    """

    WINDOW_SIZE_PERMISSIBLE_VALUES = [8, 9, 10, 11, 12, 13, 14, 15]
    """
    Permissible value for window size parameter.
    Higher values use more memory, but produce smaller output. The default is 15.
    """

    MEM_LEVEL_PERMISSIBLE_VALUES = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    """
    Permissible value for memory level parameter.
    Higher values use more memory, but are faster and produce smaller output. The default is 8.
    """


@public
class PerMessageDeflateOffer(PerMessageCompressOffer, PerMessageDeflateMixin):
    """
    Set of extension parameters for `permessage-deflate` WebSocket extension
    offered by a client to a server.
    """

    @classmethod
    def parse(cls, params):
        """
        Parses a WebSocket extension offer for `permessage-deflate` provided by a client to a server.

        :param params: Output from :func:`autobahn.websocket.WebSocketProtocol._parseExtensionsHeader`.
        :type params: list

        :returns: A new instance of :class:`autobahn.compress.PerMessageDeflateOffer`.
        :rtype: obj
        """

        # extension parameter defaults
        accept_max_window_bits = False
        accept_no_context_takeover = True
        # accept_no_context_takeover = False # FIXME: this may change in draft
        request_max_window_bits = 0
        request_no_context_takeover = False

        # verify/parse client ("client-to-server direction") parameters of permessage-deflate offer
        for p in params:

            if len(params[p]) > 1:
                raise Exception("multiple occurrence of extension parameter '%s' for extension '%s'" % (p, cls.EXTENSION_NAME))

            val = params[p][0]

            if p == 'client_max_window_bits':
                #
                # see: https://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-18
                # 8.1.2.2. client_max_window_bits

                # ".. This parameter has no value or a decimal integer value without
                # leading zeroes between 8 to 15 inclusive ..""

                # noinspection PySimplifyBooleanCheck
                if val is not True:
                    try:
                        val = int(val)
                    except:
                        raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
                    else:
                        if val not in PerMessageDeflateMixin.WINDOW_SIZE_PERMISSIBLE_VALUES:
                            raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
                        else:
                            # FIXME (maybe): possibly forward/process the client hint!
                            # accept_max_window_bits = val
                            accept_max_window_bits = True
                else:
                    accept_max_window_bits = True

            elif p == 'client_no_context_takeover':
                # noinspection PySimplifyBooleanCheck
                if val is not True:
                    raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
                else:
                    accept_no_context_takeover = True

            elif p == 'server_max_window_bits':
                try:
                    val = int(val)
                except:
                    raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
                else:
                    if val not in PerMessageDeflateMixin.WINDOW_SIZE_PERMISSIBLE_VALUES:
                        raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
                    else:
                        request_max_window_bits = val

            elif p == 'server_no_context_takeover':
                # noinspection PySimplifyBooleanCheck
                if val is not True:
                    raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
                else:
                    request_no_context_takeover = True

            else:
                raise Exception("illegal extension parameter '%s' for extension '%s'" % (p, cls.EXTENSION_NAME))

        offer = cls(accept_no_context_takeover,
                    accept_max_window_bits,
                    request_no_context_takeover,
                    request_max_window_bits)
        return offer

    def __init__(self,
                 accept_no_context_takeover=True,
                 accept_max_window_bits=True,
                 request_no_context_takeover=False,
                 request_max_window_bits=0):
        """

        :param accept_no_context_takeover: When ``True``, the client accepts the "no context takeover" feature.
        :type accept_no_context_takeover: bool
        :param accept_max_window_bits: When ``True``, the client accepts setting "max window size".
        :type accept_max_window_bits: bool
        :param request_no_context_takeover: When ``True``, the client request the "no context takeover" feature.
        :type request_no_context_takeover: bool
        :param request_max_window_bits: When non-zero, the client requests the given "max window size" (must be
            and integer from the interval ``[8..15]``).
        :type request_max_window_bits: int
        """
        if type(accept_no_context_takeover) != bool:
            raise Exception("invalid type %s for accept_no_context_takeover" % type(accept_no_context_takeover))

        self.accept_no_context_takeover = accept_no_context_takeover

        if type(accept_max_window_bits) != bool:
            raise Exception("invalid type %s for accept_max_window_bits" % type(accept_max_window_bits))

        self.accept_max_window_bits = accept_max_window_bits

        if type(request_no_context_takeover) != bool:
            raise Exception("invalid type %s for request_no_context_takeover" % type(request_no_context_takeover))

        self.request_no_context_takeover = request_no_context_takeover

        if request_max_window_bits != 0 and request_max_window_bits not in self.WINDOW_SIZE_PERMISSIBLE_VALUES:
            raise Exception("invalid value %s for request_max_window_bits - permissible values %s" % (request_max_window_bits, self.WINDOW_SIZE_PERMISSIBLE_VALUES))

        self.request_max_window_bits = request_max_window_bits

    def get_extension_string(self):
        """
        Returns the WebSocket extension configuration string as sent to the server.

        :returns: PMCE configuration string.
        :rtype: str
        """
        pmce_string = self.EXTENSION_NAME
        if self.accept_no_context_takeover:
            pmce_string += "; client_no_context_takeover"
        if self.accept_max_window_bits:
            pmce_string += "; client_max_window_bits"
        if self.request_no_context_takeover:
            pmce_string += "; server_no_context_takeover"
        if self.request_max_window_bits != 0:
            pmce_string += "; server_max_window_bits=%d" % self.request_max_window_bits
        return pmce_string

    def __json__(self):
        """
        Returns a JSON serializable object representation.

        :returns: JSON serializable representation.
        :rtype: dict
        """
        return {'extension': self.EXTENSION_NAME,
                'accept_no_context_takeover': self.accept_no_context_takeover,
                'accept_max_window_bits': self.accept_max_window_bits,
                'request_no_context_takeover': self.request_no_context_takeover,
                'request_max_window_bits': self.request_max_window_bits}

    def __repr__(self):
        """
        Returns Python object representation that can be eval'ed to reconstruct the object.

        :returns: Python string representation.
        :rtype: str
        """
        return "PerMessageDeflateOffer(accept_no_context_takeover = %s, accept_max_window_bits = %s, request_no_context_takeover = %s, request_max_window_bits = %s)" % (self.accept_no_context_takeover, self.accept_max_window_bits, self.request_no_context_takeover, self.request_max_window_bits)


@public
class PerMessageDeflateOfferAccept(PerMessageCompressOfferAccept, PerMessageDeflateMixin):
    """
    Set of parameters with which to accept an `permessage-deflate` offer
    from a client by a server.
    """

    def __init__(self,
                 offer,
                 request_no_context_takeover=False,
                 request_max_window_bits=0,
                 no_context_takeover=None,
                 window_bits=None,
                 mem_level=None):
        """

        :param offer: The offer being accepted.
        :type offer: Instance of :class:`autobahn.compress.PerMessageDeflateOffer`.
        :param request_no_context_takeover: When ``True``, the server requests the "no context takeover" feature.
        :type request_no_context_takeover: bool
        :param request_max_window_bits: When non-zero, the server requests the given "max window size" (must be
            and integer from the interval ``[8..15]``).
        :param request_max_window_bits: int
        :param no_context_takeover: Override server ("server-to-client direction") context takeover (this must
                be compatible with the offer).
        :type no_context_takeover: bool
        :param window_bits: Override server ("server-to-client direction") window size (this must be
                compatible with the offer).
        :type window_bits: int
        :param mem_level: Set server ("server-to-client direction") memory level.
        :type mem_level: int
        """
        if not isinstance(offer, PerMessageDeflateOffer):
            raise Exception("invalid type %s for offer" % type(offer))

        self.offer = offer

        if type(request_no_context_takeover) != bool:
            raise Exception("invalid type %s for request_no_context_takeover" % type(request_no_context_takeover))

        if request_no_context_takeover and not offer.accept_no_context_takeover:
            raise Exception("invalid value %s for request_no_context_takeover - feature unsupported by client" % request_no_context_takeover)

        self.request_no_context_takeover = request_no_context_takeover

        if request_max_window_bits != 0 and request_max_window_bits not in self.WINDOW_SIZE_PERMISSIBLE_VALUES:
            raise Exception("invalid value %s for request_max_window_bits - permissible values %s" % (request_max_window_bits, self.WINDOW_SIZE_PERMISSIBLE_VALUES))

        if request_max_window_bits != 0 and not offer.accept_max_window_bits:
            raise Exception("invalid value %s for request_max_window_bits - feature unsupported by client" % request_max_window_bits)

        self.request_max_window_bits = request_max_window_bits

        if no_context_takeover is not None:
            if type(no_context_takeover) != bool:
                raise Exception("invalid type %s for no_context_takeover" % type(no_context_takeover))

            if offer.request_no_context_takeover and not no_context_takeover:
                raise Exception("invalid value %s for no_context_takeover - client requested feature" % no_context_takeover)

        self.no_context_takeover = no_context_takeover

        if window_bits is not None:
            if window_bits not in self.WINDOW_SIZE_PERMISSIBLE_VALUES:
                raise Exception("invalid value %s for window_bits - permissible values %s" % (window_bits, self.WINDOW_SIZE_PERMISSIBLE_VALUES))

            if offer.request_max_window_bits != 0 and window_bits > offer.request_max_window_bits:
                raise Exception("invalid value %s for window_bits - client requested lower maximum value" % window_bits)

        self.window_bits = window_bits

        if mem_level is not None:
            if mem_level not in self.MEM_LEVEL_PERMISSIBLE_VALUES:
                raise Exception("invalid value %s for mem_level - permissible values %s" % (mem_level, self.MEM_LEVEL_PERMISSIBLE_VALUES))

        self.mem_level = mem_level

    def get_extension_string(self):
        """
        Returns the WebSocket extension configuration string as sent to the server.

        :returns: PMCE configuration string.
        :rtype: str
        """
        pmce_string = self.EXTENSION_NAME
        if self.offer.request_no_context_takeover:
            pmce_string += "; server_no_context_takeover"
        if self.offer.request_max_window_bits != 0:
            pmce_string += "; server_max_window_bits=%d" % self.offer.request_max_window_bits
        if self.request_no_context_takeover:
            pmce_string += "; client_no_context_takeover"
        if self.request_max_window_bits != 0:
            pmce_string += "; client_max_window_bits=%d" % self.request_max_window_bits
        return pmce_string

    def __json__(self):
        """
        Returns a JSON serializable object representation.

        :returns: JSON serializable representation.
        :rtype: dict
        """
        return {'extension': self.EXTENSION_NAME,
                'offer': self.offer.__json__(),
                'request_no_context_takeover': self.request_no_context_takeover,
                'request_max_window_bits': self.request_max_window_bits,
                'no_context_takeover': self.no_context_takeover,
                'window_bits': self.window_bits,
                'mem_level': self.mem_level}

    def __repr__(self):
        """
        Returns Python object representation that can be eval'ed to reconstruct the object.

        :returns: Python string representation.
        :rtype: str
        """
        return "PerMessageDeflateOfferAccept(offer = %s, request_no_context_takeover = %s, request_max_window_bits = %s, no_context_takeover = %s, window_bits = %s, mem_level = %s)" % (self.offer.__repr__(), self.request_no_context_takeover, self.request_max_window_bits, self.no_context_takeover, self.window_bits, self.mem_level)


@public
class PerMessageDeflateResponse(PerMessageCompressResponse, PerMessageDeflateMixin):
    """
    Set of parameters for `permessage-deflate` responded by server.
    """

    @classmethod
    def parse(cls, params):
        """
        Parses a WebSocket extension response for `permessage-deflate` provided by a server to a client.

        :param params: Output from :func:`autobahn.websocket.WebSocketProtocol._parseExtensionsHeader`.
        :type params: list

        :returns: A new instance of :class:`autobahn.compress.PerMessageDeflateResponse`.
        :rtype: obj
        """
        client_max_window_bits = 0
        client_no_context_takeover = False
        server_max_window_bits = 0
        server_no_context_takeover = False

        for p in params:

            if len(params[p]) > 1:
                raise Exception("multiple occurrence of extension parameter '%s' for extension '%s'" % (p, cls.EXTENSION_NAME))

            val = params[p][0]

            if p == 'client_max_window_bits':
                try:
                    val = int(val)
                except:
                    raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
                else:
                    if val not in PerMessageDeflateMixin.WINDOW_SIZE_PERMISSIBLE_VALUES:
                        raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
                    else:
                        client_max_window_bits = val

            elif p == 'client_no_context_takeover':
                # noinspection PySimplifyBooleanCheck
                if val is not True:
                    raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
                else:
                    client_no_context_takeover = True

            elif p == 'server_max_window_bits':
                try:
                    val = int(val)
                except:
                    raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
                else:
                    if val not in PerMessageDeflateMixin.WINDOW_SIZE_PERMISSIBLE_VALUES:
                        raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
                    else:
                        server_max_window_bits = val

            elif p == 'server_no_context_takeover':
                # noinspection PySimplifyBooleanCheck
                if val is not True:
                    raise Exception("illegal extension parameter value '%s' for parameter '%s' of extension '%s'" % (val, p, cls.EXTENSION_NAME))
                else:
                    server_no_context_takeover = True

            else:
                raise Exception("illegal extension parameter '%s' for extension '%s'" % (p, cls.EXTENSION_NAME))

        response = cls(client_max_window_bits,
                       client_no_context_takeover,
                       server_max_window_bits,
                       server_no_context_takeover)
        return response

    def __init__(self,
                 client_max_window_bits,
                 client_no_context_takeover,
                 server_max_window_bits,
                 server_no_context_takeover):
        """

        :param client_max_window_bits: FIXME
        :type client_max_window_bits: int
        :param client_no_context_takeover: FIXME
        :type client_no_context_takeover: bool
        :param server_max_window_bits: FIXME
        :type server_max_window_bits: int
        :param server_no_context_takeover: FIXME
        :type server_no_context_takeover: bool
        """
        self.client_max_window_bits = client_max_window_bits
        self.client_no_context_takeover = client_no_context_takeover
        self.server_max_window_bits = server_max_window_bits
        self.server_no_context_takeover = server_no_context_takeover

    def __json__(self):
        """
        Returns a JSON serializable object representation.

        :returns: JSON serializable representation.
        :rtype: dict
        """
        return {'extension': self.EXTENSION_NAME,
                'client_max_window_bits': self.client_max_window_bits,
                'client_no_context_takeover': self.client_no_context_takeover,
                'server_max_window_bits': self.server_max_window_bits,
                'server_no_context_takeover': self.server_no_context_takeover}

    def __repr__(self):
        """
        Returns Python object representation that can be eval'ed to reconstruct the object.

        :returns: Python string representation.
        :rtype: str
        """
        return "PerMessageDeflateResponse(client_max_window_bits = %s, client_no_context_takeover = %s, server_max_window_bits = %s, server_no_context_takeover = %s)" % (self.client_max_window_bits, self.client_no_context_takeover, self.server_max_window_bits, self.server_no_context_takeover)


@public
class PerMessageDeflateResponseAccept(PerMessageCompressResponseAccept, PerMessageDeflateMixin):
    """
    Set of parameters with which to accept an `permessage-deflate` response
    from a server by a client.
    """

    def __init__(self,
                 response,
                 no_context_takeover=None,
                 window_bits=None,
                 mem_level=None):
        """

        :param response: The response being accepted.
        :type response: Instance of :class:`autobahn.compress.PerMessageDeflateResponse`.
        :param no_context_takeover: Override client ("client-to-server direction") context takeover (this must be compatible with response).
        :type no_context_takeover: bool
        :param window_bits: Override client ("client-to-server direction") window size (this must be compatible with response).
        :type window_bits: int
        :param mem_level: Set client ("client-to-server direction") memory level.
        :type mem_level: int
        """
        if not isinstance(response, PerMessageDeflateResponse):
            raise Exception("invalid type %s for response" % type(response))

        self.response = response

        if no_context_takeover is not None:
            if type(no_context_takeover) != bool:
                raise Exception("invalid type %s for no_context_takeover" % type(no_context_takeover))

            if response.client_no_context_takeover and not no_context_takeover:
                raise Exception("invalid value %s for no_context_takeover - server requested feature" % no_context_takeover)

        self.no_context_takeover = no_context_takeover

        if window_bits is not None:
            if window_bits not in self.WINDOW_SIZE_PERMISSIBLE_VALUES:
                raise Exception("invalid value %s for window_bits - permissible values %s" % (window_bits, self.WINDOW_SIZE_PERMISSIBLE_VALUES))

            if response.client_max_window_bits != 0 and window_bits > response.client_max_window_bits:
                raise Exception("invalid value %s for window_bits - server requested lower maximum value" % window_bits)

        self.window_bits = window_bits

        if mem_level is not None:
            if mem_level not in self.MEM_LEVEL_PERMISSIBLE_VALUES:
                raise Exception("invalid value %s for mem_level - permissible values %s" % (mem_level, self.MEM_LEVEL_PERMISSIBLE_VALUES))

        self.mem_level = mem_level

    def __json__(self):
        """
        Returns a JSON serializable object representation.

        :returns: JSON serializable representation.
        :rtype: dict
        """
        return {'extension': self.EXTENSION_NAME,
                'response': self.response.__json__(),
                'no_context_takeover': self.no_context_takeover,
                'window_bits': self.window_bits,
                'mem_level': self.mem_level}

    def __repr__(self):
        """
        Returns Python object representation that can be eval'ed to reconstruct the object.

        :returns: Python string representation.
        :rtype: str
        """
        return "PerMessageDeflateResponseAccept(response = %s, no_context_takeover = %s, window_bits = %s, mem_level = %s)" % (self.response.__repr__(), self.no_context_takeover, self.window_bits, self.mem_level)


# noinspection PyArgumentList
class PerMessageDeflate(PerMessageCompress, PerMessageDeflateMixin):
    """
    `permessage-deflate` WebSocket extension processor.
    """
    DEFAULT_WINDOW_BITS = zlib.MAX_WBITS
    DEFAULT_MEM_LEVEL = 8

    @classmethod
    def create_from_response_accept(cls, is_server, accept):
        # accept: instance of PerMessageDeflateResponseAccept
        pmce = cls(is_server,
                   accept.response.server_no_context_takeover,
                   accept.no_context_takeover if accept.no_context_takeover is not None else accept.response.client_no_context_takeover,
                   accept.response.server_max_window_bits,
                   accept.window_bits if accept.window_bits is not None else accept.response.client_max_window_bits,
                   accept.mem_level)
        return pmce

    @classmethod
    def create_from_offer_accept(cls, is_server, accept):
        # accept: instance of PerMessageDeflateOfferAccept
        pmce = cls(is_server,
                   accept.no_context_takeover if accept.no_context_takeover is not None else accept.offer.request_no_context_takeover,
                   accept.request_no_context_takeover,
                   accept.window_bits if accept.window_bits is not None else accept.offer.request_max_window_bits,
                   accept.request_max_window_bits,
                   accept.mem_level)
        return pmce

    def __init__(self,
                 is_server,
                 server_no_context_takeover,
                 client_no_context_takeover,
                 server_max_window_bits,
                 client_max_window_bits,
                 mem_level):
        self._is_server = is_server

        self.server_no_context_takeover = server_no_context_takeover
        self.client_no_context_takeover = client_no_context_takeover

        self.server_max_window_bits = server_max_window_bits if server_max_window_bits != 0 else self.DEFAULT_WINDOW_BITS
        self.client_max_window_bits = client_max_window_bits if client_max_window_bits != 0 else self.DEFAULT_WINDOW_BITS

        self.mem_level = mem_level if mem_level else self.DEFAULT_MEM_LEVEL

        self._compressor = None
        self._decompressor = None

    def __json__(self):
        return {'extension': self.EXTENSION_NAME,
                'is_server': self._is_server,
                'server_no_context_takeover': self.server_no_context_takeover,
                'client_no_context_takeover': self.client_no_context_takeover,
                'server_max_window_bits': self.server_max_window_bits,
                'client_max_window_bits': self.client_max_window_bits,
                'mem_level': self.mem_level}

    def __repr__(self):
        return "PerMessageDeflate(is_server = %s, server_no_context_takeover = %s, client_no_context_takeover = %s, server_max_window_bits = %s, client_max_window_bits = %s, mem_level = %s)" % (self._is_server, self.server_no_context_takeover, self.client_no_context_takeover, self.server_max_window_bits, self.client_max_window_bits, self.mem_level)

    def start_compress_message(self):
        # compressobj([level[, method[, wbits[, mem_level[, strategy]]]]])
        # http://bugs.python.org/issue19278
        # http://hg.python.org/cpython/rev/c54c8e71b79a
        if self._is_server:
            if self._compressor is None or self.server_no_context_takeover:
                self._compressor = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -self.server_max_window_bits, self.mem_level)
        else:
            if self._compressor is None or self.client_no_context_takeover:
                self._compressor = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -self.client_max_window_bits, self.mem_level)

    def compress_message_data(self, data):
        return self._compressor.compress(data)

    def end_compress_message(self):
        data = self._compressor.flush(zlib.Z_SYNC_FLUSH)
        return data[:-4]

    def start_decompress_message(self):
        if self._is_server:
            if self._decompressor is None or self.client_no_context_takeover:
                self._decompressor = zlib.decompressobj(-self.client_max_window_bits)
        else:
            if self._decompressor is None or self.server_no_context_takeover:
                self._decompressor = zlib.decompressobj(-self.server_max_window_bits)

    def decompress_message_data(self, data):
        return self._decompressor.decompress(data)

    def end_decompress_message(self):
        # Eat stripped LEN and NLEN field of a non-compressed block added
        # for Z_SYNC_FLUSH.
        self._decompressor.decompress(b'\x00\x00\xff\xff')