Merge branch 'master' of https://github.com/yumoqing/ahserver
This commit is contained in:
commit
072cad2343
@ -11,12 +11,9 @@ from appPublic.jsonConfig import getConfig
|
|||||||
from appPublic.dictObject import DictObject
|
from appPublic.dictObject import DictObject
|
||||||
from appPublic.folderUtils import listFile
|
from appPublic.folderUtils import listFile
|
||||||
|
|
||||||
|
from .utils import unicode_escape
|
||||||
from .serverenv import ServerEnv
|
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:
|
class ObjectCache:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.cache = {}
|
self.cache = {}
|
||||||
|
@ -18,10 +18,12 @@ def path_decode(dpath):
|
|||||||
rc4 = RC4()
|
rc4 = RC4()
|
||||||
return rc4.decode(dpath,crypto_aim)
|
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)
|
filename = os.path.basename(filepath)
|
||||||
r = web.FileResponse(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:
|
if ct is not None:
|
||||||
r.content_type = ct
|
r.content_type = ct
|
||||||
else:
|
else:
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import codecs
|
||||||
|
from traceback import print_exc
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
@ -40,6 +42,7 @@ from .filestorage import FileStorage
|
|||||||
from .restful import DBCrud
|
from .restful import DBCrud
|
||||||
from .dbadmin import DBAdmin
|
from .dbadmin import DBAdmin
|
||||||
from .filedownload import file_download, path_decode
|
from .filedownload import file_download, path_decode
|
||||||
|
from .utils import unicode_escape
|
||||||
|
|
||||||
def getHeaderLang(request):
|
def getHeaderLang(request):
|
||||||
al = request.headers.get('Accept-Language')
|
al = request.headers.get('Accept-Language')
|
||||||
@ -227,6 +230,11 @@ class ProcessorResource(StaticResource,Url2File):
|
|||||||
if processor:
|
if processor:
|
||||||
return await processor.handle(request)
|
return await processor.handle(request)
|
||||||
|
|
||||||
|
filepath = self.url2file(str(request.url))
|
||||||
|
print('filepath=',filepath,str(request.url))
|
||||||
|
if filepath and self.isHtml(filepath):
|
||||||
|
return await self.html_handle(request, filepath)
|
||||||
|
|
||||||
print(f'path={path} handler by StaticResource..')
|
print(f'path={path} handler by StaticResource..')
|
||||||
if self.isFolder(path):
|
if self.isFolder(path):
|
||||||
config = getConfig()
|
config = getConfig()
|
||||||
@ -234,6 +242,31 @@ class ProcessorResource(StaticResource,Url2File):
|
|||||||
raise HTTPNotFound
|
raise HTTPNotFound
|
||||||
return await super()._handle(request)
|
return await super()._handle(request)
|
||||||
|
|
||||||
|
async 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.startswith('<!doctype html>'):
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
print_exc()
|
||||||
|
print(e)
|
||||||
|
return False
|
||||||
|
|
||||||
def url2processor(self, request, url):
|
def url2processor(self, request, url):
|
||||||
config = getConfig()
|
config = getConfig()
|
||||||
url = self.entireUrl(request, url)
|
url = self.entireUrl(request, url)
|
||||||
|
@ -51,14 +51,17 @@ class Url2File:
|
|||||||
if url[-1] == '/':
|
if url[-1] == '/':
|
||||||
url = url[:-1]
|
url = url[:-1]
|
||||||
|
|
||||||
if self.isFolder(url):
|
paths = url.split('/')[3:]
|
||||||
return self.defaultIndex(url)
|
f = os.path.join(self.path,*paths)
|
||||||
|
real_path = os.path.abspath(f)
|
||||||
|
if os.path.isdir(real_path):
|
||||||
|
for idx in self.indexes:
|
||||||
|
p = os.path.join(real_path,idx)
|
||||||
|
if os.path.isfile(p):
|
||||||
|
return p
|
||||||
|
|
||||||
if url.startswith(self.starts):
|
if os.path.isfile(real_path):
|
||||||
f = self.path + url[len(self.starts):]
|
return real_path
|
||||||
real_path = os.path.abspath(f)
|
|
||||||
if os.path.isfile(real_path):
|
|
||||||
return f
|
|
||||||
|
|
||||||
if not self.inherit:
|
if not self.inherit:
|
||||||
return None
|
return None
|
||||||
|
4
ahserver/utils.py
Normal file
4
ahserver/utils.py
Normal 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)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user