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()
def handle_command(self, data: bytes, stream: Stream) -> None: """ Handles an osp command in a string. """ try: tree = secET.fromstring(data) except secET.ParseError: logger.debug("Erroneous client input: %s", data) raise OspdCommandError('Invalid data') command_name = tree.tag logger.debug('Handling %s command request.', command_name) command = self.commands.get(command_name, None) if not command and command_name != "authenticate": raise OspdCommandError('Bogus command name') if not self.initialized and command.must_be_initialized: exception = OspdCommandError( '%s is still starting' % self.daemon_info['name'], 'error' ) response = exception.as_xml() stream.write(response) return response = command.handle_xml(tree) if isinstance(response, bytes): stream.write(response) else: for data in response: stream.write(data)
def handle_command(self, data: bytes, stream: Stream) -> None: """Handles an osp command in a string.""" try: tree = secET.fromstring(data) except secET.ParseError as e: logger.debug("Erroneous client input: %s", data) raise OspdCommandError('Invalid data') from e command_name = tree.tag logger.debug('Handling %s command request.', command_name) command = self.commands.get(command_name, None) if not command and command_name != "authenticate": raise OspdCommandError('Bogus command name') if not self.initialized and command.must_be_initialized: exception = OspdCommandError( f'{self.daemon_info["name"]} is still starting', 'error') response = exception.as_xml() stream.write(response) return response = command.handle_xml(tree) write_success = True if isinstance(response, bytes): write_success = stream.write(response) else: for data in response: write_success = stream.write(data) if not write_success: break scan_id = tree.get('scan_id') if self.scan_exists(scan_id) and command_name == "get_scans": if write_success: logger.debug( '%s: Results sent successfully to the client. Cleaning ' 'temporary result list.', scan_id, ) self.scan_collection.clean_temp_result_list(scan_id) else: logger.debug( '%s: Failed sending results to the client. Restoring ' 'result list into the cache.', scan_id, ) self.scan_collection.restore_temp_result_list(scan_id)
def test_as_xml(self): e = OspdCommandError('message') self.assertEqual( b'<osp_response status="400" status_text="message" />', e.as_xml())