11import asyncio
2+ import base64
23from dataclasses import dataclass
34import datetime
45import inspect
@@ -70,12 +71,15 @@ def post(self, path):
7071
7172class Resource :
7273 def __init__ (self , url : str , path : Path , show_dir : bool = False ) -> None :
73- if not path .is_dir ():
74+ if not path .exists ():
7475 raise RuntimeError (f"The path { path } is not dir." )
76+ self .path = path
7577 self .dir = str (path .absolute ()).replace ("\\ " , "/" ).removesuffix ("/" )
7678 self .url = f"/" + url .lstrip ("/" )
7779 self .show_dir = show_dir
78- async def __call__ (self , url : str , request : 'Request' ) -> Any :
80+ async def __call__ (self , request : 'Request' ) -> Any :
81+ if self .path .is_file ():
82+ return self .dir
7983 path = (self .dir + request .path .removeprefix (self .url ))
8084 filepath = Path (self .dir + request .path .removeprefix (self .url ))
8185 if filepath .is_dir () and self .show_dir :
@@ -107,7 +111,7 @@ async def __call__(self, url: str, request: 'Request') -> Any:
107111 return content
108112 elif filepath .is_file ():
109113 return filepath
110- return "Not Found"
114+ return Response ( "Not Found" , status_code = 404 )
111115
112116class Application :
113117 def __init__ (self ) -> None :
@@ -180,7 +184,7 @@ async def handle(self, request: 'Request', client: Client):
180184 else :
181185 for resource in self ._resources :
182186 if request .url .startswith (resource .url ):
183- result = await resource (request . url , request )
187+ result = await resource (request )
184188 break
185189 if result == None :
186190 accept = await request .get_headers ("Accept" , ) or ""
@@ -504,15 +508,22 @@ async def handle(data, client: Client):
504508import web
505509server : Optional [asyncio .Server ] = None
506510cert = None
511+ cur_ssl = False
512+ def get_ssl ():
513+ global cur_ssl
514+ return cur_ssl
507515
508516def load_cert ():
509- global cert
517+ global cert , cur_ssl
510518 if Path (".ssl/cert.pem" ).exists () and Path (".ssl/key.pem" ).exists ():
511519 cert = ssl .create_default_context (ssl .Purpose .CLIENT_AUTH )
512520 cert .check_hostname = False
513521 cert .load_cert_chain (Path (".ssl/cert.pem" ), Path (".ssl/key.pem" ))
514522 if server :
515523 server .close ()
524+ cur_ssl = True
525+ else :
526+ cur_ssl = False
516527
517528async def _handle (reader : asyncio .StreamReader , writer : asyncio .StreamWriter ):
518529 client = Client (reader , writer )
@@ -529,16 +540,21 @@ async def _handle(reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
529540async def main ():
530541 global cert , server
531542 load_cert ()
543+ import cluster
544+ await cluster .init ()
532545 while 1 :
533546 try :
534547 server = await asyncio .start_server (_handle , host = '0.0.0.0' , port = config .PORT , ssl = cert )
535548 print (f"Server listen on { config .PORT } { ' with ssl' if cert else '' } !" )
536- import cluster
537- await cluster .init ()
538549 await server .serve_forever ()
539550 except :
540551 if server :
541552 server .close ()
542553 traceback .print_exc ()
554+
555+ @app .get ("/favicon.ico" )
556+ async def _ ():
557+ return base64 .b64decode ("AAABAAIAEBAAAAEAIABoBAAAJgAAACAgAAABACAAKBEAAI4EAAAoAAAAEAAAACAAAAABACAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAHA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/xwN+/8cDfv/HA37/xwN+/8cDfv/HA37/xwN+/8cDf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