1 # Copyright 2013, Google Inc. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
13 # * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 from mod_pywebsocket
import common
, util
33 from mod_pywebsocket
.extensions
import PerMessageDeflateExtensionProcessor
34 from mod_pywebsocket
.extensions
import ExtensionProcessorInterface
35 from mod_pywebsocket
.common
import ExtensionParameter
38 _GOODBYE_MESSAGE
= u
'Goodbye'
39 _ENABLE_MESSAGE
= u
'EnableCompression'
40 _DISABLE_MESSAGE
= u
'DisableCompression'
42 _client_max_window_bits
= 15
45 def _get_permessage_deflate_extension_processor(request
):
46 for extension_processor
in request
.ws_extension_processors
:
47 if isinstance(extension_processor
,
48 PerMessageDeflateExtensionProcessor
):
49 return extension_processor
53 def web_socket_do_extra_handshake(request
):
55 global _client_max_window_bits
56 processor
= _get_permessage_deflate_extension_processor(request
)
57 # Remove extension processors other than
58 # PerMessageDeflateExtensionProcessor to avoid conflict.
59 request
.ws_extension_processors
= [processor
]
62 r
= request
.ws_resource
.split('?', 1)
65 parameters
= urlparse
.parse_qs(r
[1], keep_blank_values
=True)
66 if 'client_max_window_bits' in parameters
:
67 window_bits
= int(parameters
['client_max_window_bits'][0])
68 processor
.set_client_max_window_bits(window_bits
)
69 _client_max_window_bits
= window_bits
70 if 'client_no_context_takeover' in parameters
:
71 processor
.set_client_no_context_takeover(True)
72 if 'set_bfinal' in parameters
:
77 stream
= request
.ws_stream
78 possibly_compressed_body
= b
''
81 frame
= stream
._receive
_frame
_as
_frame
_object
()
82 if frame
.opcode
== common
.OPCODE_CLOSE
:
83 message
= stream
._get
_message
_from
_frame
(frame
)
84 stream
._process
_close
_message
(message
)
86 compress
= compress
or frame
.rsv1
87 possibly_compressed_body
+= frame
.payload
91 return (compress
, possibly_compressed_body
+ b
'\x00\x00\xff\xff')
93 return (compress
, possibly_compressed_body
)
96 def web_socket_transfer_data(request
):
97 processor
= _get_permessage_deflate_extension_processor(request
)
98 processor
.set_bfinal(_bfinal
)
99 inflater
= util
._Inflater
(_client_max_window_bits
)
101 compress
, possibly_compressed_body
= receive(request
)
103 if possibly_compressed_body
is None:
106 inflater
.append(possibly_compressed_body
)
107 body
= inflater
.decompress(-1)
109 body
= possibly_compressed_body
111 text
= body
.decode('utf-8')
113 if text
== _ENABLE_MESSAGE
:
114 processor
.enable_outgoing_compression()
115 elif text
== _DISABLE_MESSAGE
:
116 processor
.disable_outgoing_compression()
117 request
.ws_stream
.send_message(text
, binary
=False)
118 if text
== _GOODBYE_MESSAGE
: