From c06691d27c08f8863f4c0007208d36f578bbf4a6 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Fri, 18 Dec 2020 13:27:53 +0800 Subject: [PATCH 1/4] bugfix --- ahserver/baseProcessor.py | 5 +---- ahserver/filedownload.py | 6 ++++-- ahserver/processorResource.py | 30 +++++++++++++++++++++++++++++- ahserver/utils.py | 4 ++++ 4 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 ahserver/utils.py diff --git a/ahserver/baseProcessor.py b/ahserver/baseProcessor.py index c64c78b..60abc15 100644 --- a/ahserver/baseProcessor.py +++ b/ahserver/baseProcessor.py @@ -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 = {} diff --git a/ahserver/filedownload.py b/ahserver/filedownload.py index 8765405..9d7ea4b 100644 --- a/ahserver/filedownload.py +++ b/ahserver/filedownload.py @@ -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: diff --git a/ahserver/processorResource.py b/ahserver/processorResource.py index c2084c6..ed05072 100644 --- a/ahserver/processorResource.py +++ b/ahserver/processorResource.py @@ -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(''): + return True + if b.stratswith(''): + return True + except: + return False def url2processor(self, request, url): url = self.entireUrl(request, url) diff --git a/ahserver/utils.py b/ahserver/utils.py new file mode 100644 index 0000000..1caec30 --- /dev/null +++ b/ahserver/utils.py @@ -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) + From 401abe976395eefa9e9a9097daf34ff224463e02 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Fri, 18 Dec 2020 13:33:51 +0800 Subject: [PATCH 2/4] bugfix --- ahserver/processorResource.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ahserver/processorResource.py b/ahserver/processorResource.py index ed05072..f4dd857 100644 --- a/ahserver/processorResource.py +++ b/ahserver/processorResource.py @@ -1,5 +1,6 @@ import os import re +from traceback import print_exc import asyncio @@ -40,7 +41,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 +from .utils import unicode_escape def getHeaderLang(request): al = request.headers.get('Accept-Language') @@ -234,7 +235,7 @@ class ProcessorResource(StaticResource,Url2File): if processor: return await processor.handle(request) - filepath = self.url2filepath(str(request.url)) + filepath = self.url2file(str(request.url)) if filepath and self.isHtml(filepath): return await html_handle(request, filepath) @@ -265,7 +266,9 @@ class ProcessorResource(StaticResource,Url2File): return True if b.stratswith(''): return True - except: + except Exception as e: + print_exc() + print(e) return False def url2processor(self, request, url): From 175a49749398e308833728d2a03298b60a162314 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Fri, 18 Dec 2020 13:46:35 +0800 Subject: [PATCH 3/4] bugfix --- ahserver/processorResource.py | 1 + ahserver/url2file.py | 16 +++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/ahserver/processorResource.py b/ahserver/processorResource.py index f4dd857..f8a4d35 100644 --- a/ahserver/processorResource.py +++ b/ahserver/processorResource.py @@ -236,6 +236,7 @@ class ProcessorResource(StaticResource,Url2File): 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 html_handle(request, filepath) diff --git a/ahserver/url2file.py b/ahserver/url2file.py index dfabf91..5576f96 100644 --- a/ahserver/url2file.py +++ b/ahserver/url2file.py @@ -51,16 +51,22 @@ class Url2File: if url[-1] == '/': url = url[:-1] - if self.isFolder(url): - return self.defaultIndex(url) - + print('url2file.py:self.starts=',self.starts) if url.startswith(self.starts): f = self.path + url[len(self.starts):] real_path = os.path.abspath(f) - if os.path.isfile(real_path): - return f + + if os.path.isdir(real_path): + for f in self.indexes: + p = os.path.join(real_path,'f) + if os.path.isfile(p): + return p + + if os.path.isfile(real_path): + return real_path if not self.inherit: + print('url2file.py:real_path=',real_path) return None items = url.split('/') if len(items) > 2: From 1028c20c08859e20921ff61b664362a25a84ecff Mon Sep 17 00:00:00 2001 From: yumoqing Date: Fri, 18 Dec 2020 14:25:46 +0800 Subject: [PATCH 4/4] bugfix --- ahserver/processorResource.py | 7 ++++--- ahserver/url2file.py | 13 +++++-------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/ahserver/processorResource.py b/ahserver/processorResource.py index f8a4d35..d272b41 100644 --- a/ahserver/processorResource.py +++ b/ahserver/processorResource.py @@ -1,5 +1,6 @@ import os import re +import codecs from traceback import print_exc import asyncio @@ -238,7 +239,7 @@ class ProcessorResource(StaticResource,Url2File): filepath = self.url2file(str(request.url)) print('filepath=',filepath,str(request.url)) if filepath and self.isHtml(filepath): - return await html_handle(request, filepath) + return await self.html_handle(request, filepath) print(f'path={path} handler by StaticResource..') if self.isFolder(path): @@ -247,7 +248,7 @@ class ProcessorResource(StaticResource,Url2File): raise HTTPNotFound return await super()._handle(request) - def html_handle(self,request,filepath): + async def html_handle(self,request,filepath): with codecs.open(filepath,'r', 'utf-8') as f: b = f.read() b = unicode_escape(b) @@ -265,7 +266,7 @@ class ProcessorResource(StaticResource,Url2File): b = f.read() if b.startswith(''): return True - if b.stratswith(''): + if b.startswith(''): return True except Exception as e: print_exc() diff --git a/ahserver/url2file.py b/ahserver/url2file.py index 5576f96..6bc03b5 100644 --- a/ahserver/url2file.py +++ b/ahserver/url2file.py @@ -51,14 +51,12 @@ class Url2File: if url[-1] == '/': url = url[:-1] - print('url2file.py:self.starts=',self.starts) - if url.startswith(self.starts): - f = self.path + url[len(self.starts):] - real_path = os.path.abspath(f) - + paths = url.split('/')[3:] + f = os.path.join(self.path,*paths) + real_path = os.path.abspath(f) if os.path.isdir(real_path): - for f in self.indexes: - p = os.path.join(real_path,'f) + for idx in self.indexes: + p = os.path.join(real_path,idx) if os.path.isfile(p): return p @@ -66,7 +64,6 @@ class Url2File: return real_path if not self.inherit: - print('url2file.py:real_path=',real_path) return None items = url.split('/') if len(items) > 2: