mirror of
				https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
				synced 2025-11-04 12:22:47 +00:00 
			
		
		
		
	Defensive programming inc
This commit is contained in:
		@@ -413,8 +413,8 @@ class Client:
 | 
				
			|||||||
    def __init__(self, host='127.0.0.1', port=4877):
 | 
					    def __init__(self, host='127.0.0.1', port=4877):
 | 
				
			||||||
        self.host = host
 | 
					        self.host = host
 | 
				
			||||||
        self.port = port
 | 
					        self.port = port
 | 
				
			||||||
        self.connection = socket.socket()
 | 
					        self.socket = socket.socket()
 | 
				
			||||||
        self.connection.connect((self.host, self.port))
 | 
					        self.socket.connect((self.host, self.port))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def ctrl_connect(self, username, password):
 | 
					    def ctrl_connect(self, username, password):
 | 
				
			||||||
        # INPUT: (STRING username, STRING password)
 | 
					        # INPUT: (STRING username, STRING password)
 | 
				
			||||||
@@ -862,24 +862,24 @@ class Client:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    def __recv_DATA_BYTE(self):
 | 
					    def __recv_DATA_BYTE(self):
 | 
				
			||||||
        while True:
 | 
					        while True:
 | 
				
			||||||
            if (len(self.connection.recv(1, socket.MSG_PEEK)) == 1):
 | 
					            if (len(self.socket.recv(1, socket.MSG_PEEK)) == 1):
 | 
				
			||||||
                byte_data = self.connection.recv(1)
 | 
					                byte_data = self.socket.recv(1)
 | 
				
			||||||
                break
 | 
					                break
 | 
				
			||||||
            sleep(0.1)
 | 
					            sleep(0.1)
 | 
				
			||||||
        return struct.unpack('!B', byte_data)[0]
 | 
					        return struct.unpack('!B', byte_data)[0]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __recv_DATA_BOOL(self):
 | 
					    def __recv_DATA_BOOL(self):
 | 
				
			||||||
        while True:
 | 
					        while True:
 | 
				
			||||||
            if (len(self.connection.recv(1, socket.MSG_PEEK)) == 1):
 | 
					            if (len(self.socket.recv(1, socket.MSG_PEEK)) == 1):
 | 
				
			||||||
                byte_data = self.connection.recv(1)
 | 
					                byte_data = self.socket.recv(1)
 | 
				
			||||||
                break
 | 
					                break
 | 
				
			||||||
            sleep(0.1)
 | 
					            sleep(0.1)
 | 
				
			||||||
        return struct.unpack('!?', byte_data)[0]
 | 
					        return struct.unpack('!?', byte_data)[0]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __recv_DATA_INT(self):
 | 
					    def __recv_DATA_INT(self):
 | 
				
			||||||
        while True:
 | 
					        while True:
 | 
				
			||||||
            if (len(self.connection.recv(4, socket.MSG_PEEK)) == 4):
 | 
					            if (len(self.socket.recv(4, socket.MSG_PEEK)) == 4):
 | 
				
			||||||
                byte_data = self.connection.recv(4)
 | 
					                byte_data = self.socket.recv(4)
 | 
				
			||||||
                break
 | 
					                break
 | 
				
			||||||
            sleep(0.1)
 | 
					            sleep(0.1)
 | 
				
			||||||
        return struct.unpack('!i', byte_data)[0]
 | 
					        return struct.unpack('!i', byte_data)[0]
 | 
				
			||||||
@@ -887,8 +887,8 @@ class Client:
 | 
				
			|||||||
    def __recv_DATA_STRING(self):
 | 
					    def __recv_DATA_STRING(self):
 | 
				
			||||||
        n = self.__recv_WORD()
 | 
					        n = self.__recv_WORD()
 | 
				
			||||||
        while True:
 | 
					        while True:
 | 
				
			||||||
            if (len(self.connection.recv(n, socket.MSG_PEEK)) == n):
 | 
					            if (len(self.socket.recv(n, socket.MSG_PEEK)) == n):
 | 
				
			||||||
                byte_data = self.connection.recv(n)
 | 
					                byte_data = self.socket.recv(n)
 | 
				
			||||||
                break
 | 
					                break
 | 
				
			||||||
            sleep(0.1)
 | 
					            sleep(0.1)
 | 
				
			||||||
        return struct.unpack('!{}s'.format(n), byte_data)[0].decode()
 | 
					        return struct.unpack('!{}s'.format(n), byte_data)[0].decode()
 | 
				
			||||||
