From 71d6a3d528725ae34cfb1af08f162b056affa0f2 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Mon, 19 Feb 2024 11:18:50 +0800 Subject: [PATCH] bugfix --- ahserver/processorResource.py | 23 +++++++++++++++++------ ahserver/url2file.py | 10 +++++----- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/ahserver/processorResource.py b/ahserver/processorResource.py index 39e85e4..1efb1c5 100755 --- a/ahserver/processorResource.py +++ b/ahserver/processorResource.py @@ -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 diff --git a/ahserver/url2file.py b/ahserver/url2file.py index dc5d720..982cc0f 100755 --- a/ahserver/url2file.py +++ b/ahserver/url2file.py @@ -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: