_stream_base.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # Copyright 2011, Google Inc.
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following disclaimer
  12. # in the documentation and/or other materials provided with the
  13. # distribution.
  14. # * Neither the name of Google Inc. nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. """Base stream class.
  30. """
  31. # Note: request.connection.write/read are used in this module, even though
  32. # mod_python document says that they should be used only in connection
  33. # handlers. Unfortunately, we have no other options. For example,
  34. # request.write/read are not suitable because they don't allow direct raw bytes
  35. # writing/reading.
  36. import socket
  37. from mod_pywebsocket import util
  38. # Exceptions
  39. class ConnectionTerminatedException(Exception):
  40. """This exception will be raised when a connection is terminated
  41. unexpectedly.
  42. """
  43. pass
  44. class InvalidFrameException(ConnectionTerminatedException):
  45. """This exception will be raised when we received an invalid frame we
  46. cannot parse.
  47. """
  48. pass
  49. class BadOperationException(Exception):
  50. """This exception will be raised when send_message() is called on
  51. server-terminated connection or receive_message() is called on
  52. client-terminated connection.
  53. """
  54. pass
  55. class UnsupportedFrameException(Exception):
  56. """This exception will be raised when we receive a frame with flag, opcode
  57. we cannot handle. Handlers can just catch and ignore this exception and
  58. call receive_message() again to continue processing the next frame.
  59. """
  60. pass
  61. class InvalidUTF8Exception(Exception):
  62. """This exception will be raised when we receive a text frame which
  63. contains invalid UTF-8 strings.
  64. """
  65. pass
  66. class StreamBase(object):
  67. """Base stream class."""
  68. def __init__(self, request):
  69. """Construct an instance.
  70. Args:
  71. request: mod_python request.
  72. """
  73. self._logger = util.get_class_logger(self)
  74. self._request = request
  75. def _read(self, length):
  76. """Reads length bytes from connection. In case we catch any exception,
  77. prepends remote address to the exception message and raise again.
  78. Raises:
  79. ConnectionTerminatedException: when read returns empty string.
  80. """
  81. try:
  82. read_bytes = self._request.connection.read(length)
  83. if not read_bytes:
  84. raise ConnectionTerminatedException(
  85. 'Receiving %d byte failed. Peer (%r) closed connection' %
  86. (length, (self._request.connection.remote_addr,)))
  87. return read_bytes
  88. except socket.error, e:
  89. # Catch a socket.error. Because it's not a child class of the
  90. # IOError prior to Python 2.6, we cannot omit this except clause.
  91. # Use %s rather than %r for the exception to use human friendly
  92. # format.
  93. raise ConnectionTerminatedException(
  94. 'Receiving %d byte failed. socket.error (%s) occurred' %
  95. (length, e))
  96. except IOError, e:
  97. # Also catch an IOError because mod_python throws it.
  98. raise ConnectionTerminatedException(
  99. 'Receiving %d byte failed. IOError (%s) occurred' %
  100. (length, e))
  101. def _write(self, bytes_to_write):
  102. """Writes given bytes to connection. In case we catch any exception,
  103. prepends remote address to the exception message and raise again.
  104. """
  105. try:
  106. self._request.connection.write(bytes_to_write)
  107. except Exception, e:
  108. util.prepend_message_to_exception(
  109. 'Failed to send message to %r: ' %
  110. (self._request.connection.remote_addr,),
  111. e)
  112. raise
  113. def receive_bytes(self, length):
  114. """Receives multiple bytes. Retries read when we couldn't receive the
  115. specified amount.
  116. Raises:
  117. ConnectionTerminatedException: when read returns empty string.
  118. """
  119. read_bytes = []
  120. while length > 0:
  121. new_read_bytes = self._read(length)
  122. read_bytes.append(new_read_bytes)
  123. length -= len(new_read_bytes)
  124. return ''.join(read_bytes)
  125. def _read_until(self, delim_char):
  126. """Reads bytes until we encounter delim_char. The result will not
  127. contain delim_char.
  128. Raises:
  129. ConnectionTerminatedException: when read returns empty string.
  130. """
  131. read_bytes = []
  132. while True:
  133. ch = self._read(1)
  134. if ch == delim_char:
  135. break
  136. read_bytes.append(ch)
  137. return ''.join(read_bytes)
  138. # vi:sts=4 sw=4 et