testing/mochitest/pywebsocket/mod_pywebsocket/_stream_base.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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 """Base stream class.
michael@0 32 """
michael@0 33
michael@0 34
michael@0 35 # Note: request.connection.write/read are used in this module, even though
michael@0 36 # mod_python document says that they should be used only in connection
michael@0 37 # handlers. Unfortunately, we have no other options. For example,
michael@0 38 # request.write/read are not suitable because they don't allow direct raw bytes
michael@0 39 # writing/reading.
michael@0 40
michael@0 41
michael@0 42 from mod_pywebsocket import util
michael@0 43
michael@0 44
michael@0 45 # Exceptions
michael@0 46
michael@0 47
michael@0 48 class ConnectionTerminatedException(Exception):
michael@0 49 """This exception will be raised when a connection is terminated
michael@0 50 unexpectedly.
michael@0 51 """
michael@0 52
michael@0 53 pass
michael@0 54
michael@0 55
michael@0 56 class InvalidFrameException(ConnectionTerminatedException):
michael@0 57 """This exception will be raised when we received an invalid frame we
michael@0 58 cannot parse.
michael@0 59 """
michael@0 60
michael@0 61 pass
michael@0 62
michael@0 63
michael@0 64 class BadOperationException(Exception):
michael@0 65 """This exception will be raised when send_message() is called on
michael@0 66 server-terminated connection or receive_message() is called on
michael@0 67 client-terminated connection.
michael@0 68 """
michael@0 69
michael@0 70 pass
michael@0 71
michael@0 72
michael@0 73 class UnsupportedFrameException(Exception):
michael@0 74 """This exception will be raised when we receive a frame with flag, opcode
michael@0 75 we cannot handle. Handlers can just catch and ignore this exception and
michael@0 76 call receive_message() again to continue processing the next frame.
michael@0 77 """
michael@0 78
michael@0 79 pass
michael@0 80
michael@0 81
michael@0 82 class InvalidUTF8Exception(Exception):
michael@0 83 """This exception will be raised when we receive a text frame which
michael@0 84 contains invalid UTF-8 strings.
michael@0 85 """
michael@0 86
michael@0 87 pass
michael@0 88
michael@0 89
michael@0 90 class StreamBase(object):
michael@0 91 """Base stream class."""
michael@0 92
michael@0 93 def __init__(self, request):
michael@0 94 """Construct an instance.
michael@0 95
michael@0 96 Args:
michael@0 97 request: mod_python request.
michael@0 98 """
michael@0 99
michael@0 100 self._logger = util.get_class_logger(self)
michael@0 101
michael@0 102 self._request = request
michael@0 103
michael@0 104 def _read(self, length):
michael@0 105 """Reads length bytes from connection. In case we catch any exception,
michael@0 106 prepends remote address to the exception message and raise again.
michael@0 107
michael@0 108 Raises:
michael@0 109 ConnectionTerminatedException: when read returns empty string.
michael@0 110 """
michael@0 111
michael@0 112 bytes = self._request.connection.read(length)
michael@0 113 if not bytes:
michael@0 114 raise ConnectionTerminatedException(
michael@0 115 'Receiving %d byte failed. Peer (%r) closed connection' %
michael@0 116 (length, (self._request.connection.remote_addr,)))
michael@0 117 return bytes
michael@0 118
michael@0 119 def _write(self, bytes):
michael@0 120 """Writes given bytes to connection. In case we catch any exception,
michael@0 121 prepends remote address to the exception message and raise again.
michael@0 122 """
michael@0 123
michael@0 124 try:
michael@0 125 self._request.connection.write(bytes)
michael@0 126 except Exception, e:
michael@0 127 util.prepend_message_to_exception(
michael@0 128 'Failed to send message to %r: ' %
michael@0 129 (self._request.connection.remote_addr,),
michael@0 130 e)
michael@0 131 raise
michael@0 132
michael@0 133 def receive_bytes(self, length):
michael@0 134 """Receives multiple bytes. Retries read when we couldn't receive the
michael@0 135 specified amount.
michael@0 136
michael@0 137 Raises:
michael@0 138 ConnectionTerminatedException: when read returns empty string.
michael@0 139 """
michael@0 140
michael@0 141 bytes = []
michael@0 142 while length > 0:
michael@0 143 new_bytes = self._read(length)
michael@0 144 bytes.append(new_bytes)
michael@0 145 length -= len(new_bytes)
michael@0 146 return ''.join(bytes)
michael@0 147
michael@0 148 def _read_until(self, delim_char):
michael@0 149 """Reads bytes until we encounter delim_char. The result will not
michael@0 150 contain delim_char.
michael@0 151
michael@0 152 Raises:
michael@0 153 ConnectionTerminatedException: when read returns empty string.
michael@0 154 """
michael@0 155
michael@0 156 bytes = []
michael@0 157 while True:
michael@0 158 ch = self._read(1)
michael@0 159 if ch == delim_char:
michael@0 160 break
michael@0 161 bytes.append(ch)
michael@0 162 return ''.join(bytes)
michael@0 163
michael@0 164
michael@0 165 # vi:sts=4 sw=4 et

mercurial