__init__.py (4186B)
1 # Copyright 2011, Google Inc. 2 # All rights reserved. 3 # 4 # Redistribution and use in source and binary forms, with or without 5 # modification, are permitted provided that the following conditions are 6 # met: 7 # 8 # * Redistributions of source code must retain the above copyright 9 # notice, this list of conditions and the following disclaimer. 10 # * Redistributions in binary form must reproduce the above 11 # copyright notice, this list of conditions and the following disclaimer 12 # in the documentation and/or other materials provided with the 13 # distribution. 14 # * Neither the name of Google Inc. nor the names of its 15 # contributors may be used to endorse or promote products derived from 16 # this software without specific prior written permission. 17 # 18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 """WebSocket opening handshake processor. This class try to apply available 30 opening handshake processors for each protocol version until a connection is 31 successfully established. 32 """ 33 34 from __future__ import absolute_import 35 import logging 36 37 from mod_pywebsocket import common 38 from mod_pywebsocket.handshake import hybi 39 # Export AbortedByUserException, HandshakeException, and VersionException 40 # symbol from this module. 41 from mod_pywebsocket.handshake.base import AbortedByUserException 42 from mod_pywebsocket.handshake.base import HandshakeException 43 from mod_pywebsocket.handshake.base import VersionException 44 45 _LOGGER = logging.getLogger(__name__) 46 47 48 def do_handshake(request, dispatcher): 49 """Performs WebSocket handshake. 50 51 Args: 52 request: mod_python request. 53 dispatcher: Dispatcher (dispatch.Dispatcher). 54 55 Handshaker will add attributes such as ws_resource in performing 56 handshake. 57 """ 58 59 _LOGGER.debug('Client\'s opening handshake resource: %r', request.uri) 60 # To print mimetools.Message as escaped one-line string, we converts 61 # headers_in to dict object. Without conversion, if we use %r, it just 62 # prints the type and address, and if we use %s, it prints the original 63 # header string as multiple lines. 64 # 65 # Both mimetools.Message and MpTable_Type of mod_python can be 66 # converted to dict. 67 # 68 # mimetools.Message.__str__ returns the original header string. 69 # dict(mimetools.Message object) returns the map from header names to 70 # header values. While MpTable_Type doesn't have such __str__ but just 71 # __repr__ which formats itself as well as dictionary object. 72 _LOGGER.debug('Client\'s opening handshake headers: %r', 73 dict(request.headers_in)) 74 75 handshakers = [] 76 handshakers.append(('RFC 6455', hybi.Handshaker(request, dispatcher))) 77 78 for name, handshaker in handshakers: 79 _LOGGER.debug('Trying protocol version %s', name) 80 try: 81 handshaker.do_handshake() 82 _LOGGER.info('Established (%s protocol)', name) 83 return 84 except HandshakeException as e: 85 _LOGGER.debug( 86 'Failed to complete opening handshake as %s protocol: %r', 87 name, e) 88 if e.status: 89 raise e 90 except AbortedByUserException as e: 91 raise 92 except VersionException as e: 93 raise 94 95 # TODO(toyoshim): Add a test to cover the case all handshakers fail. 96 raise HandshakeException( 97 'Failed to complete opening handshake for all available protocols', 98 status=common.HTTP_STATUS_BAD_REQUEST) 99 100 101 # vi:sts=4 sw=4 et