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