fix(client): regex no longer case sensitive

- server: change request blank filename check
This commit is contained in:
minhtrannhat 2023-12-17 11:27:33 -05:00
parent 9a2703b024
commit a5debc437a
2 changed files with 42 additions and 18 deletions

View File

@ -11,12 +11,15 @@ import os
import re import re
# patterns for command matchings # Patterns for command matchings
# compiled for extra performance # - compiled for extra performance
get_command_pattern: Pattern = re.compile(r"^get\s+[^\s]+$") # - Ignore case, get or GET both works
put_command_pattern: Pattern = re.compile(r"^put\s+[^\s]+$") get_command_pattern: Pattern = re.compile(r"^get\s+[^\s]+$", re.IGNORECASE)
summary_command_pattern: Pattern = re.compile(r"^summary\s+[^\s]+$") put_command_pattern: Pattern = re.compile(r"^put\s+[^\s]+$", re.IGNORECASE)
change_command_pattern: Pattern = re.compile(r"^change\s+[^\s]+\s+[^\s]+$") summary_command_pattern: Pattern = re.compile(r"^summary\s+[^\s]+$", re.IGNORECASE)
change_command_pattern: Pattern = re.compile(
r"^change\s+[^\s]+\s+[^\s]+$", re.IGNORECASE
)
# opcodes # opcodes
put_request_opcode: int = 0b000 put_request_opcode: int = 0b000
@ -64,23 +67,29 @@ class Client:
self.client_socket.settimeout(10) self.client_socket.settimeout(10)
# only if using TCP # only if using TCP
self.client_socket.connect( try:
(self.server_name, self.server_port) self.client_socket.connect(
) if self.protocol == "TCP" else None (self.server_name, self.server_port)
) if self.protocol == "TCP" else None
except ConnectionRefusedError:
print(
f"myftp> - {self.protocol} - ConnectionRefusedError happened. Please restart the client program, make sure the server is running and/or put a different server name and server port."
)
return
try: try:
while True: while True:
# get command from user # get command from user
command = input(f"myftp> - {self.protocol} - : ").strip().lower() command = input(f"myftp> - {self.protocol} - : ").strip()
# handling the "bye" command # handling the "bye" command
if command == "bye": if command == "bye" or command == "BYE":
self.client_socket.close() self.client_socket.close()
print(f"myftp> - {self.protocol} - Session is terminated") print(f"myftp> - {self.protocol} - Session is terminated")
break break
# help # help
elif command == "help": elif command == "help" or command == "HELP":
first_byte: int = help_request_opcode << 5 first_byte: int = help_request_opcode << 5
command_name = "help" command_name = "help"
@ -90,7 +99,8 @@ class Client:
# get command handling # get command handling
elif get_command_pattern.match(command): elif get_command_pattern.match(command):
command_name, filename = command.split(" ", 1) _, filename = command.split(" ", 1)
command_name = "get"
first_byte = (get_request_opcode << 5) + len(filename) first_byte = (get_request_opcode << 5) + len(filename)
@ -102,7 +112,8 @@ class Client:
# put command handling # put command handling
elif put_command_pattern.match(command): elif put_command_pattern.match(command):
command_name, filename = command.split(" ", 1) _, filename = command.split(" ", 1)
command_name = "put"
first_byte, second_byte_to_n_byte, data = self.put_payload_handling( first_byte, second_byte_to_n_byte, data = self.put_payload_handling(
filename filename
@ -114,7 +125,9 @@ class Client:
# summary command handling # summary command handling
elif summary_command_pattern.match(command): elif summary_command_pattern.match(command):
command_name, filename = command.split(" ", 1) _, filename = command.split(" ", 1)
command_name = "summary"
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
@ -125,7 +138,8 @@ class Client:
# change command handling # change command handling
elif change_command_pattern.match(command): elif change_command_pattern.match(command):
command_name, old_filename, new_filename = command.split() _, old_filename, new_filename = command.split()
command_name = "change"
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"
@ -168,7 +182,9 @@ class Client:
) if self.debug else None ) if self.debug else None
if self.protocol == "UDP": if self.protocol == "UDP":
self.client_socket.sendto(payload, (self.server_name, self.server_port)) # type: ignore self.client_socket.sendto(
payload, (self.server_name, self.server_port)
) # type: ignore
else: else:
self.client_socket.sendall(payload) # type: ignore self.client_socket.sendall(payload) # type: ignore
@ -187,6 +203,9 @@ class Client:
f"myftp> - {self.protocol} - Server at {self.server_name} did not respond within 5 seconds. Check the address or server status." f"myftp> - {self.protocol} - Server at {self.server_name} did not respond within 5 seconds. Check the address or server status."
) )
except KeyboardInterrupt:
print(f"\nmyftp> - {self.protocol} - Client shutting down")
except Exception as error: except Exception as error:
traceback_info = traceback.format_exc() traceback_info = traceback.format_exc()

View File

@ -70,7 +70,7 @@ class Server:
client_socket, clientAddress = server_socket.accept() client_socket, clientAddress = server_socket.accept()
print( print(
f"myftp> - {self.protocol} Connected to TCP client at {clientAddress}" f"myftp> - {self.protocol} - Connected to TCP client at {clientAddress}"
) if self.debug else None ) if self.debug else None
while not shut_down: while not shut_down:
@ -373,6 +373,7 @@ class Server:
If not, return None, None, None tuple If not, return None, None, None tuple
""" """
filename = second_byte_to_byte_n.decode("ascii") filename = second_byte_to_byte_n.decode("ascii")
print(f"myftp> - {self.protocol} - trying to find file {filename}")
try: try:
with open(os.path.join(self.directory_path, filename), "rb") as file: with open(os.path.join(self.directory_path, filename), "rb") as file:
@ -385,6 +386,10 @@ class Server:
print(f"myftp> - {self.protocol} - file {filename} not found") print(f"myftp> - {self.protocol} - file {filename} not found")
return (None, None, None) return (None, None, None)
except IsADirectoryError:
print(f"myftp> - {self.protocol} - filename is blank")
return (None, None, None)
# assembling the payload to send back to the client # assembling the payload to send back to the client
def build_res_payload( def build_res_payload(
self, self,