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 | """This file provides the opening handshake processor for the WebSocket |
michael@0 | 32 | protocol version HyBi 00. |
michael@0 | 33 | |
michael@0 | 34 | Specification: |
michael@0 | 35 | http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00 |
michael@0 | 36 | """ |
michael@0 | 37 | |
michael@0 | 38 | |
michael@0 | 39 | # Note: request.connection.write/read are used in this module, even though |
michael@0 | 40 | # mod_python document says that they should be used only in connection |
michael@0 | 41 | # handlers. Unfortunately, we have no other options. For example, |
michael@0 | 42 | # request.write/read are not suitable because they don't allow direct raw bytes |
michael@0 | 43 | # writing/reading. |
michael@0 | 44 | |
michael@0 | 45 | |
michael@0 | 46 | import logging |
michael@0 | 47 | import re |
michael@0 | 48 | import struct |
michael@0 | 49 | |
michael@0 | 50 | from mod_pywebsocket import common |
michael@0 | 51 | from mod_pywebsocket.stream import StreamHixie75 |
michael@0 | 52 | from mod_pywebsocket import util |
michael@0 | 53 | from mod_pywebsocket.handshake._base import HandshakeException |
michael@0 | 54 | from mod_pywebsocket.handshake._base import build_location |
michael@0 | 55 | from mod_pywebsocket.handshake._base import check_header_lines |
michael@0 | 56 | from mod_pywebsocket.handshake._base import format_header |
michael@0 | 57 | from mod_pywebsocket.handshake._base import get_mandatory_header |
michael@0 | 58 | from mod_pywebsocket.handshake._base import validate_subprotocol |
michael@0 | 59 | |
michael@0 | 60 | |
michael@0 | 61 | _MANDATORY_HEADERS = [ |
michael@0 | 62 | # key, expected value or None |
michael@0 | 63 | [common.UPGRADE_HEADER, common.WEBSOCKET_UPGRADE_TYPE_HIXIE75], |
michael@0 | 64 | [common.CONNECTION_HEADER, common.UPGRADE_CONNECTION_TYPE], |
michael@0 | 65 | ] |
michael@0 | 66 | |
michael@0 | 67 | |
michael@0 | 68 | class Handshaker(object): |
michael@0 | 69 | """Opening handshake processor for the WebSocket protocol version HyBi 00. |
michael@0 | 70 | """ |
michael@0 | 71 | |
michael@0 | 72 | def __init__(self, request, dispatcher): |
michael@0 | 73 | """Construct an instance. |
michael@0 | 74 | |
michael@0 | 75 | Args: |
michael@0 | 76 | request: mod_python request. |
michael@0 | 77 | dispatcher: Dispatcher (dispatch.Dispatcher). |
michael@0 | 78 | |
michael@0 | 79 | Handshaker will add attributes such as ws_resource in performing |
michael@0 | 80 | handshake. |
michael@0 | 81 | """ |
michael@0 | 82 | |
michael@0 | 83 | self._logger = util.get_class_logger(self) |
michael@0 | 84 | |
michael@0 | 85 | self._request = request |
michael@0 | 86 | self._dispatcher = dispatcher |
michael@0 | 87 | |
michael@0 | 88 | def do_handshake(self): |
michael@0 | 89 | """Perform WebSocket Handshake. |
michael@0 | 90 | |
michael@0 | 91 | On _request, we set |
michael@0 | 92 | ws_resource, ws_protocol, ws_location, ws_origin, ws_challenge, |
michael@0 | 93 | ws_challenge_md5: WebSocket handshake information. |
michael@0 | 94 | ws_stream: Frame generation/parsing class. |
michael@0 | 95 | ws_version: Protocol version. |
michael@0 | 96 | |
michael@0 | 97 | Raises: |
michael@0 | 98 | HandshakeException: when any error happened in parsing the opening |
michael@0 | 99 | handshake request. |
michael@0 | 100 | """ |
michael@0 | 101 | |
michael@0 | 102 | # 5.1 Reading the client's opening handshake. |
michael@0 | 103 | # dispatcher sets it in self._request. |
michael@0 | 104 | check_header_lines(self._request, _MANDATORY_HEADERS) |
michael@0 | 105 | self._set_resource() |
michael@0 | 106 | self._set_subprotocol() |
michael@0 | 107 | self._set_location() |
michael@0 | 108 | self._set_origin() |
michael@0 | 109 | self._set_challenge_response() |
michael@0 | 110 | self._set_protocol_version() |
michael@0 | 111 | |
michael@0 | 112 | self._dispatcher.do_extra_handshake(self._request) |
michael@0 | 113 | |
michael@0 | 114 | self._send_handshake() |
michael@0 | 115 | |
michael@0 | 116 | def _set_resource(self): |
michael@0 | 117 | self._request.ws_resource = self._request.uri |
michael@0 | 118 | |
michael@0 | 119 | def _set_subprotocol(self): |
michael@0 | 120 | # |Sec-WebSocket-Protocol| |
michael@0 | 121 | subprotocol = self._request.headers_in.get( |
michael@0 | 122 | common.SEC_WEBSOCKET_PROTOCOL_HEADER) |
michael@0 | 123 | if subprotocol is not None: |
michael@0 | 124 | validate_subprotocol(subprotocol, hixie=True) |
michael@0 | 125 | self._request.ws_protocol = subprotocol |
michael@0 | 126 | |
michael@0 | 127 | def _set_location(self): |
michael@0 | 128 | # |Host| |
michael@0 | 129 | host = self._request.headers_in.get(common.HOST_HEADER) |
michael@0 | 130 | if host is not None: |
michael@0 | 131 | self._request.ws_location = build_location(self._request) |
michael@0 | 132 | # TODO(ukai): check host is this host. |
michael@0 | 133 | |
michael@0 | 134 | def _set_origin(self): |
michael@0 | 135 | # |Origin| |
michael@0 | 136 | origin = self._request.headers_in.get(common.ORIGIN_HEADER) |
michael@0 | 137 | if origin is not None: |
michael@0 | 138 | self._request.ws_origin = origin |
michael@0 | 139 | |
michael@0 | 140 | def _set_protocol_version(self): |
michael@0 | 141 | # |Sec-WebSocket-Draft| |
michael@0 | 142 | draft = self._request.headers_in.get(common.SEC_WEBSOCKET_DRAFT_HEADER) |
michael@0 | 143 | if draft is not None and draft != '0': |
michael@0 | 144 | raise HandshakeException('Illegal value for %s: %s' % |
michael@0 | 145 | (common.SEC_WEBSOCKET_DRAFT_HEADER, |
michael@0 | 146 | draft)) |
michael@0 | 147 | |
michael@0 | 148 | self._logger.debug('Protocol version is HyBi 00') |
michael@0 | 149 | self._request.ws_version = common.VERSION_HYBI00 |
michael@0 | 150 | self._request.ws_stream = StreamHixie75(self._request, True) |
michael@0 | 151 | |
michael@0 | 152 | def _set_challenge_response(self): |
michael@0 | 153 | # 5.2 4-8. |
michael@0 | 154 | self._request.ws_challenge = self._get_challenge() |
michael@0 | 155 | # 5.2 9. let /response/ be the MD5 finterprint of /challenge/ |
michael@0 | 156 | self._request.ws_challenge_md5 = util.md5_hash( |
michael@0 | 157 | self._request.ws_challenge).digest() |
michael@0 | 158 | self._logger.debug( |
michael@0 | 159 | 'Challenge: %r (%s)', |
michael@0 | 160 | self._request.ws_challenge, |
michael@0 | 161 | util.hexify(self._request.ws_challenge)) |
michael@0 | 162 | self._logger.debug( |
michael@0 | 163 | 'Challenge response: %r (%s)', |
michael@0 | 164 | self._request.ws_challenge_md5, |
michael@0 | 165 | util.hexify(self._request.ws_challenge_md5)) |
michael@0 | 166 | |
michael@0 | 167 | def _get_key_value(self, key_field): |
michael@0 | 168 | key_value = get_mandatory_header(self._request, key_field) |
michael@0 | 169 | |
michael@0 | 170 | self._logger.debug('%s: %r', key_field, key_value) |
michael@0 | 171 | |
michael@0 | 172 | # 5.2 4. let /key-number_n/ be the digits (characters in the range |
michael@0 | 173 | # U+0030 DIGIT ZERO (0) to U+0039 DIGIT NINE (9)) in /key_n/, |
michael@0 | 174 | # interpreted as a base ten integer, ignoring all other characters |
michael@0 | 175 | # in /key_n/. |
michael@0 | 176 | try: |
michael@0 | 177 | key_number = int(re.sub("\\D", "", key_value)) |
michael@0 | 178 | except: |
michael@0 | 179 | raise HandshakeException('%s field contains no digit' % key_field) |
michael@0 | 180 | # 5.2 5. let /spaces_n/ be the number of U+0020 SPACE characters |
michael@0 | 181 | # in /key_n/. |
michael@0 | 182 | spaces = re.subn(" ", "", key_value)[1] |
michael@0 | 183 | if spaces == 0: |
michael@0 | 184 | raise HandshakeException('%s field contains no space' % key_field) |
michael@0 | 185 | |
michael@0 | 186 | self._logger.debug( |
michael@0 | 187 | '%s: Key-number is %d and number of spaces is %d', |
michael@0 | 188 | key_field, key_number, spaces) |
michael@0 | 189 | |
michael@0 | 190 | # 5.2 6. if /key-number_n/ is not an integral multiple of /spaces_n/ |
michael@0 | 191 | # then abort the WebSocket connection. |
michael@0 | 192 | if key_number % spaces != 0: |
michael@0 | 193 | raise HandshakeException( |
michael@0 | 194 | '%s: Key-number (%d) is not an integral multiple of spaces ' |
michael@0 | 195 | '(%d)' % (key_field, key_number, spaces)) |
michael@0 | 196 | # 5.2 7. let /part_n/ be /key-number_n/ divided by /spaces_n/. |
michael@0 | 197 | part = key_number / spaces |
michael@0 | 198 | self._logger.debug('%s: Part is %d', key_field, part) |
michael@0 | 199 | return part |
michael@0 | 200 | |
michael@0 | 201 | def _get_challenge(self): |
michael@0 | 202 | # 5.2 4-7. |
michael@0 | 203 | key1 = self._get_key_value(common.SEC_WEBSOCKET_KEY1_HEADER) |
michael@0 | 204 | key2 = self._get_key_value(common.SEC_WEBSOCKET_KEY2_HEADER) |
michael@0 | 205 | # 5.2 8. let /challenge/ be the concatenation of /part_1/, |
michael@0 | 206 | challenge = '' |
michael@0 | 207 | challenge += struct.pack('!I', key1) # network byteorder int |
michael@0 | 208 | challenge += struct.pack('!I', key2) # network byteorder int |
michael@0 | 209 | challenge += self._request.connection.read(8) |
michael@0 | 210 | return challenge |
michael@0 | 211 | |
michael@0 | 212 | def _send_handshake(self): |
michael@0 | 213 | response = [] |
michael@0 | 214 | |
michael@0 | 215 | # 5.2 10. send the following line. |
michael@0 | 216 | response.append('HTTP/1.1 101 WebSocket Protocol Handshake\r\n') |
michael@0 | 217 | |
michael@0 | 218 | # 5.2 11. send the following fields to the client. |
michael@0 | 219 | response.append(format_header( |
michael@0 | 220 | common.UPGRADE_HEADER, common.WEBSOCKET_UPGRADE_TYPE_HIXIE75)) |
michael@0 | 221 | response.append(format_header( |
michael@0 | 222 | common.CONNECTION_HEADER, common.UPGRADE_CONNECTION_TYPE)) |
michael@0 | 223 | response.append(format_header( |
michael@0 | 224 | common.SEC_WEBSOCKET_LOCATION_HEADER, self._request.ws_location)) |
michael@0 | 225 | response.append(format_header( |
michael@0 | 226 | common.SEC_WEBSOCKET_ORIGIN_HEADER, self._request.ws_origin)) |
michael@0 | 227 | if self._request.ws_protocol: |
michael@0 | 228 | response.append(format_header( |
michael@0 | 229 | common.SEC_WEBSOCKET_PROTOCOL_HEADER, |
michael@0 | 230 | self._request.ws_protocol)) |
michael@0 | 231 | # 5.2 12. send two bytes 0x0D 0x0A. |
michael@0 | 232 | response.append('\r\n') |
michael@0 | 233 | # 5.2 13. send /response/ |
michael@0 | 234 | response.append(self._request.ws_challenge_md5) |
michael@0 | 235 | |
michael@0 | 236 | raw_response = ''.join(response) |
michael@0 | 237 | self._request.connection.write(raw_response) |
michael@0 | 238 | self._logger.debug('Sent server\'s opening handshake: %r', |
michael@0 | 239 | raw_response) |
michael@0 | 240 | |
michael@0 | 241 | |
michael@0 | 242 | # vi:sts=4 sw=4 et |