OLD | NEW |
| (Empty) |
1 # Copyright 2013, Google Inc. All rights reserved. | |
2 # | |
3 # Redistribution and use in source and binary forms, with or without | |
4 # modification, are permitted provided that the following conditions are | |
5 # met: | |
6 # | |
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 | |
12 # distribution. | |
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. | |
16 # | |
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. | |
28 | |
29 | |
30 import urlparse | |
31 from mod_pywebsocket.extensions import PerMessageDeflateExtensionProcessor | |
32 from mod_pywebsocket.extensions import ExtensionProcessorInterface | |
33 from mod_pywebsocket.common import ExtensionParameter | |
34 | |
35 | |
36 _GOODBYE_MESSAGE = u'Goodbye' | |
37 _ENABLE_MESSAGE = u'EnableCompression' | |
38 _DISABLE_MESSAGE = u'DisableCompression' | |
39 _bfinal = False | |
40 | |
41 | |
42 def _get_permessage_deflate_extension_processor(request): | |
43 for extension_processor in request.ws_extension_processors: | |
44 if isinstance(extension_processor, PerMessageDeflateExtensionProcessor): | |
45 return extension_processor | |
46 return None | |
47 | |
48 | |
49 def web_socket_do_extra_handshake(request): | |
50 global _bfinal | |
51 processor = _get_permessage_deflate_extension_processor(request) | |
52 # Remove extension processors other than PerMessageDeflateProcessor | |
53 # to avoid conflict. | |
54 request.ws_extension_processors = [processor] | |
55 if not processor: | |
56 return | |
57 r = request.ws_resource.split('?', 1) | |
58 if len(r) == 1: | |
59 return | |
60 parameters = urlparse.parse_qs(r[1], keep_blank_values=True) | |
61 if 'client_max_window_bits' in parameters: | |
62 window_bits = int(parameters['client_max_window_bits'][0]) | |
63 processor.set_client_max_window_bits(window_bits) | |
64 if 'client_no_context_takeover' in parameters: | |
65 processor.set_client_no_context_takeover(True) | |
66 if 'set_bfinal' in parameters: | |
67 _bfinal = True | |
68 | |
69 | |
70 def web_socket_transfer_data(request): | |
71 processor = _get_permessage_deflate_extension_processor(request) | |
72 processor.set_bfinal(_bfinal) | |
73 while True: | |
74 line = request.ws_stream.receive_message() | |
75 if line is None: | |
76 return | |
77 if isinstance(line, unicode): | |
78 if processor: | |
79 if line == _ENABLE_MESSAGE: | |
80 processor.enable_outgoing_compression() | |
81 elif line == _DISABLE_MESSAGE: | |
82 processor.disable_outgoing_compression() | |
83 request.ws_stream.send_message(line, binary=False) | |
84 if line == _GOODBYE_MESSAGE: | |
85 return | |
86 else: | |
87 request.ws_stream.send_message(line, binary=True) | |
88 | |
89 | |
90 # vi:sts=4 sw=4 et | |
OLD | NEW |