Fix some issues, improve documentation
This commit is contained in:
@@ -41,11 +41,13 @@ class AbstractCommand(ABC):
|
|||||||
host: str
|
host: str
|
||||||
path: str
|
path: str
|
||||||
port: int
|
port: int
|
||||||
|
sub_request: bool
|
||||||
|
|
||||||
def __init__(self, uri: str, port):
|
def __init__(self, uri: str, port):
|
||||||
self.uri = uri
|
self.uri = uri
|
||||||
self.host, _, self.path = parser.parse_uri(uri)
|
self.host, _, self.path = parser.parse_uri(uri)
|
||||||
self.port = int(port)
|
self.port = int(port)
|
||||||
|
self.sub_request = False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
@@ -53,6 +55,7 @@ class AbstractCommand(ABC):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def execute(self, sub_request=False):
|
def execute(self, sub_request=False):
|
||||||
|
self.sub_request = sub_request
|
||||||
(host, path) = self.parse_uri()
|
(host, path) = self.parse_uri()
|
||||||
|
|
||||||
client = sockets.get(host)
|
client = sockets.get(host)
|
||||||
@@ -169,9 +172,10 @@ class GetCommand(AbstractCommand):
|
|||||||
(version, status, msg) = parser.parse_status_line(next(lines))
|
(version, status, msg) = parser.parse_status_line(next(lines))
|
||||||
headers = parser.parse_headers(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):
|
def _await_response(self, client, retriever):
|
||||||
msg = self._get_preamble(retriever)
|
msg = self._get_preamble(retriever)
|
||||||
|
@@ -46,20 +46,6 @@ class ResponseHandler(ABC):
|
|||||||
def handle(self):
|
def handle(self):
|
||||||
pass
|
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):
|
class BasicResponseHandler(ResponseHandler):
|
||||||
"""
|
"""
|
||||||
@@ -98,11 +84,14 @@ class BasicResponseHandler(ResponseHandler):
|
|||||||
if 300 <= self.msg.status < 400:
|
if 300 <= self.msg.status < 400:
|
||||||
# Redirect
|
# Redirect
|
||||||
return self._do_handle_redirect()
|
return self._do_handle_redirect()
|
||||||
if 400 <= self.msg.status < 500:
|
if 400 <= self.msg.status < 600:
|
||||||
# Dump headers and exit with error
|
# Dump headers and exit with error
|
||||||
|
if not self.cmd.sub_request:
|
||||||
print("".join(self.msg.raw), end="")
|
print("".join(self.msg.raw), end="")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
def _do_handle_redirect(self):
|
def _do_handle_redirect(self):
|
||||||
self._skip_body()
|
self._skip_body()
|
||||||
|
|
||||||
|
@@ -34,8 +34,10 @@ class HTTPServerException(Exception):
|
|||||||
status_code: str
|
status_code: str
|
||||||
message: str
|
message: str
|
||||||
body: str
|
body: str
|
||||||
|
arg: str
|
||||||
|
|
||||||
def __init__(self, body=""):
|
def __init__(self, arg, body=""):
|
||||||
|
self.arg = arg
|
||||||
self.body = body
|
self.body = body
|
||||||
|
|
||||||
|
|
||||||
|
@@ -7,6 +7,9 @@ from httplib.httpsocket import HTTPSocket, BUFSIZE
|
|||||||
|
|
||||||
|
|
||||||
class Retriever(ABC):
|
class Retriever(ABC):
|
||||||
|
"""
|
||||||
|
This is a helper class for retrieving HTTP messages.
|
||||||
|
"""
|
||||||
client: HTTPSocket
|
client: HTTPSocket
|
||||||
|
|
||||||
def __init__(self, client: HTTPSocket):
|
def __init__(self, client: HTTPSocket):
|
||||||
@@ -14,10 +17,23 @@ class Retriever(ABC):
|
|||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def retrieve(self):
|
def retrieve(self):
|
||||||
|
"""
|
||||||
|
Creates an iterator of the retrieved message content.
|
||||||
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create(client: HTTPSocket, headers: Dict[str, str]):
|
def create(client: HTTPSocket, headers: Dict[str, str]):
|
||||||
|
"""
|
||||||
|
Creates a Retriever instance depending on the give headers.
|
||||||
|
|
||||||
|
@param client: the socket to retrieve from
|
||||||
|
@param headers: the message headers for choosing the retriever instance
|
||||||
|
@return: ChunkedRetriever if the message uses chunked encoding, ContentLengthRetriever if the message
|
||||||
|
specifies a content-length, RawRetriever if none of the above is True.
|
||||||
|
@raise UnsupportedEncoding: if the `transfer-encoding` is not supported or if the `content-encoding` is not
|
||||||
|
supported.
|
||||||
|
"""
|
||||||
|
|
||||||
# only chunked transfer-encoding is supported
|
# only chunked transfer-encoding is supported
|
||||||
transfer_encoding = headers.get("transfer-encoding")
|
transfer_encoding = headers.get("transfer-encoding")
|
||||||
@@ -32,7 +48,7 @@ class Retriever(ABC):
|
|||||||
|
|
||||||
if chunked:
|
if chunked:
|
||||||
return ChunkedRetriever(client)
|
return ChunkedRetriever(client)
|
||||||
else:
|
|
||||||
content_length = headers.get("content-length")
|
content_length = headers.get("content-length")
|
||||||
|
|
||||||
if not content_length:
|
if not content_length:
|
||||||
@@ -43,6 +59,9 @@ class Retriever(ABC):
|
|||||||
|
|
||||||
|
|
||||||
class PreambleRetriever(Retriever):
|
class PreambleRetriever(Retriever):
|
||||||
|
"""
|
||||||
|
Retriever instance for retrieving the start-line and headers of an HTTP message.
|
||||||
|
"""
|
||||||
client: HTTPSocket
|
client: HTTPSocket
|
||||||
_buffer: []
|
_buffer: []
|
||||||
|
|
||||||
@@ -59,6 +78,10 @@ class PreambleRetriever(Retriever):
|
|||||||
self._buffer = []
|
self._buffer = []
|
||||||
|
|
||||||
def retrieve(self):
|
def retrieve(self):
|
||||||
|
"""
|
||||||
|
Returns an iterator of the retrieved lines.
|
||||||
|
@return:
|
||||||
|
"""
|
||||||
|
|
||||||
line = self.client.read_line()
|
line = self.client.read_line()
|
||||||
while True:
|
while True:
|
||||||
@@ -76,6 +99,9 @@ class PreambleRetriever(Retriever):
|
|||||||
|
|
||||||
|
|
||||||
class ContentLengthRetriever(Retriever):
|
class ContentLengthRetriever(Retriever):
|
||||||
|
"""
|
||||||
|
Retriever instance for retrieving a message body with a given content-length.
|
||||||
|
"""
|
||||||
length: int
|
length: int
|
||||||
|
|
||||||
def __init__(self, client: HTTPSocket, length: int):
|
def __init__(self, client: HTTPSocket, length: int):
|
||||||
@@ -83,6 +109,11 @@ class ContentLengthRetriever(Retriever):
|
|||||||
self.length = length
|
self.length = length
|
||||||
|
|
||||||
def retrieve(self):
|
def retrieve(self):
|
||||||
|
"""
|
||||||
|
Returns an iterator of the received message bytes.
|
||||||
|
The size of each iteration is not necessarily constant.
|
||||||
|
@raise IncompleteResponse: if the connection is closed or timed out before receiving the complete payload.
|
||||||
|
"""
|
||||||
|
|
||||||
cur_payload_size = 0
|
cur_payload_size = 0
|
||||||
read_size = BUFSIZE
|
read_size = BUFSIZE
|
||||||
@@ -95,10 +126,10 @@ class ContentLengthRetriever(Retriever):
|
|||||||
try:
|
try:
|
||||||
buffer = self.client.read(remaining)
|
buffer = self.client.read(remaining)
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
logging.error("Timed out before receiving complete payload")
|
logging.error("Timed out before receiving the complete payload")
|
||||||
raise IncompleteResponse("Timed out before receiving complete payload")
|
raise IncompleteResponse("Timed out before receiving complete payload")
|
||||||
except ConnectionError:
|
except ConnectionError:
|
||||||
logging.error("Timed out before receiving complete payload")
|
logging.error("Connection closed before receiving the complete payload")
|
||||||
raise IncompleteResponse("Connection closed before receiving complete payload")
|
raise IncompleteResponse("Connection closed before receiving complete payload")
|
||||||
|
|
||||||
if len(buffer) == 0:
|
if len(buffer) == 0:
|
||||||
@@ -110,6 +141,10 @@ class ContentLengthRetriever(Retriever):
|
|||||||
|
|
||||||
|
|
||||||
class RawRetriever(Retriever):
|
class RawRetriever(Retriever):
|
||||||
|
"""
|
||||||
|
Retriever instance for retrieve a message body without any length specifier or encoding.
|
||||||
|
This retriever will keep waiting until a timeout occurs or the connection is disconnected.
|
||||||
|
"""
|
||||||
|
|
||||||
def retrieve(self):
|
def retrieve(self):
|
||||||
while True:
|
while True:
|
||||||
@@ -120,20 +155,28 @@ class RawRetriever(Retriever):
|
|||||||
|
|
||||||
|
|
||||||
class ChunkedRetriever(Retriever):
|
class ChunkedRetriever(Retriever):
|
||||||
|
"""
|
||||||
|
Retriever instance for retrieving a message body with chunked encoding.
|
||||||
|
"""
|
||||||
|
|
||||||
def retrieve(self):
|
def retrieve(self):
|
||||||
|
"""
|
||||||
|
Returns an iterator of the received message bytes.
|
||||||
|
The size of each iteration is not necessarily constant.
|
||||||
|
@raise IncompleteResponse: if the connection is closed or timed out before receiving the complete payload.
|
||||||
|
"""
|
||||||
while True:
|
while True:
|
||||||
chunk_size = self.__get_chunk_size()
|
chunk_size = self.__get_chunk_size()
|
||||||
logging.debug("chunk-size: %s", chunk_size)
|
logging.debug("chunk-size: %s", chunk_size)
|
||||||
if chunk_size == 0:
|
if chunk_size == 0:
|
||||||
|
# remove all trailing lines
|
||||||
self.client.reset_request()
|
self.client.reset_request()
|
||||||
break
|
break
|
||||||
|
|
||||||
buffer = self.client.read(chunk_size)
|
buffer = self.client.read(chunk_size)
|
||||||
logging.debug("chunk: %r", buffer)
|
|
||||||
yield buffer
|
yield buffer
|
||||||
|
|
||||||
self.client.read_line() # remove CRLF
|
self.client.read_line() # remove trailing CRLF
|
||||||
|
|
||||||
def __get_chunk_size(self):
|
def __get_chunk_size(self):
|
||||||
line = self.client.read_line()
|
line = self.client.read_line()
|
||||||
|
@@ -75,7 +75,7 @@ class AbstractCommand(ABC):
|
|||||||
self._process_conditional_headers()
|
self._process_conditional_headers()
|
||||||
|
|
||||||
message = f"HTTP/1.1 {status} {status_message[status]}\r\n"
|
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)
|
content_length = len(body)
|
||||||
message += f"Content-Length: {content_length}\r\n"
|
message += f"Content-Length: {content_length}\r\n"
|
||||||
@@ -107,7 +107,7 @@ class AbstractCommand(ABC):
|
|||||||
path = root + norm_path
|
path = root + norm_path
|
||||||
|
|
||||||
if check and not os.path.exists(path):
|
if check and not os.path.exists(path):
|
||||||
raise NotFound()
|
raise NotFound(path)
|
||||||
|
|
||||||
return path
|
return path
|
||||||
|
|
||||||
@@ -131,7 +131,7 @@ class AbstractCommand(ABC):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
if modified <= min_date:
|
if modified <= min_date:
|
||||||
raise NotModified()
|
raise NotModified(f"{modified} <= {min_date}")
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -149,11 +149,11 @@ class AbstractModifyCommand(AbstractCommand, ABC):
|
|||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
path = self._get_path(False)
|
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!")
|
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!")
|
raise Forbidden("Target directory is an existing file!")
|
||||||
|
|
||||||
exists = os.path.exists(path)
|
exists = os.path.exists(path)
|
||||||
|
@@ -57,7 +57,7 @@ class RequestHandler:
|
|||||||
retriever = Retriever.create(self.conn, headers)
|
retriever = Retriever.create(self.conn, headers)
|
||||||
except UnsupportedEncoding as e:
|
except UnsupportedEncoding as e:
|
||||||
logging.error("Encoding not supported: %s=%s", e.enc_type, e.encoding)
|
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():
|
for buffer in retriever.retrieve():
|
||||||
body += buffer
|
body += buffer
|
||||||
@@ -68,7 +68,7 @@ class RequestHandler:
|
|||||||
|
|
||||||
cmd = command.create(message)
|
cmd = command.create(message)
|
||||||
msg = cmd.execute()
|
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)
|
self.conn.conn.sendall(msg)
|
||||||
|
|
||||||
def _check_request_line(self, method: str, target: Union[ParseResultBytes, ParseResult], version):
|
def _check_request_line(self, method: str, target: Union[ParseResultBytes, ParseResult], version):
|
||||||
@@ -77,22 +77,22 @@ class RequestHandler:
|
|||||||
raise MethodNotAllowed(METHODS)
|
raise MethodNotAllowed(METHODS)
|
||||||
|
|
||||||
if version not in ("1.0", "1.1"):
|
if version not in ("1.0", "1.1"):
|
||||||
raise HTTPVersionNotSupported()
|
raise HTTPVersionNotSupported(version)
|
||||||
|
|
||||||
# only origin-form and absolute-form are allowed
|
# only origin-form and absolute-form are allowed
|
||||||
if target.scheme not in ("", "http"):
|
if target.scheme not in ("", "http"):
|
||||||
# Only http is supported...
|
# 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]:
|
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] != "/":
|
if target.path == "" or target.path[0] != "/":
|
||||||
raise NotFound()
|
raise NotFound(str(target))
|
||||||
|
|
||||||
def _validate_request(self, msg):
|
def _validate_request(self, msg):
|
||||||
if msg.version == "1.1" and "host" not in msg.headers:
|
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)
|
self._check_request_line(msg.method, msg.target, msg.version)
|
||||||
|
|
||||||
@@ -119,5 +119,5 @@ class RequestHandler:
|
|||||||
message += "Content-Length: 0\r\n"
|
message += "Content-Length: 0\r\n"
|
||||||
message += "\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))
|
client.sendall(message.encode(FORMAT))
|
||||||
|
@@ -15,4 +15,4 @@ class ServerSocket(HTTPSocket):
|
|||||||
try:
|
try:
|
||||||
return super().read_line()
|
return super().read_line()
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
raise BadRequest()
|
raise BadRequest("UnicodeDecodeError")
|
||||||
|
@@ -67,17 +67,20 @@ class Worker:
|
|||||||
handler = RequestHandler(conn, self.host)
|
handler = RequestHandler(conn, self.host)
|
||||||
handler.listen()
|
handler.listen()
|
||||||
except HTTPServerCloseException as e:
|
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)
|
RequestHandler.send_error(conn, e.status_code, e.message)
|
||||||
break
|
break
|
||||||
except HTTPServerException as e:
|
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)
|
RequestHandler.send_error(conn, e.status_code, e.message)
|
||||||
except socket.timeout:
|
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
|
break
|
||||||
except Exception as e:
|
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)
|
RequestHandler.send_error(conn, InternalServerError.status_code, InternalServerError.message)
|
||||||
break
|
break
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user