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 30 31 """WebSocket opening handshake processor. This class try to apply available 32 opening handshake processors for each protocol version until a connection is 33 successfully established. 34 """ 35 36 37 import logging 38 39 from mod_pywebsocket import common 40 from mod_pywebsocket.handshake import hybi00 41 from mod_pywebsocket.handshake import hybi 42 # Export AbortedByUserException, HandshakeException, and VersionException 43 # symbol from this module. 44 from mod_pywebsocket.handshake._base import AbortedByUserException 45 from mod_pywebsocket.handshake._base import HandshakeException 46 from mod_pywebsocket.handshake._base import VersionException 47 48 49 _LOGGER = logging.getLogger(__name__) 50 51 52 def do_handshake(request, dispatcher, allowDraft75=False, strict=False): 53 """Performs WebSocket handshake. 54 55 Args: 56 request: mod_python request. 57 dispatcher: Dispatcher (dispatch.Dispatcher). 58 allowDraft75: obsolete argument. ignored. 59 strict: obsolete argument. ignored. 60 61 Handshaker will add attributes such as ws_resource in performing 62 handshake. 63 """ 64 65 _LOGGER.debug('Client\'s opening handshake resource: %r', request.uri) 66 # To print mimetools.Message as escaped one-line string, we converts 67 # headers_in to dict object. Without conversion, if we use %r, it just 68 # prints the type and address, and if we use %s, it prints the original 69 # header string as multiple lines. 70 # 71 # Both mimetools.Message and MpTable_Type of mod_python can be 72 # converted to dict. 73 # 74 # mimetools.Message.__str__ returns the original header string. 75 # dict(mimetools.Message object) returns the map from header names to 76 # header values. While MpTable_Type doesn't have such __str__ but just 77 # __repr__ which formats itself as well as dictionary object. 78 _LOGGER.debug( 79 'Client\'s opening handshake headers: %r', dict(request.headers_in)) 80 81 handshakers = [] 82 handshakers.append( 83 ('RFC 6455', hybi.Handshaker(request, dispatcher))) 84 handshakers.append( 85 ('HyBi 00', hybi00.Handshaker(request, dispatcher))) 86 87 for name, handshaker in handshakers: 88 _LOGGER.debug('Trying protocol version %s', name) 89 try: 90 handshaker.do_handshake() 91 _LOGGER.info('Established (%s protocol)', name) 92 return 93 except HandshakeException, e: 94 _LOGGER.debug( 95 'Failed to complete opening handshake as %s protocol: %r', 96 name, e) 97 if e.status: 98 raise e 99 except AbortedByUserException, e: 100 raise 101 except VersionException, e: 102 raise 103 104 # TODO(toyoshim): Add a test to cover the case all handshakers fail. 105 raise HandshakeException( 106 'Failed to complete opening handshake for all available protocols', 107 status=common.HTTP_STATUS_BAD_REQUEST) 108 109 110 # vi:sts=4 sw=4 et 111