| OLD | NEW |
| (Empty) |
| 1 # Copyright 2012, 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 DeflateFrameExtensionProcessor | |
| 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 | |
| 40 | |
| 41 def _get_deflate_frame_extension_processor(request): | |
| 42 for extension_processor in request.ws_extension_processors: | |
| 43 if isinstance(extension_processor, DeflateFrameExtensionProcessor): | |
| 44 return extension_processor | |
| 45 return None | |
| 46 | |
| 47 | |
| 48 def web_socket_do_extra_handshake(request): | |
| 49 processor = _get_deflate_frame_extension_processor(request) | |
| 50 if not processor: | |
| 51 return | |
| 52 # Avoid extension conflict. | |
| 53 request.ws_extension_processors = [processor] | |
| 54 | |
| 55 r = request.ws_resource.split('?', 1) | |
| 56 if len(r) == 1: | |
| 57 return | |
| 58 parameters = urlparse.parse_qs(r[1], keep_blank_values=True) | |
| 59 if 'max_window_bits' in parameters: | |
| 60 window_bits = int(parameters['max_window_bits'][0]) | |
| 61 processor.set_response_window_bits(window_bits) | |
| 62 if 'no_context_takeover' in parameters: | |
| 63 processor.set_response_no_context_takeover(True) | |
| 64 if 'set_bfinal' in parameters: | |
| 65 processor.set_bfinal(True) | |
| 66 | |
| 67 | |
| 68 def web_socket_transfer_data(request): | |
| 69 processor = _get_deflate_frame_extension_processor(request) | |
| 70 while True: | |
| 71 line = request.ws_stream.receive_message() | |
| 72 if line is None: | |
| 73 return | |
| 74 if isinstance(line, unicode): | |
| 75 if processor: | |
| 76 if line == _ENABLE_MESSAGE: | |
| 77 processor.enable_outgoing_compression() | |
| 78 elif line == _DISABLE_MESSAGE: | |
| 79 processor.disable_outgoing_compression() | |
| 80 request.ws_stream.send_message(line, binary=False) | |
| 81 if line == _GOODBYE_MESSAGE: | |
| 82 return | |
| 83 else: | |
| 84 request.ws_stream.send_message(line, binary=True) | |
| 85 | |
| 86 | |
| 87 # vi:sts=4 sw=4 et | |
| OLD | NEW |