feat(udp): unknown command completed

This commit is contained in:
minhtrannhat 2023-12-06 23:03:36 -05:00
parent 9dbb13b277
commit ae1d34da2a
Signed by: minhtrannhat
GPG Key ID: E13CFA85C53F8062
2 changed files with 26 additions and 13 deletions

View File

@ -24,6 +24,7 @@ get_request_opcode: int = 0b001
change_request_opcode: int = 0b010 change_request_opcode: int = 0b010
summary_request_opcode: int = 0b011 summary_request_opcode: int = 0b011
help_request_opcode: int = 0b100 help_request_opcode: int = 0b100
unknown_request_opcode: int = 0b101
# Res-code dict # Res-code dict
rescode_dict: dict[int, str] = { rescode_dict: dict[int, str] = {
@ -94,44 +95,49 @@ class Client:
# put command handling # put command handling
elif put_command_pattern.match(command): elif put_command_pattern.match(command):
_, filename = command.split(" ", 1) command_name, filename = command.split(" ", 1)
print( print(
f"myftp> - {self.protocol} - Putting file {filename} into the server" f"myftp> - {self.protocol} - Putting file {filename} into the server"
) if self.debug else None ) if self.debug else None
# summary command handling # summary command handling
elif summary_command_pattern.match(command): elif summary_command_pattern.match(command):
_, filename = command.split(" ", 1) command_name, filename = command.split(" ", 1)
print( print(
f"myftp> - {self.protocol} - Summary file {filename} from the server" f"myftp> - {self.protocol} - Summary file {filename} from the server"
) if self.debug else None ) if self.debug else None
# change command handling # change command handling
elif change_command_pattern.match(command): elif change_command_pattern.match(command):
_, old_filename, new_filename = command.split() command_name, old_filename, new_filename = command.split()
print( print(
f"myftp> - {self.protocol} - Changing file named {old_filename} into {new_filename} on the server" f"myftp> - {self.protocol} - Changing file named {old_filename} into {new_filename} on the server"
) if self.debug else None ) if self.debug else None
# unknown request, assigned opcode is 0b101
else: else:
print( command_name = None
f"myftp> - {self.protocol} - Invalid command. Supported commands are put, get, summary, change, list and help. Type help for detailed usage." first_byte: int = unknown_request_opcode << 5
)
continue
# get or put case # get or put case
if command_name == "get" or command_name == "put": if command_name == "get" or command_name == "put":
payload = first_byte.to_bytes(1, "big") + second_byte_to_n_byte payload = first_byte.to_bytes(1, "big") + second_byte_to_n_byte # type: ignore
# help case elif command_name == "summary":
pass
elif command == "change":
pass
# help case and unknown request
else: else:
payload: bytes = first_byte.to_bytes(1, "big") payload: bytes = first_byte.to_bytes(1, "big") # type: ignore
print( print(
f"myftp> - {self.protocol} - sent payload {bin(int.from_bytes(payload, byteorder='big'))[2:]} to the server" f"myftp> - {self.protocol} - sent payload {bin(int.from_bytes(payload, byteorder='big'))[2:]} to the server" # type: ignore
) if self.debug else None ) if self.debug else None
client_socket.sendto(payload, (self.server_name, self.server_port)) client_socket.sendto(payload, (self.server_name, self.server_port)) # type: ignore
response_payload = client_socket.recv(2048) response_payload = client_socket.recv(2048)

View File

@ -29,6 +29,7 @@ op_codes_dict: dict[int, str] = {
0b010: "change", 0b010: "change",
0b011: "summary", 0b011: "summary",
0b100: "help", 0b100: "help",
0b101: "unknown",
} }
@ -103,6 +104,12 @@ class Server:
filename = None filename = None
response_data = None response_data = None
elif request_type == "unknown":
rescode = rescode_fail_dict["unknown_request_rescode"]
filename_length = None
filename = None
response_data = None
res_payload: bytes = self.build_res_payload( res_payload: bytes = self.build_res_payload(
rescode=rescode, # type: ignore rescode=rescode, # type: ignore
filename_length=filename_length, filename_length=filename_length,
@ -143,7 +150,7 @@ class Server:
) )
except KeyError: except KeyError:
raise KeyError("Can't find the request type of this payload") raise KeyError("Cant not find the request type")
return request_type, filename_length_in_bytes return request_type, filename_length_in_bytes