Fix some issues, improve documentation
This commit is contained in:
@@ -75,7 +75,7 @@ class AbstractCommand(ABC):
|
||||
self._process_conditional_headers()
|
||||
|
||||
message = f"HTTP/1.1 {status} {status_message[status]}\r\n"
|
||||
message += self._get_date() + "\r\n"
|
||||
message += f"Date: {self._get_date()}\r\n"
|
||||
|
||||
content_length = len(body)
|
||||
message += f"Content-Length: {content_length}\r\n"
|
||||
@@ -107,7 +107,7 @@ class AbstractCommand(ABC):
|
||||
path = root + norm_path
|
||||
|
||||
if check and not os.path.exists(path):
|
||||
raise NotFound()
|
||||
raise NotFound(path)
|
||||
|
||||
return path
|
||||
|
||||
@@ -131,7 +131,7 @@ class AbstractCommand(ABC):
|
||||
return True
|
||||
|
||||
if modified <= min_date:
|
||||
raise NotModified()
|
||||
raise NotModified(f"{modified} <= {min_date}")
|
||||
|
||||
return True
|
||||
|
||||
@@ -149,11 +149,11 @@ class AbstractModifyCommand(AbstractCommand, ABC):
|
||||
|
||||
def execute(self):
|
||||
path = self._get_path(False)
|
||||
dir = os.path.dirname(path)
|
||||
directory = os.path.dirname(path)
|
||||
|
||||
if not os.path.exists(dir):
|
||||
if not os.path.exists(directory):
|
||||
raise Forbidden("Target directory does not exists!")
|
||||
if os.path.exists(dir) and not os.path.isdir(dir):
|
||||
if os.path.exists(directory) and not os.path.isdir(directory):
|
||||
raise Forbidden("Target directory is an existing file!")
|
||||
|
||||
exists = os.path.exists(path)
|
||||
|
@@ -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))
|
||||
|
@@ -15,4 +15,4 @@ class ServerSocket(HTTPSocket):
|
||||
try:
|
||||
return super().read_line()
|
||||
except UnicodeDecodeError:
|
||||
raise BadRequest()
|
||||
raise BadRequest("UnicodeDecodeError")
|
||||
|
@@ -67,17 +67,20 @@ class Worker:
|
||||
handler = RequestHandler(conn, self.host)
|
||||
handler.listen()
|
||||
except HTTPServerCloseException as e:
|
||||
logging.debug("HTTP Exception:", exc_info=e)
|
||||
logging.warning("[HTTP: %s] %s. Reason: %s", e.status_code, e.message, e.arg)
|
||||
RequestHandler.send_error(conn, e.status_code, e.message)
|
||||
break
|
||||
except HTTPServerException as e:
|
||||
logging.debug("HTTP Exception:", exc_info=e)
|
||||
logging.debug("[HTTP: %s] %s. Reason: %s", e.status_code, e.message, e.arg)
|
||||
RequestHandler.send_error(conn, e.status_code, e.message)
|
||||
except socket.timeout:
|
||||
logging.debug("Socket for client %s timed out", addr)
|
||||
logging.info("Socket for client %s timed out.", addr)
|
||||
break
|
||||
except ConnectionAbortedError:
|
||||
logging.info("Socket for client %s disconnected.", addr)
|
||||
break
|
||||
except Exception as e:
|
||||
logging.debug("Internal error", exc_info=e)
|
||||
logging.error("Internal error", exc_info=e)
|
||||
RequestHandler.send_error(conn, InternalServerError.status_code, InternalServerError.message)
|
||||
break
|
||||
|
||||
|
Reference in New Issue
Block a user