@@ -947,29 +947,29 @@ class Client:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    def __recv_WORD(self):
 | 
					    def __recv_WORD(self):
 | 
				
			||||||
        while True:
 | 
					        while True:
 | 
				
			||||||
            if (len(self.connection.recv(2, socket.MSG_PEEK)) == 2):
 | 
					            if (len(self.socket.recv(2, socket.MSG_PEEK)) == 2):
 | 
				
			||||||
                byte_data = self.connection.recv(2)
 | 
					                byte_data = self.socket.recv(2)
 | 
				
			||||||
                break
 | 
					                break
 | 
				
			||||||
            sleep(0.1)
 | 
					            sleep(0.1)
 | 
				
			||||||
        return struct.unpack('!H', byte_data)[0]
 | 
					        return struct.unpack('!H', byte_data)[0]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __send_BYTE(self, byte_data):
 | 
					    def __send_BYTE(self, byte_data):
 | 
				
			||||||
        data = struct.pack('!B', byte_data)
 | 
					        data = struct.pack('!B', byte_data)
 | 
				
			||||||
        self.connection.sendall(data)
 | 
					        self.socket.sendall(data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __send_BOOL(self, bool_data):
 | 
					    def __send_BOOL(self, bool_data):
 | 
				
			||||||
        data = struct.pack('!?', bool_data)
 | 
					        data = struct.pack('!?', bool_data)
 | 
				
			||||||
        self.connection.sendall(data)
 | 
					        self.socket.sendall(data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __send_INT(self, int_data):
 | 
					    def __send_INT(self, int_data):
 | 
				
			||||||
        data = struct.pack('!i', int_data)
 | 
					        data = struct.pack('!i', int_data)
 | 
				
			||||||
        self.connection.sendall(data)
 | 
					        self.socket.sendall(data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __send_STRING(self, string_data):
 | 
					    def __send_STRING(self, string_data):
 | 
				
			||||||
        encoded_string_data = string_data.encode('utf-8')
 | 
					        encoded_string_data = string_data.encode('utf-8')
 | 
				
			||||||
        n = len(encoded_string_data)
 | 
					        n = len(encoded_string_data)
 | 
				
			||||||
        data = struct.pack('!H{}s'.format(n), n, encoded_string_data)
 | 
					        data = struct.pack('!H{}s'.format(n), n, encoded_string_data)
 | 
				
			||||||
        self.connection.sendall(data)
 | 
					        self.socket.sendall(data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __send_INT_LIST(self, int_list_data):
 | 
					    def __send_INT_LIST(self, int_list_data):
 | 
				
			||||||
        n = len(int_list_data)
 | 
					        n = len(int_list_data)
 | 
				
			||||||
@@ -985,4 +985,4 @@ class Client:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    def __send_WORD(self, word_data):
 | 
					    def __send_WORD(self, word_data):
 | 
				
			||||||
        data = struct.pack('!H', word_data)
 | 
					        data = struct.pack('!H', word_data)
 | 
				
			||||||
        self.connection.sendall(data)
 | 
					        self.socket.sendall(data)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -75,12 +75,10 @@ def corpus_analysis_session_handler(app, corpus_id, user_id, session_id):
 | 
				
			|||||||
        corpus = Corpus.query.get(corpus_id)
 | 
					        corpus = Corpus.query.get(corpus_id)
 | 
				
			||||||
        user = User.query.get(user_id)
 | 
					        user = User.query.get(user_id)
 | 
				
			||||||
        if corpus is None:
 | 
					        if corpus is None:
 | 
				
			||||||
            logger.warning("404")
 | 
					 | 
				
			||||||
            response = {'code': 404, 'msg': 'Not Found'}
 | 
					            response = {'code': 404, 'msg': 'Not Found'}
 | 
				
			||||||
            socketio.emit('corpus_analysis_init', response, room=session_id)
 | 
					            socketio.emit('corpus_analysis_init', response, room=session_id)
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
        elif not (corpus.creator == user or user.is_administrator()):
 | 
					        elif not (corpus.creator == user or user.is_administrator()):
 | 
				
			||||||
            logger.warning("403")
 | 
					 | 
				
			||||||
            response = {'code': 403, 'msg': 'Forbidden'}
 | 
					            response = {'code': 403, 'msg': 'Forbidden'}
 | 
				
			||||||
            socketio.emit('corpus_analysis_init', response, room=session_id)
 | 
					            socketio.emit('corpus_analysis_init', response, room=session_id)
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
@@ -88,7 +86,12 @@ def corpus_analysis_session_handler(app, corpus_id, user_id, session_id):
 | 
				
			|||||||
            db.session.refresh(corpus)
 | 
					            db.session.refresh(corpus)
 | 
				
			||||||
            socketio.sleep(3)
 | 
					            socketio.sleep(3)
 | 
				
			||||||
        client = CQiWrapper(host='corpus_{}_analysis'.format(corpus_id))
 | 
					        client = CQiWrapper(host='corpus_{}_analysis'.format(corpus_id))
 | 
				
			||||||
        client.connect()
 | 
					        try:
 | 
				
			||||||
 | 
					            client.connect()
 | 
				
			||||||
 | 
					        except Exception:
 | 
				
			||||||
 | 
					            response = {'code': 500, 'msg': 'Internal Server Error'}
 | 
				
			||||||
 | 
					            socketio.emit('corpus_analysis_init', response, room=session_id)
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
        corpus_analysis_clients[session_id] = client
 | 
					        corpus_analysis_clients[session_id] = client
 | 
				
			||||||
        if corpus_id not in corpus_analysis_sessions:
 | 
					        if corpus_id not in corpus_analysis_sessions:
 | 
				
			||||||
            corpus_analysis_sessions[corpus_id] = [session_id]
 | 
					            corpus_analysis_sessions[corpus_id] = [session_id]
 | 
				
			||||||
@@ -100,7 +103,10 @@ def corpus_analysis_session_handler(app, corpus_id, user_id, session_id):
 | 
				
			|||||||
        while session_id in connected_sessions:
 | 
					        while session_id in connected_sessions:
 | 
				
			||||||
            socketio.sleep(3)
 | 
					            socketio.sleep(3)
 | 
				
			||||||
        ''' Teardown analysis session '''
 | 
					        ''' Teardown analysis session '''
 | 
				
			||||||
        client.disconnect()
 | 
					        try:
 | 
				
			||||||
 | 
					            client.disconnect()
 | 
				
			||||||
 | 
					        except Exception:
 | 
				
			||||||
 | 
					            pass
 | 
				
			||||||
        corpus_analysis_clients.pop(session_id, None)
 | 
					        corpus_analysis_clients.pop(session_id, None)
 | 
				
			||||||
        corpus_analysis_sessions[corpus_id].remove(session_id)
 | 
					        corpus_analysis_sessions[corpus_id].remove(session_id)
 | 
				
			||||||
        if not corpus_analysis_sessions[corpus_id]:
 | 
					        if not corpus_analysis_sessions[corpus_id]:
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user