xmlstream.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. # -*- test-case-name: twisted.words.test.test_xmlstream -*-
  2. #
  3. # Copyright (c) Twisted Matrix Laboratories.
  4. # See LICENSE for details.
  5. """
  6. XML Stream processing.
  7. An XML Stream is defined as a connection over which two XML documents are
  8. exchanged during the lifetime of the connection, one for each direction. The
  9. unit of interaction is a direct child element of the root element (stanza).
  10. The most prominent use of XML Streams is Jabber, but this module is generically
  11. usable. See Twisted Words for Jabber specific protocol support.
  12. Maintainer: Ralph Meijer
  13. @var STREAM_CONNECTED_EVENT: This event signals that the connection has been
  14. established.
  15. @type STREAM_CONNECTED_EVENT: L{str}.
  16. @var STREAM_END_EVENT: This event signals that the connection has been closed.
  17. @type STREAM_END_EVENT: L{str}.
  18. @var STREAM_ERROR_EVENT: This event signals that a parse error occurred.
  19. @type STREAM_ERROR_EVENT: L{str}.
  20. @var STREAM_START_EVENT: This event signals that the root element of the XML
  21. Stream has been received.
  22. For XMPP, this would be the C{<stream:stream ...>} opening tag.
  23. @type STREAM_START_EVENT: L{str}.
  24. """
  25. from __future__ import absolute_import, division
  26. from twisted.python import failure
  27. from twisted.python.compat import intern, unicode
  28. from twisted.internet import protocol
  29. from twisted.words.xish import domish, utility
  30. STREAM_CONNECTED_EVENT = intern("//event/stream/connected")
  31. STREAM_START_EVENT = intern("//event/stream/start")
  32. STREAM_END_EVENT = intern("//event/stream/end")
  33. STREAM_ERROR_EVENT = intern("//event/stream/error")
  34. class XmlStream(protocol.Protocol, utility.EventDispatcher):
  35. """ Generic Streaming XML protocol handler.
  36. This protocol handler will parse incoming data as XML and dispatch events
  37. accordingly. Incoming stanzas can be handled by registering observers using
  38. XPath-like expressions that are matched against each stanza. See
  39. L{utility.EventDispatcher} for details.
  40. """
  41. def __init__(self):
  42. utility.EventDispatcher.__init__(self)
  43. self.stream = None
  44. self.rawDataOutFn = None
  45. self.rawDataInFn = None
  46. def _initializeStream(self):
  47. """ Sets up XML Parser. """
  48. self.stream = domish.elementStream()
  49. self.stream.DocumentStartEvent = self.onDocumentStart
  50. self.stream.ElementEvent = self.onElement
  51. self.stream.DocumentEndEvent = self.onDocumentEnd
  52. ### --------------------------------------------------------------
  53. ###
  54. ### Protocol events
  55. ###
  56. ### --------------------------------------------------------------
  57. def connectionMade(self):
  58. """ Called when a connection is made.
  59. Sets up the XML parser and dispatches the L{STREAM_CONNECTED_EVENT}
  60. event indicating the connection has been established.
  61. """
  62. self._initializeStream()
  63. self.dispatch(self, STREAM_CONNECTED_EVENT)
  64. def dataReceived(self, data):
  65. """ Called whenever data is received.
  66. Passes the data to the XML parser. This can result in calls to the
  67. DOM handlers. If a parse error occurs, the L{STREAM_ERROR_EVENT} event
  68. is called to allow for cleanup actions, followed by dropping the
  69. connection.
  70. """
  71. try:
  72. if self.rawDataInFn:
  73. self.rawDataInFn(data)
  74. self.stream.parse(data)
  75. except domish.ParserError:
  76. self.dispatch(failure.Failure(), STREAM_ERROR_EVENT)
  77. self.transport.loseConnection()
  78. def connectionLost(self, reason):
  79. """ Called when the connection is shut down.
  80. Dispatches the L{STREAM_END_EVENT}.
  81. """
  82. self.dispatch(reason, STREAM_END_EVENT)
  83. self.stream = None
  84. ### --------------------------------------------------------------
  85. ###
  86. ### DOM events
  87. ###
  88. ### --------------------------------------------------------------
  89. def onDocumentStart(self, rootElement):
  90. """ Called whenever the start tag of a root element has been received.
  91. Dispatches the L{STREAM_START_EVENT}.
  92. """
  93. self.dispatch(self, STREAM_START_EVENT)
  94. def onElement(self, element):
  95. """ Called whenever a direct child element of the root element has
  96. been received.
  97. Dispatches the received element.
  98. """
  99. self.dispatch(element)
  100. def onDocumentEnd(self):
  101. """ Called whenever the end tag of the root element has been received.
  102. Closes the connection. This causes C{connectionLost} being called.
  103. """
  104. self.transport.loseConnection()
  105. def setDispatchFn(self, fn):
  106. """ Set another function to handle elements. """
  107. self.stream.ElementEvent = fn
  108. def resetDispatchFn(self):
  109. """ Set the default function (C{onElement}) to handle elements. """
  110. self.stream.ElementEvent = self.onElement
  111. def send(self, obj):
  112. """ Send data over the stream.
  113. Sends the given C{obj} over the connection. C{obj} may be instances of
  114. L{domish.Element}, C{unicode} and C{str}. The first two will be
  115. properly serialized and/or encoded. C{str} objects must be in UTF-8
  116. encoding.
  117. Note: because it is easy to make mistakes in maintaining a properly
  118. encoded C{str} object, it is advised to use C{unicode} objects
  119. everywhere when dealing with XML Streams.
  120. @param obj: Object to be sent over the stream.
  121. @type obj: L{domish.Element}, L{domish} or C{str}
  122. """
  123. if domish.IElement.providedBy(obj):
  124. obj = obj.toXml()
  125. if isinstance(obj, unicode):
  126. obj = obj.encode('utf-8')
  127. if self.rawDataOutFn:
  128. self.rawDataOutFn(obj)
  129. self.transport.write(obj)
  130. class BootstrapMixin(object):
  131. """
  132. XmlStream factory mixin to install bootstrap event observers.
  133. This mixin is for factories providing
  134. L{IProtocolFactory<twisted.internet.interfaces.IProtocolFactory>} to make
  135. sure bootstrap event observers are set up on protocols, before incoming
  136. data is processed. Such protocols typically derive from
  137. L{utility.EventDispatcher}, like L{XmlStream}.
  138. You can set up bootstrap event observers using C{addBootstrap}. The
  139. C{event} and C{fn} parameters correspond with the C{event} and
  140. C{observerfn} arguments to L{utility.EventDispatcher.addObserver}.
  141. @since: 8.2.
  142. @ivar bootstraps: The list of registered bootstrap event observers.
  143. @type bootstrap: C{list}
  144. """
  145. def __init__(self):
  146. self.bootstraps = []
  147. def installBootstraps(self, dispatcher):
  148. """
  149. Install registered bootstrap observers.
  150. @param dispatcher: Event dispatcher to add the observers to.
  151. @type dispatcher: L{utility.EventDispatcher}
  152. """
  153. for event, fn in self.bootstraps:
  154. dispatcher.addObserver(event, fn)
  155. def addBootstrap(self, event, fn):
  156. """
  157. Add a bootstrap event handler.
  158. @param event: The event to register an observer for.
  159. @type event: C{str} or L{xpath.XPathQuery}
  160. @param fn: The observer callable to be registered.
  161. """
  162. self.bootstraps.append((event, fn))
  163. def removeBootstrap(self, event, fn):
  164. """
  165. Remove a bootstrap event handler.
  166. @param event: The event the observer is registered for.
  167. @type event: C{str} or L{xpath.XPathQuery}
  168. @param fn: The registered observer callable.
  169. """
  170. self.bootstraps.remove((event, fn))
  171. class XmlStreamFactoryMixin(BootstrapMixin):
  172. """
  173. XmlStream factory mixin that takes care of event handlers.
  174. All positional and keyword arguments passed to create this factory are
  175. passed on as-is to the protocol.
  176. @ivar args: Positional arguments passed to the protocol upon instantiation.
  177. @type args: C{tuple}.
  178. @ivar kwargs: Keyword arguments passed to the protocol upon instantiation.
  179. @type kwargs: C{dict}.
  180. """
  181. def __init__(self, *args, **kwargs):
  182. BootstrapMixin.__init__(self)
  183. self.args = args
  184. self.kwargs = kwargs
  185. def buildProtocol(self, addr):
  186. """
  187. Create an instance of XmlStream.
  188. The returned instance will have bootstrap event observers registered
  189. and will proceed to handle input on an incoming connection.
  190. """
  191. xs = self.protocol(*self.args, **self.kwargs)
  192. xs.factory = self
  193. self.installBootstraps(xs)
  194. return xs
  195. class XmlStreamFactory(XmlStreamFactoryMixin,
  196. protocol.ReconnectingClientFactory):
  197. """
  198. Factory for XmlStream protocol objects as a reconnection client.
  199. """
  200. protocol = XmlStream
  201. def buildProtocol(self, addr):
  202. """
  203. Create a protocol instance.
  204. Overrides L{XmlStreamFactoryMixin.buildProtocol} to work with
  205. a L{ReconnectingClientFactory}. As this is called upon having an
  206. connection established, we are resetting the delay for reconnection
  207. attempts when the connection is lost again.
  208. """
  209. self.resetDelay()
  210. return XmlStreamFactoryMixin.buildProtocol(self, addr)