Esempio n. 1
0
    def handle_client_stream(self, stream: Stream) -> None:
        """ Handles stream of data received from client. """
        data = b''

        request_parser = RequestParser()

        while True:
            try:
                buf = stream.read()
                if not buf:
                    break

                data += buf

                if request_parser.has_ended(buf):
                    break
            except (AttributeError, ValueError) as message:
                logger.error(message)
                return
            except (ssl.SSLError) as exception:
                logger.debug('Error: %s', exception)
                break
            except (socket.timeout) as exception:
                logger.debug('Request timeout: %s', exception)
                break

        if len(data) <= 0:
            logger.debug("Empty client stream")
            return

        if not self.initialized:
            exception = OspdCommandError(
                '%s is still starting' % self.daemon_info['name'], 'error')
            response = exception.as_xml()
            stream.write(response)
            stream.close()
            return

        response = None
        try:
            self.handle_command(data, stream)
        except OspdCommandError as exception:
            response = exception.as_xml()
            logger.debug('Command error: %s', exception.message)
        except Exception:  # pylint: disable=broad-except
            logger.exception('While handling client command:')
            exception = OspdCommandError('Fatal error', 'error')
            response = exception.as_xml()

        if response:
            stream.write(response)

        stream.close()
Esempio n. 2
0
 def test_parse(self):
     parser = RequestParser()
     self.assertFalse(parser.has_ended(b'<foo><bar>'))
     self.assertFalse(parser.has_ended(b'</bar>'))
     self.assertTrue(parser.has_ended(b'</foo>'))