small fixes

This commit is contained in:
2021-03-27 17:32:16 +01:00
parent ff32ce9b39
commit 0f7d67c98d
2 changed files with 14 additions and 5 deletions

View File

@@ -62,9 +62,10 @@ class ResponseHandler(ABC):
class BasicResponseHandler(ResponseHandler): class BasicResponseHandler(ResponseHandler):
""" Response handler which throws away the body and only shows the headers. """
Response handler which throws away the body and only shows the headers.
In case of a redirect, it will process it and pass it to the appropriate response handler. In case of a redirect, it will process it and pass it to the appropriate response handler.
""" """
def __init__(self, client: HTTPClient, msg: Message, cmd: AbstractCommand): def __init__(self, client: HTTPClient, msg: Message, cmd: AbstractCommand):
retriever = Retriever.create(client, msg.headers) retriever = Retriever.create(client, msg.headers)
@@ -105,10 +106,15 @@ class BasicResponseHandler(ResponseHandler):
def _do_handle_redirect(self): def _do_handle_redirect(self):
self._skip_body() self._skip_body()
if self.msg.status == 304:
print("".join(self.msg.raw), end="")
return None
location = self.msg.headers.get("location") location = self.msg.headers.get("location")
if not location: if not location or len(location.strip()) == 0:
raise InvalidResponse("No location in redirect") raise InvalidResponse("No location in redirect")
location = parser.urljoin(self.cmd.uri, location)
parsed_location = urlsplit(location) parsed_location = urlsplit(location)
if not parsed_location.hostname: if not parsed_location.hostname:
raise InvalidResponse("Invalid location") raise InvalidResponse("Invalid location")

View File

@@ -7,6 +7,7 @@ from time import mktime
from wsgiref.handlers import format_date_time from wsgiref.handlers import format_date_time
from client.httpclient import FORMAT from client.httpclient import FORMAT
from httplib import parser
from httplib.exceptions import NotFound, Forbidden, NotModified from httplib.exceptions import NotFound, Forbidden, NotModified
from httplib.message import ServerMessage as Message from httplib.message import ServerMessage as Message
@@ -16,6 +17,7 @@ status_message = {
200: "OK", 200: "OK",
201: "Created", 201: "Created",
202: "Accepted", 202: "Accepted",
204: "No Content",
304: "Not Modified", 304: "Not Modified",
400: "Bad Request", 400: "Bad Request",
404: "Not Found", 404: "Not Found",
@@ -56,7 +58,7 @@ class AbstractCommand(ABC):
def _get_date(self): def _get_date(self):
""" """
Returns a string representation of the current date according to RFC 1123 Returns a string representation of the current date according to RFC 1123.
""" """
now = datetime.now() now = datetime.now()
stamp = mktime(now.timetuple()) stamp = mktime(now.timetuple())
@@ -167,7 +169,8 @@ class AbstractModifyCommand(AbstractCommand, ABC):
else: else:
status = 201 status = 201
return self._build_message(status, None, ) location = parser.urljoin("/", os.path.relpath(path, root))
return self._build_message(status, "text/plain", b"", {"Location": location})
class HeadCommand(AbstractCommand): class HeadCommand(AbstractCommand):