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 import zlib | |
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 | |
36 | |
37 | |
38 _GOODBYE_MESSAGE = u'Goodbye' | |
39 _ENABLE_MESSAGE = u'EnableCompression' | |
40 _DISABLE_MESSAGE = u'DisableCompression' | |
41 _bfinal = False | |
42 _client_max_window_bits = 15 | |
43 | |
44 | |
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 | |
50 return None | |
51 | |
52 | |
53 def web_socket_do_extra_handshake(request): | |
54 global _bfinal | |
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] | |
60 if not processor: | |
61 return | |
62 r = request.ws_resource.split('?', 1) | |
63 if len(r) == 1: | |
64 return | |
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: | |
73 _bfinal = True | |
74 | |
75 | |
76 def receive(request): | |
77 stream = request.ws_stream | |
78 possibly_compressed_body = b'' | |
79 compress = False | |
80 while True: | |
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) | |
85 return (False, None) | |
86 compress = compress or frame.rsv1 | |
87 possibly_compressed_body += frame.payload | |
88 if frame.fin: | |
89 break | |
90 if compress: | |
91 return (compress, possibly_compressed_body + b'\x00\x00\xff\xff') | |
92 else: | |
93 return (compress, possibly_compressed_body) | |
94 | |
95 | |
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) | |
100 while True: | |
101 compress, possibly_compressed_body = receive(request) | |
102 body = None | |
103 if possibly_compressed_body is None: | |
104 return | |
105 if compress: | |
106 inflater.append(possibly_compressed_body) | |
107 body = inflater.decompress(-1) | |
108 else: | |
109 body = possibly_compressed_body | |
110 | |
111 text = body.decode('utf-8') | |
112 if processor: | |
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: | |
119 return | |
120 | |
121 | |
122 # vi:sts=4 sw=4 et | |
OLD | NEW |