diff --git a/client/response_handler.py b/client/response_handler.py index d725bef..ab5ae5f 100644 --- a/client/response_handler.py +++ b/client/response_handler.py @@ -62,9 +62,10 @@ class ResponseHandler(ABC): 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. - """ + """ def __init__(self, client: HTTPClient, msg: Message, cmd: AbstractCommand): retriever = Retriever.create(client, msg.headers) @@ -105,10 +106,15 @@ class BasicResponseHandler(ResponseHandler): def _do_handle_redirect(self): self._skip_body() + if self.msg.status == 304: + print("".join(self.msg.raw), end="") + return None + location = self.msg.headers.get("location") - if not location: + if not location or len(location.strip()) == 0: raise InvalidResponse("No location in redirect") + location = parser.urljoin(self.cmd.uri, location) parsed_location = urlsplit(location) if not parsed_location.hostname: raise InvalidResponse("Invalid location") diff --git a/server/command.py b/server/command.py index 185d068..64089cf 100644 --- a/server/command.py +++ b/server/command.py @@ -7,6 +7,7 @@ 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.message import ServerMessage as Message @@ -16,6 +17,7 @@ status_message = { 200: "OK", 201: "Created", 202: "Accepted", + 204: "No Content", 304: "Not Modified", 400: "Bad Request", 404: "Not Found", @@ -56,7 +58,7 @@ class AbstractCommand(ABC): 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() stamp = mktime(now.timetuple()) @@ -167,7 +169,8 @@ class AbstractModifyCommand(AbstractCommand, ABC): else: 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):