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

View File

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