From ed5dcf124584a92e7878a57b6ffe45e33296e465 Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 7 Sep 2023 11:39:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0403,404=E9=A1=B5=E9=9D=A2;=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=89=8D=E7=AB=AFhistory=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dist-packages/webengine/gtk3/_protocal.py | 42 +++++++++++++++++-- .../webengine/gtk3/_webengine.py | 5 ++- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/usr/lib/python3/dist-packages/webengine/gtk3/_protocal.py b/usr/lib/python3/dist-packages/webengine/gtk3/_protocal.py index 4cae122..e775a41 100644 --- a/usr/lib/python3/dist-packages/webengine/gtk3/_protocal.py +++ b/usr/lib/python3/dist-packages/webengine/gtk3/_protocal.py @@ -2,7 +2,9 @@ import os from gi.repository import Gio, WebKit2 from ._mimetypes import get_mimetype +from ._version import version +__dir__ = os.path.dirname(os.path.realpath(__file__)) class Protocal: root = '' @@ -22,29 +24,61 @@ class Protocal: return os.path.join(self.root, filepath) + def _get_error_page(self, tips = '404 not found!'): + data = f""" + + + + + Page not found + + + +

Oops!

+

{tips}

+
+ WebEngine v{version} + + + """ + return data + def handle_response(self, req): schema = req.get_scheme() pathname = req.get_path()[1:] ext = pathname.split('.')[-1] + mimetype = get_mimetype(ext) + + if pathname == ext: + pathname = 'index.html' # print('----------------------------------------') - # print(req.get_uri(),schema, pathname, ext, get_mimetype(ext)) + # print(req.get_uri(),schema, pathname, ext, mimetype) # print('----------------------------------------') if schema == self.protocal: - data = open(self.abspath(pathname)).read() + filepath = self.abspath(pathname) + if os.path.isfile(filepath): + try: + with open(filepath) as f: + data = f.read() + except Exception: + data = self._get_error_page('403 Forbidden!') + else: + data = self._get_error_page() + data = Gio.MemoryInputStream.new_from_data(data.encode()) # ------- 更多功能的reponse ---------------- # res = WebKit2.URISchemeResponse.new(data, -1) - # res.set_content_type(get_mimetype(ext)) + # res.set_content_type(mimetype) # res.set_http_headers('text/html') # res.set_status(200) # req.finish_with_response(res) # ---------------------------------------- # 简单的response - req.finish(data, -1, get_mimetype(ext)) + req.finish(data, -1, mimetype) def create_protocal(root): diff --git a/usr/lib/python3/dist-packages/webengine/gtk3/_webengine.py b/usr/lib/python3/dist-packages/webengine/gtk3/_webengine.py index 0b7bfcd..6a17977 100644 --- a/usr/lib/python3/dist-packages/webengine/gtk3/_webengine.py +++ b/usr/lib/python3/dist-packages/webengine/gtk3/_webengine.py @@ -131,7 +131,10 @@ class WebEngine(WebKit2.WebView): if self.root is None: raise EnvironmentError('web root dir not set!') else: - self.load_uri(f"app://{url}") + if url.startswith('/'): + self.load_uri(f"app://{url}") + else: + raise ValueError('url must starts with "/"')