From 7ecfedbec72149592cdce45fe97d2d61dba8ef2f Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Sun, 28 Mar 2021 03:00:04 +0200 Subject: [PATCH] Command add properties for fields --- client/command.py | 33 +++++++++++++++++++++++++++------ client/response_handler.py | 8 ++++---- httplib/parser.py | 4 ++-- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/client/command.py b/client/command.py index 3df33e8..70de195 100644 --- a/client/command.py +++ b/client/command.py @@ -38,18 +38,38 @@ class AbstractCommand(ABC): """ A class representing the command for sending an HTTP request. """ - uri: str - host: str - path: str - port: int + _uri: str + _host: str + _path: str + _port: int sub_request: bool def __init__(self, uri: str, port): self.uri = uri - self.host, _, self.path = parser.parse_uri(uri) - self.port = int(port) + self._port = int(port) self.sub_request = False + @property + def uri(self): + return self._uri + + @uri.setter + def uri(self, value): + self._uri = value + self._host, self._port, self._path = parser.parse_uri(value) + + @property + def host(self): + return self._host + + @property + def path(self): + return self._path + + @property + def port(self): + return self._port + @property @abstractmethod def method(self): @@ -61,6 +81,7 @@ class AbstractCommand(ABC): @param sub_request: If this execution is in function of a prior command. """ + self.uri = "" self.sub_request = sub_request (host, path) = self.parse_uri() diff --git a/client/response_handler.py b/client/response_handler.py index 4bd8ddc..69343b3 100644 --- a/client/response_handler.py +++ b/client/response_handler.py @@ -78,7 +78,7 @@ class BasicResponseHandler(ResponseHandler): if self.msg.status == 101: # Switching protocols is not supported print("".join(self.msg.raw), end="") - return + return None if 200 <= self.msg.status < 300: return self.retriever @@ -87,6 +87,7 @@ class BasicResponseHandler(ResponseHandler): # Redirect self._skip_body() return self._handle_redirect() + if 400 <= self.msg.status < 600: self._skip_body() # Dump headers and exit with error @@ -114,7 +115,6 @@ class BasicResponseHandler(ResponseHandler): raise InvalidResponse("Only http is supported") self.cmd.uri = location - self.cmd.host, self.cmd.port, self.cmd.path = parser.parse_uri(location) if self.msg.status == 301: logging.info("Status 301. Closing socket [%s]", self.cmd.host) @@ -177,8 +177,8 @@ class DownloadHandler(ResponseHandler, ABC): class RawDownloadHandler(DownloadHandler): - def __init__(self, retriever: Retriever, client: HTTPClient, msg: Message, cmd: AbstractCommand, dir=None): - super().__init__(retriever, client, msg, cmd, dir) + def __init__(self, retriever: Retriever, client: HTTPClient, msg: Message, cmd: AbstractCommand, directory=None): + super().__init__(retriever, client, msg, cmd, directory) def handle(self) -> str: logging.debug("Retrieving payload") diff --git a/httplib/parser.py b/httplib/parser.py index 661395c..4e8819d 100644 --- a/httplib/parser.py +++ b/httplib/parser.py @@ -78,7 +78,7 @@ def parse_request_line(line: str): raise BadRequest(f"Invalid HTTP-version: {version}") if len(target) == "": - raise BadRequest() + raise BadRequest("request-target not specified") parsed_target = urlsplit(target) return method, parsed_target, version.split("/")[1] @@ -174,7 +174,7 @@ def get_uri(url: str): def urljoin(base, url): """ - Join a base url and a URL to form a absolute url. + Join a base url and a URL to form an absolute url. """ return urllib.parse.urljoin(base, url)