update: added 'HEAD' method support for HTTP Server
This commit is contained in:
+1
-1
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
|
||||
[project]
|
||||
name = "common"
|
||||
description = "Commonly reusable code"
|
||||
version = "0.1.18"
|
||||
version = "0.1.19"
|
||||
requires-python = ">=3.13"
|
||||
authors = [
|
||||
{ name="BreakerBear", email="breakerbear@autistic.men" },
|
||||
|
||||
@@ -101,6 +101,7 @@ class History(logging.Handler):
|
||||
class Server(ThreadingHTTPServer):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.application: str
|
||||
self.encoding: str
|
||||
self.handlers = dict()
|
||||
|
||||
@@ -132,6 +133,11 @@ class RequestHandler(BaseHTTPRequestHandler):
|
||||
self.end_headers()
|
||||
self.wfile.write(response)
|
||||
|
||||
def do_HEAD(self):
|
||||
self.send_response(200)
|
||||
self.send_header('X-Powered-By', app) if (app := self.server.application) else None
|
||||
self.end_headers()
|
||||
|
||||
def do_POST(self, request=None):
|
||||
try:
|
||||
size = int(self.headers.get('Content-Length', '0'))
|
||||
@@ -160,27 +166,26 @@ class ServiceProvider:
|
||||
def __init__(self, options):
|
||||
self.options = dataclasses.asdict(options)
|
||||
self.server = Server((self.options['host'], self.options['port']), self.options['handler'])
|
||||
self.server.application = self.options['application']
|
||||
self.server.encoding = self.options['encoding']
|
||||
|
||||
@classmethod
|
||||
def default(cls):
|
||||
if not self.options['setup']: return
|
||||
import sys, _thread as t
|
||||
logger = logging.getLogger()
|
||||
history = History()
|
||||
logger.addHandler(history)
|
||||
opts = cls.Options()
|
||||
self = cls(opts)
|
||||
self.set('logs', lambda: history.truncate())
|
||||
self.set('exit', lambda: (t.interrupt_main(), sys.exit(0)))
|
||||
return self
|
||||
|
||||
@dataclasses.dataclass
|
||||
class Options:
|
||||
host : str = '127.0.0.1'
|
||||
port : int = 0
|
||||
setup : bool = True
|
||||
handler : type = RequestHandler
|
||||
encoding : str = 'UTF-8'
|
||||
interval : float = 0.2
|
||||
application : str = ''
|
||||
|
||||
def add(self, *handlers: Callable[..., Any], pairs: Iterable[tuple[str, Callable[..., Any]]] = None):
|
||||
for handler in handlers: self.set(handler.__name__, handler)
|
||||
|
||||
Reference in New Issue
Block a user