1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mochitest/pywebsocket/mod_pywebsocket/_stream_base.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,165 @@ 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 +"""Base stream class. 1.35 +""" 1.36 + 1.37 + 1.38 +# Note: request.connection.write/read are used in this module, even though 1.39 +# mod_python document says that they should be used only in connection 1.40 +# handlers. Unfortunately, we have no other options. For example, 1.41 +# request.write/read are not suitable because they don't allow direct raw bytes 1.42 +# writing/reading. 1.43 + 1.44 + 1.45 +from mod_pywebsocket import util 1.46 + 1.47 + 1.48 +# Exceptions 1.49 + 1.50 + 1.51 +class ConnectionTerminatedException(Exception): 1.52 + """This exception will be raised when a connection is terminated 1.53 + unexpectedly. 1.54 + """ 1.55 + 1.56 + pass 1.57 + 1.58 + 1.59 +class InvalidFrameException(ConnectionTerminatedException): 1.60 + """This exception will be raised when we received an invalid frame we 1.61 + cannot parse. 1.62 + """ 1.63 + 1.64 + pass 1.65 + 1.66 + 1.67 +class BadOperationException(Exception): 1.68 + """This exception will be raised when send_message() is called on 1.69 + server-terminated connection or receive_message() is called on 1.70 + client-terminated connection. 1.71 + """ 1.72 + 1.73 + pass 1.74 + 1.75 + 1.76 +class UnsupportedFrameException(Exception): 1.77 + """This exception will be raised when we receive a frame with flag, opcode 1.78 + we cannot handle. Handlers can just catch and ignore this exception and 1.79 + call receive_message() again to continue processing the next frame. 1.80 + """ 1.81 + 1.82 + pass 1.83 + 1.84 + 1.85 +class InvalidUTF8Exception(Exception): 1.86 + """This exception will be raised when we receive a text frame which 1.87 + contains invalid UTF-8 strings. 1.88 + """ 1.89 + 1.90 + pass 1.91 + 1.92 + 1.93 +class StreamBase(object): 1.94 + """Base stream class.""" 1.95 + 1.96 + def __init__(self, request): 1.97 + """Construct an instance. 1.98 + 1.99 + Args: 1.100 + request: mod_python request. 1.101 + """ 1.102 + 1.103 + self._logger = util.get_class_logger(self) 1.104 + 1.105 + self._request = request 1.106 + 1.107 + def _read(self, length): 1.108 + """Reads length bytes from connection. In case we catch any exception, 1.109 + prepends remote address to the exception message and raise again. 1.110 + 1.111 + Raises: 1.112 + ConnectionTerminatedException: when read returns empty string. 1.113 + """ 1.114 + 1.115 + bytes = self._request.connection.read(length) 1.116 + if not bytes: 1.117 + raise ConnectionTerminatedException( 1.118 + 'Receiving %d byte failed. Peer (%r) closed connection' % 1.119 + (length, (self._request.connection.remote_addr,))) 1.120 + return bytes 1.121 + 1.122 + def _write(self, bytes): 1.123 + """Writes given bytes to connection. In case we catch any exception, 1.124 + prepends remote address to the exception message and raise again. 1.125 + """ 1.126 + 1.127 + try: 1.128 + self._request.connection.write(bytes) 1.129 + except Exception, e: 1.130 + util.prepend_message_to_exception( 1.131 + 'Failed to send message to %r: ' % 1.132 + (self._request.connection.remote_addr,), 1.133 + e) 1.134 + raise 1.135 + 1.136 + def receive_bytes(self, length): 1.137 + """Receives multiple bytes. Retries read when we couldn't receive the 1.138 + specified amount. 1.139 + 1.140 + Raises: 1.141 + ConnectionTerminatedException: when read returns empty string. 1.142 + """ 1.143 + 1.144 + bytes = [] 1.145 + while length > 0: 1.146 + new_bytes = self._read(length) 1.147 + bytes.append(new_bytes) 1.148 + length -= len(new_bytes) 1.149 + return ''.join(bytes) 1.150 + 1.151 + def _read_until(self, delim_char): 1.152 + """Reads bytes until we encounter delim_char. The result will not 1.153 + contain delim_char. 1.154 + 1.155 + Raises: 1.156 + ConnectionTerminatedException: when read returns empty string. 1.157 + """ 1.158 + 1.159 + bytes = [] 1.160 + while True: 1.161 + ch = self._read(1) 1.162 + if ch == delim_char: 1.163 + break 1.164 + bytes.append(ch) 1.165 + return ''.join(bytes) 1.166 + 1.167 + 1.168 +# vi:sts=4 sw=4 et