This commit is contained in:
yumoqing 2024-02-19 11:18:50 +08:00
parent 21143f0138
commit 71d6a3d528
2 changed files with 22 additions and 11 deletions

View File

@ -106,9 +106,11 @@ class ProcessorResource(AppLogger, StaticResource,Url2File):
def setIndexes(self, indexes):
self.y_indexes = indexes
def abspath(self, request, path:str):
url = self.entireUrl(request, path)
fname = self.url2file(url)
path = self.url2path(url)
fname = self.url2file(path)
return fname
async def getPostData(self,request: Request) -> dict:
@ -187,6 +189,9 @@ class ProcessorResource(AppLogger, StaticResource,Url2File):
self._host = request.headers.get('X-Forwarded-Host', host)
self._port = request.headers.get('X-Forwarded-Port', port)
self._prepath = request.headers.get('X-Forwarded-Prepath', '')
self._preurl = f'{self._scheme}://{self._host}:{self._port}{self._prepath}'
print(f'{request.path=}, {self._preurl=}')
async def _handle(self,request:Request) -> StreamResponse:
clientkeys = {
@ -400,12 +405,12 @@ class ProcessorResource(AppLogger, StaticResource,Url2File):
url.startswith('ws://') or \
url.startswith('wss://'):
return url
scheme = request.headers.get('X-Forwarded-Scheme') or request.scheme
port = request.headers.get('X-Forwarded-Port') or str(request['port'])
prepath = request.headers.get('X-Forwarded-Prepath') or ''
scheme = self._scheme
port = self._port
prepath = self._prepath
if prepath != '':
prepath = f'/{prepath}'
h = self.gethost(request)
h = self._host
if url.startswith('/'):
return f'{scheme}://{h}:{port}{prepath}{url}'
path = request.path
@ -414,10 +419,16 @@ class ProcessorResource(AppLogger, StaticResource,Url2File):
p = self.relatedurl(path,url)
return f'{scheme}://{h}:{port}{prepath}{p}'
def url2path(self, url):
if url.startswith(self._prepath):
return url[len(self._prepath):]
return url
async def path_call(self, request, path, params={}):
url = self.entireUrl(request, path)
# print(f'{path=}, after entireUrl(), {url=}')
fpath = self.url2file(url)
path = self.url2path(url)
fpath = self.url2file(path)
processor = self.url2processor(request, url, fpath)
new_request = request.clone(rel_url=url)
# new_request.path = path

View File

@ -5,7 +5,7 @@ import os
class Url2File:
def __init__(self,path:str,prefix: str,
indexes: list, inherit: bool=False):
self.path = path
self.rootpath = path
self.starts = prefix
self.indexes = indexes
self.inherit = inherit
@ -31,7 +31,7 @@ class Url2File:
url.startswith('ws://') or \
url.startswith('wss://'):
paths = paths[3:]
f = os.path.join(self.path,*paths)
f = os.path.join(self.rootpath,*paths)
real_path = os.path.abspath(f)
# print(f'{real_path=}, {url=}, {f=}')
return real_path
@ -51,11 +51,11 @@ class Url2File:
return real_path
if not os.path.isdir(os.path.dirname(real_path)):
# print(f'url2file() return None, {real_path=}, {url=},{ourl=}, {self.path=}')
# print(f'url2file() return None, {real_path=}, {url=},{ourl=}, {self.rootpath=}')
return None
if not self.inherit:
# print(f'url2file() return None, self.inherit is false, {url:}, {self.path=}')
# print(f'url2file() return None, self.inherit is false, {url:}, {self.rootpath=}')
return None
items = url.split('/')
@ -65,7 +65,7 @@ class Url2File:
url = '/'.join(items)
# print(f'{oldurl=}, {url=}')
return self.url2file(url)
# print(f'url2file() return None finally, {items:}, {url=}, {ourl=}, {self.path=}')
# print(f'url2file() return None finally, {items:}, {url=}, {ourl=}, {self.rootpath=}')
return None
def relatedurl(self,url: str, name: str) -> str: