update
This commit is contained in:
@@ -4,11 +4,10 @@ import sys
|
||||
from abc import ABC, abstractmethod
|
||||
from datetime import datetime
|
||||
from time import mktime
|
||||
from typing import Dict
|
||||
from wsgiref.handlers import format_date_time
|
||||
|
||||
from client.httpclient import FORMAT
|
||||
from httplib.exceptions import NotFound, Conflict, Forbidden
|
||||
from httplib.exceptions import NotFound, Forbidden, NotModified
|
||||
from httplib.message import ServerMessage as Message
|
||||
|
||||
root = os.path.join(os.path.dirname(sys.argv[0]), "public")
|
||||
@@ -21,7 +20,6 @@ status_message = {
|
||||
400: "Bad Request",
|
||||
404: "Not Found",
|
||||
500: "Internal Server Error",
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +38,6 @@ def create(message: Message):
|
||||
|
||||
class AbstractCommand(ABC):
|
||||
path: str
|
||||
headers: Dict[str, str]
|
||||
msg: Message
|
||||
|
||||
def __init__(self, message: Message):
|
||||
@@ -52,7 +49,15 @@ class AbstractCommand(ABC):
|
||||
def command(self):
|
||||
pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def _conditional_headers(self):
|
||||
pass
|
||||
|
||||
def _get_date(self):
|
||||
"""
|
||||
Returns a string representation of the current date according to RFC 1123
|
||||
"""
|
||||
now = datetime.now()
|
||||
stamp = mktime(now.timetuple())
|
||||
return format_date_time(stamp)
|
||||
@@ -61,7 +66,12 @@ class AbstractCommand(ABC):
|
||||
def execute(self):
|
||||
pass
|
||||
|
||||
def _build_message(self, status: int, content_type: str, body: bytes):
|
||||
def _build_message(self, status: int, content_type: str, body: bytes, extra_headers=None):
|
||||
|
||||
if extra_headers is None:
|
||||
extra_headers = {}
|
||||
self._process_conditional_headers()
|
||||
|
||||
message = f"HTTP/1.1 {status} {status_message[status]}\r\n"
|
||||
message += self._get_date() + "\r\n"
|
||||
|
||||
@@ -72,15 +82,17 @@ class AbstractCommand(ABC):
|
||||
message += f"Content-Type: {content_type}"
|
||||
if content_type.startswith("text"):
|
||||
message += "; charset=UTF-8"
|
||||
message += "\r\n"
|
||||
message += "\r\n"
|
||||
elif content_length > 0:
|
||||
message += f"Content-Type: application/octet-stream"
|
||||
message += f"Content-Type: application/octet-stream\r\n"
|
||||
|
||||
for header in extra_headers:
|
||||
message += f"{header}: {extra_headers[header]}\r\n"
|
||||
|
||||
message += "\r\n"
|
||||
message = message.encode(FORMAT)
|
||||
if content_length > 0:
|
||||
message += body
|
||||
message += b"\r\n"
|
||||
|
||||
return message
|
||||
|
||||
@@ -97,6 +109,30 @@ class AbstractCommand(ABC):
|
||||
|
||||
return path
|
||||
|
||||
def _process_conditional_headers(self):
|
||||
|
||||
for header in self._conditional_headers:
|
||||
tmp = self.msg.headers.get(header)
|
||||
|
||||
if not tmp:
|
||||
continue
|
||||
self._conditional_headers[header]()
|
||||
|
||||
def _if_modified_since(self):
|
||||
date_val = self.msg.headers.get("if-modified-since")
|
||||
if not date_val:
|
||||
return True
|
||||
modified = datetime.utcfromtimestamp(os.path.getmtime(self._get_path(False)))
|
||||
try:
|
||||
min_date = datetime.strptime(date_val, '%a, %d %b %Y %H:%M:%S GMT')
|
||||
except ValueError:
|
||||
return True
|
||||
|
||||
if modified <= min_date:
|
||||
raise NotModified()
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class AbstractModifyCommand(AbstractCommand, ABC):
|
||||
|
||||
@@ -105,6 +141,10 @@ class AbstractModifyCommand(AbstractCommand, ABC):
|
||||
def _file_mode(self):
|
||||
pass
|
||||
|
||||
@property
|
||||
def _conditional_headers(self):
|
||||
return {}
|
||||
|
||||
def execute(self):
|
||||
path = self._get_path(False)
|
||||
dir = os.path.dirname(path)
|
||||
@@ -114,31 +154,47 @@ class AbstractModifyCommand(AbstractCommand, ABC):
|
||||
if os.path.exists(dir) and not os.path.isdir(dir):
|
||||
raise Forbidden("Target directory is an existing file!")
|
||||
|
||||
exists = os.path.exists(path)
|
||||
|
||||
try:
|
||||
with open(path, mode=f"{self._file_mode}b") as file:
|
||||
file.write(self.msg.body)
|
||||
except IsADirectoryError:
|
||||
raise Forbidden("The target resource is a directory!")
|
||||
|
||||
if exists:
|
||||
status = 204
|
||||
else:
|
||||
status = 201
|
||||
|
||||
return self._build_message(status, None, )
|
||||
|
||||
|
||||
class HeadCommand(AbstractCommand):
|
||||
@property
|
||||
def command(self):
|
||||
return "HEAD"
|
||||
|
||||
@property
|
||||
def _conditional_headers(self):
|
||||
return {'if-modified-since': self._if_modified_since}
|
||||
|
||||
def execute(self):
|
||||
path = self._get_path()
|
||||
|
||||
mime = mimetypes.guess_type(path)[0]
|
||||
return self._build_message(200, mime, b"")
|
||||
|
||||
@property
|
||||
def command(self):
|
||||
return "HEAD"
|
||||
|
||||
|
||||
class GetCommand(AbstractCommand):
|
||||
@property
|
||||
def command(self):
|
||||
return "GET"
|
||||
|
||||
@property
|
||||
def _conditional_headers(self):
|
||||
return {'if-modified-since': self._if_modified_since}
|
||||
|
||||
def get_mimetype(self, path):
|
||||
mime = mimetypes.guess_type(path)[0]
|
||||
|
||||
|
@@ -10,6 +10,9 @@ from server import worker
|
||||
|
||||
|
||||
class HTTPServer:
|
||||
"""
|
||||
|
||||
"""
|
||||
address: str
|
||||
port: int
|
||||
workers = []
|
||||
@@ -20,6 +23,13 @@ class HTTPServer:
|
||||
_stop_event: Event
|
||||
|
||||
def __init__(self, address: str, port: int, worker_count, logging_level):
|
||||
"""
|
||||
Initialize a HTTP server with the specified address, port, worker_count and logging_level
|
||||
@param address: the address to listen on for connections
|
||||
@param port: the port to listen on for connections
|
||||
@param worker_count:
|
||||
@param logging_level:
|
||||
"""
|
||||
self.address = address
|
||||
self.port = port
|
||||
self.worker_count = worker_count
|
||||
@@ -30,24 +40,39 @@ class HTTPServer:
|
||||
self._stop_event = mp.Event()
|
||||
|
||||
def start(self):
|
||||
"""
|
||||
Start the HTTP server.
|
||||
"""
|
||||
try:
|
||||
self.__do_start()
|
||||
except KeyboardInterrupt:
|
||||
self.__shutdown()
|
||||
|
||||
def __do_start(self):
|
||||
"""
|
||||
Internal method to start the server.
|
||||
|
||||
@raise:
|
||||
"""
|
||||
# Create socket
|
||||
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
# self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
self.server.bind((self.address, self.port))
|
||||
|
||||
# Create workers processes to handle requests
|
||||
self.__create_workers()
|
||||
|
||||
self.__listen()
|
||||
|
||||
def __listen(self):
|
||||
"""
|
||||
Start listening for new connections
|
||||
|
||||
If a connection is received, it will be dispatched to the worker queue, and picked up by a worker process.
|
||||
"""
|
||||
|
||||
self.server.listen()
|
||||
logging.debug("Listening for connections")
|
||||
logging.debug("Listening on %s:%d", self.address, self.port)
|
||||
|
||||
while True:
|
||||
if self._dispatch_queue.qsize() > self.worker_count:
|
||||
@@ -62,6 +87,11 @@ class HTTPServer:
|
||||
logging.debug("Dispatched connection %s", addr)
|
||||
|
||||
def __shutdown(self):
|
||||
"""
|
||||
Cleanly shutdown the server
|
||||
|
||||
Notifies the worker processes to shutdown and eventually closes the server socket
|
||||
"""
|
||||
|
||||
# Set stop event
|
||||
self._stop_event.set()
|
||||
@@ -85,10 +115,18 @@ class HTTPServer:
|
||||
self.server.close()
|
||||
|
||||
def __create_workers(self):
|
||||
"""
|
||||
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`
|
||||
is passed along with the `self.dispatch_queue` and `self._stop_event`
|
||||
|
||||
"""
|
||||
for i in range(self.worker_count):
|
||||
logging.debug("Creating worker: %d", i + 1)
|
||||
p = mp.Process(target=worker.worker,
|
||||
args=(f"{self.address}:{self.port}", i + 1, self.logging_level, self._dispatch_queue, self._stop_event))
|
||||
args=(f"{self.address}:{self.port}", i + 1, self.logging_level, self._dispatch_queue,
|
||||
self._stop_event))
|
||||
p.start()
|
||||
self.workers.append(p)
|
||||
|
||||
|
@@ -1,7 +1,6 @@
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from datetime import datetime
|
||||
from socket import socket
|
||||
from time import mktime
|
||||
@@ -16,6 +15,7 @@ from httplib.httpsocket import HTTPSocket, FORMAT
|
||||
from httplib.message import ServerMessage as Message
|
||||
from httplib.retriever import Retriever, PreambleRetriever
|
||||
from server import command
|
||||
from server.serversocket import ServerSocket
|
||||
|
||||
METHODS = ("GET", "HEAD", "PUT", "POST")
|
||||
|
||||
@@ -25,7 +25,7 @@ class RequestHandler:
|
||||
root = os.path.join(os.path.dirname(sys.argv[0]), "public")
|
||||
|
||||
def __init__(self, conn: socket, host):
|
||||
self.conn = HTTPSocket(conn, host)
|
||||
self.conn = ServerSocket(conn, host)
|
||||
|
||||
def listen(self):
|
||||
|
||||
@@ -68,6 +68,7 @@ class RequestHandler:
|
||||
|
||||
cmd = command.create(message)
|
||||
msg = cmd.execute()
|
||||
logging.debug("---response begin---\r\n%s---response end---", msg)
|
||||
self.conn.conn.sendall(msg)
|
||||
|
||||
def _check_request_line(self, method: str, target: Union[ParseResultBytes, ParseResult], version):
|
||||
@@ -119,4 +120,4 @@ class RequestHandler:
|
||||
message += "\r\n"
|
||||
|
||||
logging.debug("Sending: %r", message)
|
||||
client.sendall(message.encode(FORMAT))
|
||||
client.sendall(message.encode(FORMAT))
|
18
server/serversocket.py
Normal file
18
server/serversocket.py
Normal file
@@ -0,0 +1,18 @@
|
||||
import socket
|
||||
|
||||
from httplib.exceptions import BadRequest
|
||||
from httplib.httpsocket import HTTPSocket
|
||||
|
||||
BUFSIZE = 4096
|
||||
TIMEOUT = 3
|
||||
FORMAT = "UTF-8"
|
||||
MAXLINE = 4096
|
||||
|
||||
|
||||
class ServerSocket(HTTPSocket):
|
||||
|
||||
def read_line(self):
|
||||
try:
|
||||
return super().read_line()
|
||||
except UnicodeDecodeError:
|
||||
raise BadRequest()
|
@@ -4,7 +4,7 @@ import socket
|
||||
import threading
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
from httplib.exceptions import HTTPServerException, InternalServerError
|
||||
from httplib.exceptions import HTTPServerException, InternalServerError, HTTPServerCloseException
|
||||
from server.requesthandler import RequestHandler
|
||||
|
||||
THREAD_LIMIT = 128
|
||||
@@ -61,17 +61,25 @@ class Worker:
|
||||
self.shutdown()
|
||||
|
||||
def _handle_client(self, conn: socket.socket, addr):
|
||||
try:
|
||||
handler = RequestHandler(conn, self.host)
|
||||
handler.listen()
|
||||
except HTTPServerException as e:
|
||||
logging.debug("HTTP Exception:", exc_info=e)
|
||||
RequestHandler.send_error(conn, e.status_code, e.message)
|
||||
except socket.timeout:
|
||||
logging.debug("Socket for client %s timed out", addr)
|
||||
except Exception as e:
|
||||
logging.debug("Internal error", exc_info=e)
|
||||
RequestHandler.send_error(conn, InternalServerError.status_code, InternalServerError.message)
|
||||
|
||||
while True:
|
||||
try:
|
||||
handler = RequestHandler(conn, self.host)
|
||||
handler.listen()
|
||||
except HTTPServerCloseException as e:
|
||||
logging.debug("HTTP Exception:", exc_info=e)
|
||||
RequestHandler.send_error(conn, e.status_code, e.message)
|
||||
break
|
||||
except HTTPServerException as e:
|
||||
logging.debug("HTTP Exception:", exc_info=e)
|
||||
RequestHandler.send_error(conn, e.status_code, e.message)
|
||||
except socket.timeout:
|
||||
logging.debug("Socket for client %s timed out", addr)
|
||||
break
|
||||
except Exception as e:
|
||||
logging.debug("Internal error", exc_info=e)
|
||||
RequestHandler.send_error(conn, InternalServerError.status_code, InternalServerError.message)
|
||||
break
|
||||
|
||||
conn.shutdown(socket.SHUT_RDWR)
|
||||
conn.close()
|
||||
|
Reference in New Issue
Block a user