small update

This commit is contained in:
2021-03-28 00:35:58 +01:00
parent 5b5a821522
commit a3ce68330f
2 changed files with 27 additions and 19 deletions

View File

@@ -89,7 +89,7 @@ class AbstractCommand(ABC):
if content_type:
message += f"Content-Type: {content_type}"
if content_type.startswith("text"):
message += "; charset=UTF-8"
message += f"; charset={FORMAT}"
message += "\r\n"
elif content_length > 0:
message += f"Content-Type: application/octet-stream\r\n"
@@ -141,6 +141,20 @@ class AbstractCommand(ABC):
return True
def get_mimetype(self, path):
mime = mimetypes.guess_type(path)[0]
if mime:
return mime
try:
file = open(path, "r", encoding=FORMAT)
file.readline()
file.close()
return "text/plain"
except UnicodeDecodeError:
return "application/octet-stream"
class AbstractModifyCommand(AbstractCommand, ABC):
@@ -191,7 +205,7 @@ class HeadCommand(AbstractCommand):
def execute(self):
path = self._get_path()
mime = mimetypes.guess_type(path)[0]
mime = self.get_mimetype(path)
return self._build_message(200, mime, b"")
@@ -204,20 +218,6 @@ class GetCommand(AbstractCommand):
def _conditional_headers(self):
return {'if-modified-since': self._if_modified_since}
def get_mimetype(self, path):
mime = mimetypes.guess_type(path)[0]
if mime:
return mime
try:
file = open(path, "r", encoding="utf-8")
file.readline()
file.close()
return "text/plain"
except UnicodeDecodeError:
return "application/octet-stream"
def execute(self):
path = self._get_path()
mime = self.get_mimetype(path)