Parse html with regex, fix small issues

This commit is contained in:
2021-03-27 23:41:28 +01:00
parent bbca6f603b
commit 4473d1bec9
7 changed files with 134 additions and 80 deletions

View File

@@ -6,12 +6,12 @@ from datetime import datetime
from time import mktime
from wsgiref.handlers import format_date_time
from client.httpclient import FORMAT
from httplib import parser
from httplib.exceptions import NotFound, Forbidden, NotModified
from httplib.httpsocket import FORMAT
from httplib.message import ServerMessage as Message
root = os.path.join(os.path.dirname(sys.argv[0]), "public")
CONTENT_ROOT = os.path.join(os.path.dirname(sys.argv[0]), "public")
status_message = {
200: "OK",
@@ -26,6 +26,12 @@ status_message = {
def create(message: Message):
"""
Creates a Command based on the specified message
@param message: the message to create the Command with.
@return: An instance of `AbstractCommand`
"""
if message.method == "GET":
return GetCommand(message)
elif message.method == "HEAD":
@@ -102,9 +108,9 @@ class AbstractCommand(ABC):
norm_path = os.path.normpath(self.msg.target.path)
if norm_path == "/":
path = root + "/index.html"
path = CONTENT_ROOT + "/index.html"
else:
path = root + norm_path
path = CONTENT_ROOT + norm_path
if check and not os.path.exists(path):
raise NotFound(path)
@@ -169,7 +175,7 @@ class AbstractModifyCommand(AbstractCommand, ABC):
else:
status = 201
location = parser.urljoin("/", os.path.relpath(path, root))
location = parser.urljoin("/", os.path.relpath(path, CONTENT_ROOT))
return self._build_message(status, "text/plain", b"", {"Location": location})