1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mochitest/pywebsocket/mod_pywebsocket/handshake/__init__.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,116 @@ 1.4 +# Copyright 2011, Google Inc. 1.5 +# All rights reserved. 1.6 +# 1.7 +# Redistribution and use in source and binary forms, with or without 1.8 +# modification, are permitted provided that the following conditions are 1.9 +# met: 1.10 +# 1.11 +# * Redistributions of source code must retain the above copyright 1.12 +# notice, this list of conditions and the following disclaimer. 1.13 +# * Redistributions in binary form must reproduce the above 1.14 +# copyright notice, this list of conditions and the following disclaimer 1.15 +# in the documentation and/or other materials provided with the 1.16 +# distribution. 1.17 +# * Neither the name of Google Inc. nor the names of its 1.18 +# contributors may be used to endorse or promote products derived from 1.19 +# this software without specific prior written permission. 1.20 +# 1.21 +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 + 1.33 + 1.34 +"""WebSocket opening handshake processor. This class try to apply available 1.35 +opening handshake processors for each protocol version until a connection is 1.36 +successfully established. 1.37 +""" 1.38 + 1.39 + 1.40 +import logging 1.41 + 1.42 +from mod_pywebsocket import common 1.43 +from mod_pywebsocket.handshake import draft75 1.44 +from mod_pywebsocket.handshake import hybi00 1.45 +from mod_pywebsocket.handshake import hybi 1.46 +# Export AbortedByUserException, HandshakeException, and VersionException 1.47 +# symbol from this module. 1.48 +from mod_pywebsocket.handshake._base import AbortedByUserException 1.49 +from mod_pywebsocket.handshake._base import HandshakeException 1.50 +from mod_pywebsocket.handshake._base import VersionException 1.51 + 1.52 + 1.53 +_LOGGER = logging.getLogger(__name__) 1.54 + 1.55 + 1.56 +def do_handshake(request, dispatcher, allowDraft75=False, strict=False): 1.57 + """Performs WebSocket handshake. 1.58 + 1.59 + Args: 1.60 + request: mod_python request. 1.61 + dispatcher: Dispatcher (dispatch.Dispatcher). 1.62 + allowDraft75: allow draft 75 handshake protocol. 1.63 + strict: Strictly check handshake request in draft 75. 1.64 + Default: False. If True, request.connection must provide 1.65 + get_memorized_lines method. 1.66 + 1.67 + Handshaker will add attributes such as ws_resource in performing 1.68 + handshake. 1.69 + """ 1.70 + 1.71 + _LOGGER.debug('Client\'s opening handshake resource: %r', request.uri) 1.72 + # To print mimetools.Message as escaped one-line string, we converts 1.73 + # headers_in to dict object. Without conversion, if we use %r, it just 1.74 + # prints the type and address, and if we use %s, it prints the original 1.75 + # header string as multiple lines. 1.76 + # 1.77 + # Both mimetools.Message and MpTable_Type of mod_python can be 1.78 + # converted to dict. 1.79 + # 1.80 + # mimetools.Message.__str__ returns the original header string. 1.81 + # dict(mimetools.Message object) returns the map from header names to 1.82 + # header values. While MpTable_Type doesn't have such __str__ but just 1.83 + # __repr__ which formats itself as well as dictionary object. 1.84 + _LOGGER.debug( 1.85 + 'Client\'s opening handshake headers: %r', dict(request.headers_in)) 1.86 + 1.87 + handshakers = [] 1.88 + handshakers.append( 1.89 + ('RFC 6455', hybi.Handshaker(request, dispatcher))) 1.90 + handshakers.append( 1.91 + ('HyBi 00', hybi00.Handshaker(request, dispatcher))) 1.92 + if allowDraft75: 1.93 + handshakers.append( 1.94 + ('Hixie 75', draft75.Handshaker(request, dispatcher, strict))) 1.95 + 1.96 + for name, handshaker in handshakers: 1.97 + _LOGGER.debug('Trying protocol version %s', name) 1.98 + try: 1.99 + handshaker.do_handshake() 1.100 + _LOGGER.info('Established (%s protocol)', name) 1.101 + return 1.102 + except HandshakeException, e: 1.103 + _LOGGER.debug( 1.104 + 'Failed to complete opening handshake as %s protocol: %r', 1.105 + name, e) 1.106 + if e.status: 1.107 + raise e 1.108 + except AbortedByUserException, e: 1.109 + raise 1.110 + except VersionException, e: 1.111 + raise 1.112 + 1.113 + # TODO(toyoshim): Add a test to cover the case all handshakers fail. 1.114 + raise HandshakeException( 1.115 + 'Failed to complete opening handshake for all available protocols', 1.116 + status=common.HTTP_STATUS_BAD_REQUEST) 1.117 + 1.118 + 1.119 +# vi:sts=4 sw=4 et