1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mochitest/pywebsocket/mod_pywebsocket/common.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,179 @@ 1.4 +# Copyright 2012, 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 +# Constants indicating WebSocket protocol version. 1.35 +VERSION_HIXIE75 = -1 1.36 +VERSION_HYBI00 = 0 1.37 +VERSION_HYBI01 = 1 1.38 +VERSION_HYBI02 = 2 1.39 +VERSION_HYBI03 = 2 1.40 +VERSION_HYBI04 = 4 1.41 +VERSION_HYBI05 = 5 1.42 +VERSION_HYBI06 = 6 1.43 +VERSION_HYBI07 = 7 1.44 +VERSION_HYBI08 = 8 1.45 +VERSION_HYBI09 = 8 1.46 +VERSION_HYBI10 = 8 1.47 +VERSION_HYBI11 = 8 1.48 +VERSION_HYBI12 = 8 1.49 +VERSION_HYBI13 = 13 1.50 +VERSION_HYBI14 = 13 1.51 +VERSION_HYBI15 = 13 1.52 +VERSION_HYBI16 = 13 1.53 +VERSION_HYBI17 = 13 1.54 + 1.55 +# Constants indicating WebSocket protocol latest version. 1.56 +VERSION_HYBI_LATEST = VERSION_HYBI13 1.57 + 1.58 +# Port numbers 1.59 +DEFAULT_WEB_SOCKET_PORT = 80 1.60 +DEFAULT_WEB_SOCKET_SECURE_PORT = 443 1.61 + 1.62 +# Schemes 1.63 +WEB_SOCKET_SCHEME = 'ws' 1.64 +WEB_SOCKET_SECURE_SCHEME = 'wss' 1.65 + 1.66 +# Frame opcodes defined in the spec. 1.67 +OPCODE_CONTINUATION = 0x0 1.68 +OPCODE_TEXT = 0x1 1.69 +OPCODE_BINARY = 0x2 1.70 +OPCODE_CLOSE = 0x8 1.71 +OPCODE_PING = 0x9 1.72 +OPCODE_PONG = 0xa 1.73 + 1.74 +# UUIDs used by HyBi 04 and later opening handshake and frame masking. 1.75 +WEBSOCKET_ACCEPT_UUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11' 1.76 + 1.77 +# Opening handshake header names and expected values. 1.78 +UPGRADE_HEADER = 'Upgrade' 1.79 +WEBSOCKET_UPGRADE_TYPE = 'websocket' 1.80 +WEBSOCKET_UPGRADE_TYPE_HIXIE75 = 'WebSocket' 1.81 +CONNECTION_HEADER = 'Connection' 1.82 +UPGRADE_CONNECTION_TYPE = 'Upgrade' 1.83 +HOST_HEADER = 'Host' 1.84 +ORIGIN_HEADER = 'Origin' 1.85 +SEC_WEBSOCKET_ORIGIN_HEADER = 'Sec-WebSocket-Origin' 1.86 +SEC_WEBSOCKET_KEY_HEADER = 'Sec-WebSocket-Key' 1.87 +SEC_WEBSOCKET_ACCEPT_HEADER = 'Sec-WebSocket-Accept' 1.88 +SEC_WEBSOCKET_VERSION_HEADER = 'Sec-WebSocket-Version' 1.89 +SEC_WEBSOCKET_PROTOCOL_HEADER = 'Sec-WebSocket-Protocol' 1.90 +SEC_WEBSOCKET_EXTENSIONS_HEADER = 'Sec-WebSocket-Extensions' 1.91 +SEC_WEBSOCKET_DRAFT_HEADER = 'Sec-WebSocket-Draft' 1.92 +SEC_WEBSOCKET_KEY1_HEADER = 'Sec-WebSocket-Key1' 1.93 +SEC_WEBSOCKET_KEY2_HEADER = 'Sec-WebSocket-Key2' 1.94 +SEC_WEBSOCKET_LOCATION_HEADER = 'Sec-WebSocket-Location' 1.95 + 1.96 +# Extensions 1.97 +DEFLATE_STREAM_EXTENSION = 'deflate-stream' 1.98 +DEFLATE_FRAME_EXTENSION = 'deflate-frame' 1.99 +X_WEBKIT_DEFLATE_FRAME_EXTENSION = 'x-webkit-deflate-frame' 1.100 + 1.101 +# Status codes 1.102 +# Code STATUS_NO_STATUS_RECEIVED, STATUS_ABNORMAL_CLOSURE, and 1.103 +# STATUS_TLS_HANDSHAKE are pseudo codes to indicate specific error cases. 1.104 +# Could not be used for codes in actual closing frames. 1.105 +# Application level errors must use codes in the range 1.106 +# STATUS_USER_REGISTERED_BASE to STATUS_USER_PRIVATE_MAX. The codes in the 1.107 +# range STATUS_USER_REGISTERED_BASE to STATUS_USER_REGISTERED_MAX are managed 1.108 +# by IANA. Usually application must define user protocol level errors in the 1.109 +# range STATUS_USER_PRIVATE_BASE to STATUS_USER_PRIVATE_MAX. 1.110 +STATUS_NORMAL_CLOSURE = 1000 1.111 +STATUS_GOING_AWAY = 1001 1.112 +STATUS_PROTOCOL_ERROR = 1002 1.113 +STATUS_UNSUPPORTED_DATA = 1003 1.114 +STATUS_NO_STATUS_RECEIVED = 1005 1.115 +STATUS_ABNORMAL_CLOSURE = 1006 1.116 +STATUS_INVALID_FRAME_PAYLOAD_DATA = 1007 1.117 +STATUS_POLICY_VIOLATION = 1008 1.118 +STATUS_MESSAGE_TOO_BIG = 1009 1.119 +STATUS_MANDATORY_EXTENSION = 1010 1.120 +STATUS_INTERNAL_SERVER_ERROR = 1011 1.121 +STATUS_TLS_HANDSHAKE = 1015 1.122 +STATUS_USER_REGISTERED_BASE = 3000 1.123 +STATUS_USER_REGISTERED_MAX = 3999 1.124 +STATUS_USER_PRIVATE_BASE = 4000 1.125 +STATUS_USER_PRIVATE_MAX = 4999 1.126 +# Following definitions are aliases to keep compatibility. Applications must 1.127 +# not use these obsoleted definitions anymore. 1.128 +STATUS_NORMAL = STATUS_NORMAL_CLOSURE 1.129 +STATUS_UNSUPPORTED = STATUS_UNSUPPORTED_DATA 1.130 +STATUS_CODE_NOT_AVAILABLE = STATUS_NO_STATUS_RECEIVED 1.131 +STATUS_ABNORMAL_CLOSE = STATUS_ABNORMAL_CLOSURE 1.132 +STATUS_INVALID_FRAME_PAYLOAD = STATUS_INVALID_FRAME_PAYLOAD_DATA 1.133 +STATUS_MANDATORY_EXT = STATUS_MANDATORY_EXTENSION 1.134 + 1.135 +# HTTP status codes 1.136 +HTTP_STATUS_BAD_REQUEST = 400 1.137 +HTTP_STATUS_FORBIDDEN = 403 1.138 +HTTP_STATUS_NOT_FOUND = 404 1.139 + 1.140 + 1.141 +def is_control_opcode(opcode): 1.142 + return (opcode >> 3) == 1 1.143 + 1.144 + 1.145 +class ExtensionParameter(object): 1.146 + """Holds information about an extension which is exchanged on extension 1.147 + negotiation in opening handshake. 1.148 + """ 1.149 + 1.150 + def __init__(self, name): 1.151 + self._name = name 1.152 + # TODO(tyoshino): Change the data structure to more efficient one such 1.153 + # as dict when the spec changes to say like 1.154 + # - Parameter names must be unique 1.155 + # - The order of parameters is not significant 1.156 + self._parameters = [] 1.157 + 1.158 + def name(self): 1.159 + return self._name 1.160 + 1.161 + def add_parameter(self, name, value): 1.162 + self._parameters.append((name, value)) 1.163 + 1.164 + def get_parameters(self): 1.165 + return self._parameters 1.166 + 1.167 + def get_parameter_names(self): 1.168 + return [name for name, unused_value in self._parameters] 1.169 + 1.170 + def has_parameter(self, name): 1.171 + for param_name, param_value in self._parameters: 1.172 + if param_name == name: 1.173 + return True 1.174 + return False 1.175 + 1.176 + def get_parameter_value(self, name): 1.177 + for param_name, param_value in self._parameters: 1.178 + if param_name == name: 1.179 + return param_value 1.180 + 1.181 + 1.182 +# vi:sts=4 sw=4 et