def __init__(self, stream, address): self.stream = stream self.address = address self.connectiontime = self.last_seen = datetime.now() self.taghandler = TagHandler(self) self.parser = processor.StreamProcessor(self.taghandler.streamhandler, self.taghandler.contenthandler) self.stream.read_bytes(1, self._read_char)
def __init__(self, stream, address): self.stream = stream self.address = address self.connectiontime = self.last_seen = datetime.now() self.taghandler = TagHandler(self) self.parser = processor.StreamProcessor( self.taghandler.streamhandler, self.taghandler.contenthandler) self.stream.read_bytes(1, self._read_char)
def setUp(self): self.connection = MockConnection() self.taghandler = TagHandler(self.connection)
class TestTagHandler(PyfireTestCase): def setUp(self): self.connection = MockConnection() self.taghandler = TagHandler(self.connection) def test_stream_start(self): attrs = { 'to': 'localhost', 'xmlns': 'jabber:client', 'xmlns:stream': 'http://etherx.jabber.org/streams', 'version': '1.0' } attrs = MockAttr(attrs) self.taghandler.streamhandler(attrs) self.assertTrue(self.connection.strings[0].startswith( """<?xml version="1.0"?><stream:stream from="localhost" id=""")) self.assertTrue(self.connection.strings[0].endswith( """" version="1.0" xml:lang="en" xmlns="jabber:client" """ + """xmlns:stream="http://etherx.jabber.org/streams" >""")) def test_bad_stream_version(self): attrs = { 'to': 'localhost', 'xmlns': 'jabber:client', 'xmlns:stream': 'http://etherx.jabber.org/streams', 'version': '11.1' } attrs = MockAttr(attrs) with self.assertRaises(errors.UnsupportedVersionError) as cm: self.taghandler.streamhandler(attrs) def test_no_stream_version(self): attrs = { 'to': 'localhost', 'xmlns': 'jabber:client', 'xmlns:stream': 'http://etherx.jabber.org/streams' } attrs = MockAttr(attrs) self.taghandler.streamhandler(attrs) self.assertFalse('" version="' in self.connection.strings[0]) def test_streaminit_no_from(self): attrs = { 'to': 'localhost', 'xmlns': 'jabber:client', 'xmlns:stream': 'http://etherx.jabber.org/streams', 'version': '1.0' } attrs = MockAttr(attrs) self.taghandler.streamhandler(attrs) self.assertFalse('to="' in self.connection.strings[0]) def test_streaminit_has_from(self): attrs = { 'from': 'testuser@localhost', 'to': 'localhost', 'xmlns': 'jabber:client', 'xmlns:stream': 'http://etherx.jabber.org/streams', 'version': '1.0' } attrs = MockAttr(attrs) self.taghandler.streamhandler(attrs) self.assertTrue('to="' in self.connection.strings[0]) def test_streaminit_invalid_from(self): attrs = { 'from': '@localhost', 'to': 'localhost', 'xmlns': 'jabber:client', 'xmlns:stream': 'http://etherx.jabber.org/streams', 'version': '1.0' } attrs = MockAttr(attrs) with self.assertRaises(errors.InvalidFromError) as cm: self.taghandler.streamhandler(attrs)
class XMPPConnection(object): """One XMPP connection initiated by class:`XMPPServer`""" def __init__(self, stream, address): self.stream = stream self.address = address self.connectiontime = self.last_seen = datetime.now() self.taghandler = TagHandler(self) self.parser = processor.StreamProcessor( self.taghandler.streamhandler, self.taghandler.contenthandler) self.stream.read_bytes(1, self._read_char) def _read_char(self, data): """Reads from client in byte mode""" try: if data == " ": log.debug("Found whitespace keepalive") self.stream.read_bytes(1, self._read_char) else: log.debug("Processing byte: %s" % data) self.parser.feed(data) self.stream.read_until(">", self._read_xml) self.last_seen = datetime.now() except IOError: self.done() def _read_xml(self, data): """Reads from client until closing tag for xml is found""" try: self.last_seen = datetime.now() log.debug("Processing chunk: %s" % data) self.parser.feed(data) if self.parser.depth >= 2: self.stream.read_until(">", self._read_xml) else: self.stream.read_bytes(1, self._read_char) except IOError: self.done() def send_string(self, string, raises_error=True): """Sends a string to client""" try: self.stream.write(string) log.debug("Sent string to client:" + string) except IOError: if raises_error: raise def send_element(self, element, raises_error=True): """Serializes and send an ET Element""" self.send_string(ET.tostring(element), raises_error) def stop_connection(self): """Sends stream close, discards stream closed errors""" # Ignore IOErrors as stream already has been closed # as there is no need so send stream end element on closed streams ;) try: self.taghandler.close() self.send_string("</stream:stream>") except IOError: pass self.done() def done(self): """Does cleanup work""" self.stream.close() def closed(self): """Checks if underlying stream is closed""" return self.stream.closed()
class TestTagHandler(PyfireTestCase): def setUp(self): self.connection = MockConnection() self.taghandler = TagHandler(self.connection) def test_stream_start(self): attrs = { 'to': 'localhost', 'xmlns': 'jabber:client', 'xmlns:stream': 'http://etherx.jabber.org/streams', 'version': '1.0' } attrs = MockAttr(attrs) self.taghandler.streamhandler(attrs) self.assertTrue( self.connection.strings[0].startswith( """<?xml version="1.0"?><stream:stream from="localhost" id=""")) self.assertTrue( self.connection.strings[0].endswith( """" version="1.0" xml:lang="en" xmlns="jabber:client" """ + """xmlns:stream="http://etherx.jabber.org/streams" >""" )) def test_bad_stream_version(self): attrs = { 'to': 'localhost', 'xmlns': 'jabber:client', 'xmlns:stream': 'http://etherx.jabber.org/streams', 'version': '11.1' } attrs = MockAttr(attrs) with self.assertRaises(errors.UnsupportedVersionError) as cm: self.taghandler.streamhandler(attrs) def test_no_stream_version(self): attrs = { 'to': 'localhost', 'xmlns': 'jabber:client', 'xmlns:stream': 'http://etherx.jabber.org/streams' } attrs = MockAttr(attrs) self.taghandler.streamhandler(attrs) self.assertFalse('" version="' in self.connection.strings[0]) def test_streaminit_no_from(self): attrs = { 'to': 'localhost', 'xmlns': 'jabber:client', 'xmlns:stream': 'http://etherx.jabber.org/streams', 'version': '1.0' } attrs = MockAttr(attrs) self.taghandler.streamhandler(attrs) self.assertFalse('to="' in self.connection.strings[0]) def test_streaminit_has_from(self): attrs = { 'from': 'testuser@localhost', 'to': 'localhost', 'xmlns': 'jabber:client', 'xmlns:stream': 'http://etherx.jabber.org/streams', 'version': '1.0' } attrs = MockAttr(attrs) self.taghandler.streamhandler(attrs) self.assertTrue('to="' in self.connection.strings[0]) def test_streaminit_invalid_from(self): attrs = { 'from': '@localhost', 'to': 'localhost', 'xmlns': 'jabber:client', 'xmlns:stream': 'http://etherx.jabber.org/streams', 'version': '1.0' } attrs = MockAttr(attrs) with self.assertRaises(errors.InvalidFromError) as cm: self.taghandler.streamhandler(attrs)
class XMPPConnection(object): """One XMPP connection initiated by class:`XMPPServer`""" def __init__(self, stream, address): self.stream = stream self.address = address self.connectiontime = self.last_seen = datetime.now() self.taghandler = TagHandler(self) self.parser = processor.StreamProcessor(self.taghandler.streamhandler, self.taghandler.contenthandler) self.stream.read_bytes(1, self._read_char) def _read_char(self, data): """Reads from client in byte mode""" try: if data == " ": log.debug("Found whitespace keepalive") self.stream.read_bytes(1, self._read_char) else: log.debug("Processing byte: %s" % data) self.parser.feed(data) self.stream.read_until(">", self._read_xml) self.last_seen = datetime.now() except IOError: self.done() def _read_xml(self, data): """Reads from client until closing tag for xml is found""" try: self.last_seen = datetime.now() log.debug("Processing chunk: %s" % data) self.parser.feed(data) if self.parser.depth >= 2: self.stream.read_until(">", self._read_xml) else: self.stream.read_bytes(1, self._read_char) except IOError: self.done() def send_string(self, string, raises_error=True): """Sends a string to client""" try: self.stream.write(string) log.debug("Sent string to client:" + string) except IOError: if raises_error: raise def send_element(self, element, raises_error=True): """Serializes and send an ET Element""" self.send_string(ET.tostring(element), raises_error) def stop_connection(self): """Sends stream close, discards stream closed errors""" # Ignore IOErrors as stream already has been closed # as there is no need so send stream end element on closed streams ;) try: self.taghandler.close() self.send_string("</stream:stream>") except IOError: pass self.done() def done(self): """Does cleanup work""" self.stream.close() def closed(self): """Checks if underlying stream is closed""" return self.stream.closed()