Fix some issues, improve documentation

This commit is contained in:
2021-03-27 19:05:09 +01:00
parent 0f7d67c98d
commit 9036755a62
8 changed files with 89 additions and 48 deletions

View File

@@ -41,11 +41,13 @@ class AbstractCommand(ABC):
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.sub_request = False
@property
@abstractmethod
@@ -53,6 +55,7 @@ class AbstractCommand(ABC):
pass
def execute(self, sub_request=False):
self.sub_request = sub_request
(host, path) = self.parse_uri()
client = sockets.get(host)
@@ -169,9 +172,10 @@ class GetCommand(AbstractCommand):
(version, status, msg) = parser.parse_status_line(next(lines))
headers = parser.parse_headers(lines)
logging.debug("---response begin---\r\n%s---response end---", "".join(retriever.buffer))
buffer = retriever.buffer
logging.debug("---response begin---\r\n%s---response end---", "".join(buffer))
return Message(version, status, msg, headers, retriever.buffer)
return Message(version, status, msg, headers, buffer)
def _await_response(self, client, retriever):
msg = self._get_preamble(retriever)

View File

@@ -46,20 +46,6 @@ class ResponseHandler(ABC):
def handle(self):
pass
@staticmethod
def parse_uri(uri: str):
parsed = urlsplit(uri)
# If there is no netloc, the url is invalid, so prepend `//` and try again
if parsed.netloc == "":
parsed = urlsplit("//" + uri)
host = parsed.netloc
path = parsed.path
if len(path) == 0 or path[0] != '/':
path = "/" + path
return host, path
class BasicResponseHandler(ResponseHandler):
"""
@@ -98,11 +84,14 @@ class BasicResponseHandler(ResponseHandler):
if 300 <= self.msg.status < 400:
# Redirect
return self._do_handle_redirect()
if 400 <= self.msg.status < 500:
if 400 <= self.msg.status < 600:
# Dump headers and exit with error
print("".join(self.msg.raw), end="")
if not self.cmd.sub_request:
print("".join(self.msg.raw), end="")
return None
return None
def _do_handle_redirect(self):
self._skip_body()