Improve documentation
This commit is contained in:
@@ -148,7 +148,7 @@ class AbstractCommand(ABC):
|
||||
@return: True if the header is invalid, and thus shouldn't be taken into account, throws NotModified
|
||||
if the content isn't modified since the given date.
|
||||
|
||||
@raise NotModified: If the date of if-modified-since greater than the modify date of the resource.
|
||||
@raise NotModified: If the date of if-modified-since greater than the modify-date of the resource.
|
||||
"""
|
||||
date_val = self.msg.headers.get("if-modified-since")
|
||||
if not date_val:
|
||||
@@ -164,7 +164,8 @@ class AbstractCommand(ABC):
|
||||
|
||||
return True
|
||||
|
||||
def get_mimetype(self, path):
|
||||
@staticmethod
|
||||
def get_mimetype(path):
|
||||
"""
|
||||
Guess the type of file.
|
||||
@param path: the path to the file to guess the type of
|
||||
@@ -243,8 +244,8 @@ class HeadCommand(AbstractCommand):
|
||||
|
||||
def execute(self):
|
||||
path = self._get_path()
|
||||
|
||||
mime = self.get_mimetype(path)
|
||||
|
||||
return self._build_message(200, mime, b"")
|
||||
|
||||
|
||||
@@ -301,6 +302,6 @@ class PutCommand(AbstractModifyCommand):
|
||||
|
||||
def execute(self):
|
||||
if "content-range" in self.msg.headers:
|
||||
raise BadRequest("PUT request contains Content-Range header")
|
||||
raise BadRequest("PUT request contains a Content-Range header")
|
||||
|
||||
super().execute()
|
||||
|
@@ -83,7 +83,7 @@ class HTTPServer:
|
||||
"""
|
||||
Cleanly shutdown the server
|
||||
|
||||
Notifies the worker processes to shutdown and eventually closes the server socket
|
||||
Notifies the worker processes to shut down and eventually closes the server socket
|
||||
"""
|
||||
|
||||
# Set stop event
|
||||
@@ -111,7 +111,7 @@ class HTTPServer:
|
||||
"""
|
||||
Create worker processes up to `self.worker_count`.
|
||||
|
||||
A worker process is created with start method "spawn", target `worker.worker` and the `self.logging_level`
|
||||
A worker process is created with start method "spawn", target `worker.worker`, and the `self.logging_level`
|
||||
is passed along with the `self.dispatch_queue` and `self._stop_event`
|
||||
"""
|
||||
for i in range(self.worker_count):
|
||||
|
@@ -20,13 +20,15 @@ class RequestHandler:
|
||||
A RequestHandler instance processes incoming HTTP requests messages from a single client.
|
||||
|
||||
RequestHandler instances are created everytime a client connects. They will read the incoming
|
||||
messages, parse, verify them and send a respond.
|
||||
messages, parse, verify them and send a response.
|
||||
"""
|
||||
|
||||
conn: ServerSocket
|
||||
host: str
|
||||
|
||||
def __init__(self, conn: socket, host):
|
||||
self.conn = ServerSocket(conn, host)
|
||||
self.conn = ServerSocket(conn)
|
||||
self.host = host
|
||||
|
||||
def listen(self):
|
||||
"""
|
||||
@@ -111,7 +113,7 @@ class RequestHandler:
|
||||
# Only http is supported...
|
||||
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.host and target.netloc != self.host.split(":")[0]:
|
||||
raise NotFound(str(target))
|
||||
|
||||
if target.path == "" or target.path[0] != "/":
|
||||
@@ -123,7 +125,7 @@ class RequestHandler:
|
||||
|
||||
@see: _check_request_line for exceptions raised when validating the request-line.
|
||||
@param msg: the message to validate
|
||||
@raise BadRequest: if HTTP 1.1 and the Host header is missing
|
||||
@raise BadRequest: if HTTP 1.1, and the Host header is missing
|
||||
"""
|
||||
|
||||
if msg.version == "1.1" and "host" not in msg.headers:
|
||||
|
@@ -1,11 +1,18 @@
|
||||
import socket
|
||||
|
||||
from httplib.exceptions import BadRequest
|
||||
from httplib.httpsocket import HTTPSocket
|
||||
|
||||
|
||||
class ServerSocket(HTTPSocket):
|
||||
"""
|
||||
Wrapper class for a socket. Represents a client connected to this server.
|
||||
"""
|
||||
|
||||
"""
|
||||
Reads the next line decoded as `httpsocket.FORMAT`
|
||||
|
||||
@return: the decoded next line retrieved from the socket
|
||||
@raise InvalidResponse: If the next line couldn't be decoded, but was expected to
|
||||
"""
|
||||
def read_line(self):
|
||||
try:
|
||||
return super().read_line()
|
||||
|
@@ -48,7 +48,7 @@ class Worker:
|
||||
@param host: The hostname of the HTTP server
|
||||
@param name: The name of this Worker instance
|
||||
@param queue: The dispatch queue for incoming socket connections
|
||||
@param stop_event: The Event that signals when to shutdown this worker.
|
||||
@param stop_event: The Event that signals when to shut down this worker.
|
||||
"""
|
||||
self.host = host
|
||||
self.name = name
|
||||
@@ -70,9 +70,9 @@ class Worker:
|
||||
"""
|
||||
while not self.stop_event.is_set():
|
||||
|
||||
# Blocks until thread is free
|
||||
# Blocks until the thread is free
|
||||
self.finished_queue.get()
|
||||
# Blocks until new client connects
|
||||
# Blocks until a new client connects
|
||||
conn, addr = self.queue.get()
|
||||
|
||||
if conn is None or addr is None:
|
||||
@@ -80,7 +80,7 @@ class Worker:
|
||||
|
||||
logging.debug("Processing new client: %s", addr)
|
||||
|
||||
# submit client to thread
|
||||
# submit the client to the executor
|
||||
self.executor.submit(self._handle_client, conn, addr)
|
||||
|
||||
self.shutdown()
|
||||
@@ -145,8 +145,10 @@ class Worker:
|
||||
self.executor.shutdown(False)
|
||||
|
||||
logging.info("Closing sockets")
|
||||
|
||||
# Copy dictionary to prevent issues with concurrency
|
||||
clients = self.dispatched_sockets.copy().values()
|
||||
|
||||
for client in clients:
|
||||
client: socket.socket
|
||||
try:
|
||||
|
Reference in New Issue
Block a user