This commit is contained in:
yumoqing 2021-05-06 10:09:01 +08:00
parent 8394e18f4e
commit 598889c678
2 changed files with 33 additions and 27 deletions

View File

@ -192,11 +192,13 @@ class ProcessorResource(StaticResource,Url2File):
self.y_env.gethost = gethost self.y_env.gethost = gethost
self.y_env.path_call = partial(self.path_call,request) self.y_env.path_call = partial(self.path_call,request)
self.user = await auth.get_auth(request) self.user = await auth.get_auth(request)
self.request_filename = self.url2file(str(request.url))
path = request.path path = request.path
config = getConfig() config = getConfig()
if config.website.dbadm and path.startswith(config.website.dbadm): if config.website.dbadm and path.startswith(config.website.dbadm):
pp = path.split('/')[2:] pp = path.split('/')[2:]
if len(pp)<3: if len(pp)<3:
print(str(request.url), 'not found')
raise HTTPNotFound raise HTTPNotFound
dbname = pp[0] dbname = pp[0]
tablename = pp[1] tablename = pp[1]
@ -206,6 +208,7 @@ class ProcessorResource(StaticResource,Url2File):
if config.website.dbrest and path.startswith(config.website.dbrest): if config.website.dbrest and path.startswith(config.website.dbrest):
pp = path.split('/')[2:] pp = path.split('/')[2:]
if len(pp)<2: if len(pp)<2:
print(str(request.url), 'not found')
raise HTTPNotFound raise HTTPNotFound
dbname = pp[0] dbname = pp[0]
tablename = pp[1] tablename = pp[1]
@ -217,22 +220,24 @@ class ProcessorResource(StaticResource,Url2File):
if config.website.download and path.startswith(config.website.download): if config.website.download and path.startswith(config.website.download):
pp = path.split('/')[2:] pp = path.split('/')[2:]
if len(pp)<1: if len(pp)<1:
print(str(request.url), 'not found')
raise HTTPNotFound raise HTTPNotFound
dp = '/'.join(pp) dp = '/'.join(pp)
path = path_decode(dp) path = path_decode(dp)
return await file_download(request, path) return await file_download(request, path)
processor = self.url2processor(request, str(request.url)) # processor = self.url2processor(request, str(request.url))
processor = self.url2processor(request, str(request.url), self.request_filename)
if processor: if processor:
return await processor.handle(request) return await processor.handle(request)
filepath = self.url2file(str(request.url)) if self.request_filename and self.isHtml(self.request_filename):
if filepath and self.isHtml(filepath): return await self.html_handle(request, self.request_filename)
return await self.html_handle(request, filepath)
if filepath and os.path.isdir(filepath): if self.request_filename and os.path.isdir(self.request_filename):
config = getConfig() config = getConfig()
if not config.website.allowListFolder: if not config.website.allowListFolder:
print(str(request.url), 'not found')
raise HTTPNotFound raise HTTPNotFound
return await super()._handle(request) return await super()._handle(request)
@ -261,7 +266,7 @@ class ProcessorResource(StaticResource,Url2File):
except Exception as e: except Exception as e:
return False return False
def url2processor(self, request, url): def url2processor(self, request, url, fpath):
config = getConfig() config = getConfig()
url = self.entireUrl(request, url) url = self.entireUrl(request, url)
host = '/'.join(str(request.url).split('/')[:3]) host = '/'.join(str(request.url).split('/')[:3])
@ -273,8 +278,12 @@ class ProcessorResource(StaticResource,Url2File):
processor = FunctionProcessor(path,self,a) processor = FunctionProcessor(path,self,a)
return processor return processor
if self.request_filename is None:
print(url, 'not found')
raise HTTPNotFound
for word, handlername in self.y_processors: for word, handlername in self.y_processors:
if path.endswith(word): if fpath.endswith(word):
Klass = getProcessor(handlername) Klass = getProcessor(handlername)
processor = Klass(path,self) processor = Klass(path,self)
return processor return processor
@ -287,29 +296,17 @@ class ProcessorResource(StaticResource,Url2File):
if url.startswith('/'): if url.startswith('/'):
return '%s%s' % (h,url) return '%s%s' % (h,url)
path = request.path path = request.path
fn = self.url2file(str(request.url)) if self.request_filename and os.path.isdir(self.request_filename):
if fn and os.path.isdir(fn):
path = '%s/oops' % path path = '%s/oops' % path
p = self.relatedurl(path,url) p = self.relatedurl(path,url)
return '%s%s' % (h, p) return '%s%s' % (h, p)
async def path_call(self,request, path, params={}): async def path_call(self,request, path, params={}):
url = self.entireUrl(request, path) url = self.entireUrl(request, path)
processor = self.url2processor(request, url) fpath = self.url2file(url)
processor = self.url2processor(request, url, fpath)
return await processor.path_call(request, params=params) return await processor.path_call(request, params=params)
def url_call(self,request, url,params={}):
processor = self.url2processor(request, url)
if processor:
# self.y_env.update(params)
loop = asyncio.get_event_loop()
loop.run_until_complete(processor.execute(request))
return processor.content
long_url = self.entireUrl(request,url)
hc = Http_Client()
x = hc(long_url,method=method,params=params)
return x
def absUrl(self,request,url): def absUrl(self,request,url):
http='http://' http='http://'
https='https://' https='https://'

View File

@ -21,15 +21,20 @@ class Url2File:
break break
return '/'.join(items) return '/'.join(items)
def url2file(self, url: str): def url2ospath(self, url: str) -> str:
url = url.split('?')[0]
if url[-1] == '/': if len(url) > 0 and url[-1] == '/':
url = url[:-1] url = url[:-1]
paths = url.split('/') paths = url.split('/')
if url.startswith('http://') or url.startswith('https://'): if url.startswith('http://') or url.startswith('https://'):
paths = paths[3:] paths = paths[3:]
f = os.path.join(self.path,*paths) f = os.path.join(self.path,*paths)
real_path = os.path.abspath(f) real_path = os.path.abspath(f)
return real_path
def url2file(self, url: str) -> str:
url = url.split('?')[0]
real_path = self.url2ospath(url)
if os.path.isdir(real_path): if os.path.isdir(real_path):
for idx in self.indexes: for idx in self.indexes:
p = os.path.join(real_path,idx) p = os.path.join(real_path,idx)
@ -39,8 +44,12 @@ class Url2File:
if os.path.isfile(real_path): if os.path.isfile(real_path):
return real_path return real_path
if not os.path.isdir(os.path.dirname(real_path)):
return None
if not self.inherit: if not self.inherit:
return None return None
items = url.split('/') items = url.split('/')
if len(items) > 2: if len(items) > 2:
del items[-2] del items[-2]
@ -49,10 +58,10 @@ class Url2File:
return None return None
def relatedurl(self,url: str, name: str) -> str: def relatedurl(self,url: str, name: str) -> str:
if url[-1] == '/': if len(url) > 0 and url[-1] == '/':
url = url[:-1] url = url[:-1]
fp = self.url2file(url) fp = self.url2ospath(url)
if os.path.isfile(fp): if os.path.isfile(fp):
items = url.split('/') items = url.split('/')
del items[-1] del items[-1]