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

@@ -57,7 +57,7 @@ class RequestHandler:
retriever = Retriever.create(self.conn, headers)
except UnsupportedEncoding as e:
logging.error("Encoding not supported: %s=%s", e.enc_type, e.encoding)
raise NotImplemented()
raise NotImplemented(f"{e.enc_type}={e.encoding}")
for buffer in retriever.retrieve():
body += buffer
@@ -68,7 +68,7 @@ class RequestHandler:
cmd = command.create(message)
msg = cmd.execute()
logging.debug("---response begin---\r\n%s---response end---", msg)
logging.debug("---response begin---\r\n%s\r\n---response end---", msg.split(b"\r\n\r\n", 1)[0].decode(FORMAT))
self.conn.conn.sendall(msg)
def _check_request_line(self, method: str, target: Union[ParseResultBytes, ParseResult], version):
@@ -77,22 +77,22 @@ class RequestHandler:
raise MethodNotAllowed(METHODS)
if version not in ("1.0", "1.1"):
raise HTTPVersionNotSupported()
raise HTTPVersionNotSupported(version)
# only origin-form and absolute-form are allowed
if target.scheme not in ("", "http"):
# Only http is supported...
raise BadRequest()
raise BadRequest(f"scheme={target.scheme}")
if target.netloc != "" and target.netloc != self.conn.host and target.netloc != self.conn.host.split(":")[0]:
raise NotFound()
raise NotFound(str(target))
if target.path == "" or target.path[0] != "/":
raise NotFound()
raise NotFound(str(target))
def _validate_request(self, msg):
if msg.version == "1.1" and "host" not in msg.headers:
raise BadRequest()
raise BadRequest("Missing host header")
self._check_request_line(msg.method, msg.target, msg.version)
@@ -119,5 +119,5 @@ class RequestHandler:
message += "Content-Length: 0\r\n"
message += "\r\n"
logging.debug("Sending: %r", message)
logging.debug("---response begin---\r\n%s---response end---", message)
client.sendall(message.encode(FORMAT))