This commit is contained in:
yumoqing 2020-12-18 13:27:53 +08:00
parent f039c242bb
commit c06691d27c
4 changed files with 38 additions and 7 deletions

View File

@ -11,12 +11,9 @@ from appPublic.jsonConfig import getConfig
from appPublic.dictObject import DictObject
from appPublic.folderUtils import listFile
from .utils import unicode_escape
from .serverenv import ServerEnv
def unicode_escape(s):
x = [ch if ord(ch) < 256 else ch.encode('unicode_escape').decode('utf-8') for ch in s]
return ''.join(x)
class ObjectCache:
def __init__(self):
self.cache = {}

View File

@ -18,10 +18,12 @@ def path_decode(dpath):
rc4 = RC4()
return rc4.decode(dpath,crypto_aim)
async def file_download(request, filepath):
async def file_download(request, filepath, content_type=None):
filename = os.path.basename(filepath)
r = web.FileResponse(filepath)
ct, encoding = mimetypes.guess_type(filepath)
ct = content_type
if ct is None:
ct, encoding = mimetypes.guess_type(filepath)
if ct is not None:
r.content_type = ct
else:

View File

@ -40,6 +40,7 @@ from .filestorage import FileStorage
from .restful import DBCrud
from .dbadmin import DBAdmin
from .filedownload import file_download, path_decode
from utils import unicode_escape
def getHeaderLang(request):
al = request.headers.get('Accept-Language')
@ -232,13 +233,40 @@ class ProcessorResource(StaticResource,Url2File):
processor = self.url2processor(request, str(request.url))
if processor:
return await processor.handle(request)
filepath = self.url2filepath(str(request.url))
if filepath and self.isHtml(filepath):
return await html_handle(request, filepath)
print(f'path={path} handler by StaticResource..')
if self.isFolder(path):
config = getConfig()
if not config.website.allowListFolder:
raise HTTPNotFound
return await super()._handle(request)
def html_handle(self,request,filepath):
with codecs.open(filepath,'r', 'utf-8') as f:
b = f.read()
b = unicode_escape(b)
headers = {
'Content-Type': 'text/html; utf-8',
'Accept-Ranges': 'bytes',
'Content-Length': str(len(b))
}
resp = Response(text=b,headers=headers)
return resp
def isHtml(self,fn):
try:
with codecs.open(fn,'r','utf-8') as f:
b = f.read()
if b.startswith('<html>'):
return True
if b.stratswith('<!doctype html>'):
return True
except:
return False
def url2processor(self, request, url):
url = self.entireUrl(request, url)

4
ahserver/utils.py Normal file
View File

@ -0,0 +1,4 @@
def unicode_escape(s):
x = [ch if ord(ch) < 256 else ch.encode('unicode_escape').decode('utf-8') for ch in s]
return ''.join(x)