-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_payload_parser.py
100 lines (79 loc) · 4.1 KB
/
request_payload_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from socket import socket
from bytes_buffer import SocketBytesBuffer
from constants import *
from socket_helper import SockHelper
class RequestPayloadParser:
"""
The RequestPayloadParser parses the payload according to the request code
"""
def __init__(self, request_code, sock: socket, payload_size: int):
super().__init__()
self.__request_code = request_code
self.__sock = sock
self.__payload_size = payload_size
self.__helper = SockHelper(self.__sock)
def parse(self):
if self.__payload_size > MAX_PAYLOAD_BYTES:
raise Exception("Message payload size limit reached. got={}".format(self.__payload_size))
if self.__request_code == REQUEST_CODE_REGISTER:
return self.__parse_register_request_params()
elif self.__request_code == REQUEST_CODE_GET_CLIENTS_LIST:
return self.__parse_get_clients_req_params()
elif self.__request_code == REQUEST_CODE_GET_CLIENT_PUBLIC_KEY:
return self.__parse_get_client_public_key_req_params()
elif self.__request_code == REQUEST_CODE_SEND_MESSAGE:
return self.__parse_send_message_req_params()
elif self.__request_code == REQUEST_CODE_GET_MESSAGES:
return self.__parse_get_messages_req_params()
else:
raise Exception("Got unknown request code. got={}".format(self.__request_code))
def __parse_register_request_params(self):
if not self.__payload_size == STR_SIZE + PUBLIC_KEY_SIZE:
raise Exception("Incorrect payload size. got={}".format(self.__payload_size))
client_name = self.__helper.read_str(STR_SIZE)
client_pk = self.__helper.read_bytes(PUBLIC_KEY_SIZE)
return {
REQUEST_PAYLOAD_CLIENT_NAME: client_name,
REQUEST_PAYLOAD_PUBLIC_KEY: client_pk
}
def __parse_get_clients_req_params(self):
if not self.__payload_size == 0:
raise Exception("Incorrect payload size. got={}".format(self.__payload_size))
return {}
def __parse_get_client_public_key_req_params(self):
if not self.__payload_size == CLIENT_ID_SIZE:
raise Exception("Incorrect payload size. got={}".format(self.__payload_size))
client_id = self.__helper.read_bytes(CLIENT_ID_SIZE)
return {
REQUEST_PAYLOAD_CLIENT_ID: client_id
}
def __parse_send_message_req_params(self):
if self.__payload_size < CLIENT_ID_SIZE + MESSAGE_LENGTH_SIZE + MESSAGE_TYPE_SIZE:
raise Exception("Incorrect payload size. got={}".format(self.__payload_size))
client_id = self.__helper.read_bytes(CLIENT_ID_SIZE)
message_type = self.__helper.read_byte()
message_size = self.__helper.read_int()
if message_size > MAX_MSG_BODY_BYTES:
raise Exception("Max message body size reached. got={}".format(message_size))
if not self.__payload_size == message_size + CLIENT_ID_SIZE + MESSAGE_LENGTH_SIZE + MESSAGE_TYPE_SIZE:
raise Exception("Incorrect payload size. does not match message size. got={}".format(message_size))
if message_type == MESSAGE_TYPE_ENC_KEY_REQUEST:
if not message_size == 0:
raise Exception("Incorrect message size. should be 0. instead got {}".format(message_size))
else:
message_body = bytes()
elif message_type == MESSAGE_TYPE_ENC_KEY_RESP or message_type == MESSAGE_TYPE_TEXT_MESSAGE:
message_body = self.__helper.read_bytes(message_size)
elif message_type == MESSAGE_TYPE_FILE_MESSAGE:
message_body = SocketBytesBuffer(self.__sock, message_size)
else:
raise Exception("Unknown message type. got={}".format(message_type))
return {
REQUEST_PAYLOAD_CLIENT_ID: client_id,
REQUEST_PAYLOAD_MESSAGE_TYPE: message_type,
REQUEST_PAYLOAD_MESSAGE_BODY: message_body
}
def __parse_get_messages_req_params(self):
if not self.__payload_size == 0:
raise Exception("Incorrect payload size. got={}".format(self.__payload_size))
return {}