http_server_async: bugfixes

This commit is contained in:
Izalia Mae 2021-10-25 07:14:38 -04:00
parent 5b455035b2
commit fd6e8856bd
4 changed files with 35 additions and 12 deletions

View file

@ -26,8 +26,8 @@ class Application:
self._server = None
self.cfg = Config(**kwargs)
#self.router = Router(trim_last_slash=True)
self.router = Router()
self.router = Router(trim_last_slash=True)
#self.router = Router()
self.middleware = DotDict({'request': [], 'response': []})
for view in views:
@ -88,7 +88,7 @@ class Application:
def add_static(self, path, src):
if Path(src).isdir:
path = Path(path).join('{path}')
path = Path(path).join('{path:.*}')
self.add_route(Static(src), path)

View file

@ -298,9 +298,11 @@ class HeaderItem(list):
return ','.join(str(v) for v in self)
def set(self, *value):
def set(self, *values):
self.clear()
self.append(value)
for value in values:
self.append(value)
def one(self):

View file

@ -40,7 +40,11 @@ class Response:
@content_type.setter
def content_type(self, data):
self.headers['Content-Type'] = data
try:
self.headers['Content-Type'].set(data)
except KeyError:
self.headers['Content-Type'] = data
@property
@ -175,10 +179,13 @@ class Response:
if isinstance(data, str):
data = data.encode('utf-8')
elif isinstance(data, bytearray):
data = bytes(data)
elif any(map(isinstance, [data], [dict, list, tuple])):
data = json.dumps(data).encode('utf-8')
else:
elif not isinstance(data, bytes):
data = str(data).encode('utf-8')
return data

View file

@ -1,14 +1,28 @@
import magic
import magic, mimetypes
from . import http_methods, error
from ..dotdict import DotDict
from ..path import Path
from ..template import Color
from ..exceptions import (
InvalidMethodException,
MethodNotHandledException
)
default_theme = DotDict(
primary = Color('#e7a'),
secondary = Color('#a7e'),
background = Color('#191919'),
positive = Color('#aea'),
negative = Color('#e99'),
white = Color('#eee'),
black = Color('#111'),
speed = 250
)
class View:
__path__ = ''
@ -42,16 +56,16 @@ def Static(src):
with open(src_path, 'rb') as fd:
data = fd.read()
mime = magic.from_buffer(data[:2048], mime=True)
mime = magic.from_buffer(data[:2048], mime=True) or mimetypes.guess_type(path)
except (FileNotFoundError, IsADirectoryError) as e:
raise error.NotFound('Static file not found')
headers = {}
response.body = data
response.content_type = mime
return response
return StaticHandler
@ -100,5 +114,5 @@ class Style(View):
__path__ = '/framework/style.css'
async def get(self, request, response):
response.body = self.app.render('base.css')
response.body = self.app.render('base.css', default_theme)
response.content_type = 'text/css'