From 92d0cd5b1ca60d386fa17e04d5f4c1c435b215ff Mon Sep 17 00:00:00 2001 From: yumoqing Date: Sun, 20 Dec 2020 11:43:00 +0800 Subject: [PATCH] bugfix --- ahserver/baseProcessor.py | 36 +++++++++++++++------------- ahserver/myTE.py | 1 + ahserver/processorResource.py | 29 +++++++++++------------ ahserver/url2file.py | 44 +++++++++++------------------------ 4 files changed, 48 insertions(+), 62 deletions(-) diff --git a/ahserver/baseProcessor.py b/ahserver/baseProcessor.py index a6aa106..b7ccef9 100644 --- a/ahserver/baseProcessor.py +++ b/ahserver/baseProcessor.py @@ -47,6 +47,7 @@ class BaseProcessor: def __init__(self,path,resource): self.path = path + print('self.path=',self.path) self.resource = resource self.retResponse = None # self.last_modified = os.path.getmtime(path) @@ -58,6 +59,7 @@ class BaseProcessor: self.content = '' async def set_run_env(self, request): + self.real_path = self.resource.url2file(self.resource.entireUrl(request, self.path)) g = ServerEnv() self.run_ns = {} self.run_ns.update(g) @@ -101,11 +103,17 @@ class TemplateProcessor(BaseProcessor): def isMe(self,name): return name=='tmpl' - async def datahandle(self,request): - path = request.path + async def path_call(self, request): + await self.set_run_env(request) + path = self.path + url = self.resource.entireUrl(request, path) ns = self.run_ns te = self.run_ns['tmpl_engine'] - self.content = te.render(path,**ns) + print('****url=',url,'*****') + return te.render(url,**ns) + + async def datahandle(self,request): + self.content = await self.path_call(request) def setheaders(self): super(TemplateProcessor,self).setheaders() @@ -121,20 +129,14 @@ class JSUIProcessor(TemplateProcessor): def isMe(self,name): return name=='jsui' - async def path_call(self, request, path): - ns = self.run_ns - te = self.run_ns['tmpl_engine'] - return te.render(path,**ns) - - async def datahandle(self, request): params = await self.resource.y_env['request2ns']() if params.get('_jsui',None): super().datahandle(request) else: - content0 = await self.path_call(request,'/header.tmpl') - content1 = await self.path_call(request,self.path) - content2 = await self.path_call(request,'/footer.tmpl') + content0 = await self.resource.path_call(request,'/header.tmpl') + content1 = await self.resource.path_call(request,self.path) + content2 = await self.resource.path_call(request,'/footer.tmpl') self.content = '%s%s%s' % (content0,content1,content2) class PythonScriptProcessor(BaseProcessor): @@ -144,6 +146,7 @@ class PythonScriptProcessor(BaseProcessor): def loadScript(self, path): data = '' + print('path=',path) with codecs.open(path,'rb','utf-8') as f: data = f.read() b= ''.join(data.split('\r')) @@ -152,17 +155,18 @@ class PythonScriptProcessor(BaseProcessor): txt = "async def myfunc(request,**ns):\n" + '\n'.join(lines) return txt - async def path_call(self, request, path): + async def path_call(self, request): await self.set_run_env(request) lenv = self.run_ns del lenv['request'] - txt = self.loadScript(path) + txt = self.loadScript(self.real_path) exec(txt,lenv,lenv) func = lenv['myfunc'] return await func(request,**lenv) async def datahandle(self,request): - self.content = await self.path_call(request, self.path) + print('self.real_path=',self.real_path) + self.content = await self.path_call(request) class MarkdownProcessor(BaseProcessor): @classmethod @@ -171,7 +175,7 @@ class MarkdownProcessor(BaseProcessor): async def datahandle(self,request:Request): data = '' - with codecs.open(self.path,'rb','utf-8') as f: + with codecs.open(self.real_path,'rb','utf-8') as f: data = f.read() b = data b = self.urlreplace(b,request) diff --git a/ahserver/myTE.py b/ahserver/myTE.py index 2971907..5e04807 100644 --- a/ahserver/myTE.py +++ b/ahserver/myTE.py @@ -16,6 +16,7 @@ class TmplLoader(BaseLoader, TmplUrl2File): def get_source(self,env: Environment,template: str): config = getConfig() coding = config.website.coding + fp = self.url2file(template) if not os.path.isfile(fp): raise TemplateNotFound(template) diff --git a/ahserver/processorResource.py b/ahserver/processorResource.py index 87404df..13d4cb6 100644 --- a/ahserver/processorResource.py +++ b/ahserver/processorResource.py @@ -93,13 +93,10 @@ class ProcessorResource(StaticResource,Url2File): def setIndexes(self, indexes): self.y_indexes = indexes - def abspath(self,path:str): - path = path[len(self.y_prefix):] - if len(path)>0 and path[0] == '/': - path = path[1:] - rp = os.path.join(self.y_directory , path) - real_path = os.path.abspath(rp) - return real_path + def abspath(self, request, path:str): + url = self.entireUrl(request, path) + print('url=',url, 'path=',path) + return self.url2file(url) async def getPostData(self,request: Request) -> dict: reader = await request.multipart() @@ -236,7 +233,7 @@ class ProcessorResource(StaticResource,Url2File): return await self.html_handle(request, filepath) print(f'path={path} handler by StaticResource..') - if self.isFolder(path): + if os.path.isdir(filepath): config = getConfig() if not config.website.allowListFolder: raise HTTPNotFound @@ -272,16 +269,18 @@ class ProcessorResource(StaticResource,Url2File): url = self.entireUrl(request, url) host = '/'.join(str(request.url).split('/')[:3]) path = url[len(host):].split('?')[0] + real_path = self.abspath(request, path) + print('real_path=', real_path) if config.website.startswiths: for a in config.website.startswiths: if path.startswith(a.leading): - processor = FunctionProcessor(self.abspath(path),self,a) + processor = FunctionProcessor(path,self,a) return processor for word, handlername in self.y_processors: if path.endswith(word): Klass = getProcessor(handlername) - processor = Klass(self.abspath(path),self) + processor = Klass(path,self) return processor return None @@ -296,10 +295,9 @@ class ProcessorResource(StaticResource,Url2File): return '%s%s' % (h, p) async def path_call(self,request, path, params={}): - processor = self.url2processor(request, path) - real_path = self.url2file(path) - print('processorResource.py:real_path=',real_path) - return await processor.path_call(request, real_path) + url = self.entireUrl(request, path) + processor = self.url2processor(request, url) + return await processor.path_call(request) def url_call(self,request, url,params={}): processor = self.url2processor(request, url) @@ -321,4 +319,5 @@ class ProcessorResource(StaticResource,Url2File): if url.startswith('https://') or url.startswith('http://') : return url path = request.path - return self.relatedurl(path,url) + new_path = self.relatedurl(path,url) + return self.entireUrl(request, new_path) diff --git a/ahserver/url2file.py b/ahserver/url2file.py index 6bc03b5..f79cc04 100644 --- a/ahserver/url2file.py +++ b/ahserver/url2file.py @@ -21,62 +21,44 @@ class Url2File: break return '/'.join(items) - def isFolder(self,url: str) ->bool: - if url.startswith(self.starts): - rp = self.path + url[len(self.starts):] - real_path = os.path.abspath(rp) - if os.path.isdir(real_path): - return True - return False - - - def isFile(self,url:str) ->bool: - if url.startswith(self.starts): - rp = self.path + url[len(self.starts):] - real_path = os.path.abspath(rp) - if os.path.isfile(real_path): - return True - return False - - - def defaultIndex(self,url: str) -> str: - for p in self.indexes: - rp = url + '/' + p - r = self.url2file(rp) - if r is not None: - return r - return None - - def url2file(self,url: str): + def url2file(self, url: str): + if url[-1] == '/': url = url[:-1] - - paths = url.split('/')[3:] + paths = url.split('/') + if url.startswith('http://') or url.startswith('https://'): + paths = paths[3:] f = os.path.join(self.path,*paths) real_path = os.path.abspath(f) + print('0 - url2file():real_path=', real_path,'url=',url) if os.path.isdir(real_path): for idx in self.indexes: p = os.path.join(real_path,idx) if os.path.isfile(p): + print('find it:',p, 'url=', url) return p if os.path.isfile(real_path): + print('find it here:',real_path, 'url=', url) return real_path if not self.inherit: + print('1-real_path=', real_path, ' not exists') return None items = url.split('/') if len(items) > 2: del items[-2] url = '/'.join(items) return self.url2file(url) + print('2-real_path=', real_path, ' not exists') return None def relatedurl(self,url: str, name: str) -> str: if url[-1] == '/': url = url[:-1] - if not self.isFolder(url): + fp = self.url2file(url) + if os.path.isfile(fp): items = url.split('/') del items[-1] url = '/'.join(items) @@ -88,7 +70,7 @@ class Url2File: return self.url2file(url) class TmplUrl2File(Url2File): - def __init__(self,paths,indexes, subffixes=['.tmpl'],inherit=False): + def __init__(self,paths,indexes, subffixes=['.tmpl','.ui' ],inherit=False): self.paths = paths self.u2fs = [ Url2File(p,prefix,indexes,inherit=inherit) \ for p,prefix in paths ]