Compare commits

..

No commits in common. "ed5dcf124584a92e7878a57b6ffe45e33296e465" and "c885d72b8f066ad190d6f4308cfe01ea64ba067a" have entirely different histories.

4 changed files with 7 additions and 49 deletions

1
.gitignore vendored
View File

@ -1,3 +1,2 @@
__pycache__ __pycache__
unpack
*.deb *.deb

View File

@ -7,7 +7,6 @@ if [ -d unpack ]; then
sudo rm -rf unpack sudo rm -rf unpack
fi fi
find usr -type d -name __pycache__ | xargs rm -rf
mkdir -p unpack/DEBIAN mkdir -p unpack/DEBIAN

View File

@ -2,9 +2,7 @@
import os import os
from gi.repository import Gio, WebKit2 from gi.repository import Gio, WebKit2
from ._mimetypes import get_mimetype from ._mimetypes import get_mimetype
from ._version import version
__dir__ = os.path.dirname(os.path.realpath(__file__))
class Protocal: class Protocal:
root = '' root = ''
@ -24,61 +22,29 @@ class Protocal:
return os.path.join(self.root, filepath) return os.path.join(self.root, filepath)
def _get_error_page(self, tips = '404 not found!'):
data = f"""
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>Page not found</title>
<style>body {{text-align: center;-webkit-user-select:none}} cite {{font-size:12px}}</style>
</head>
<body>
<h1>Oops!</h1>
<h2>{tips}</h2>
<hr>
<cite>WebEngine v{version}</cite>
</body>
</html>
"""
return data
def handle_response(self, req): def handle_response(self, req):
schema = req.get_scheme() schema = req.get_scheme()
pathname = req.get_path()[1:] pathname = req.get_path()[1:]
ext = pathname.split('.')[-1] ext = pathname.split('.')[-1]
mimetype = get_mimetype(ext)
if pathname == ext:
pathname = 'index.html'
# print('----------------------------------------') # print('----------------------------------------')
# print(req.get_uri(),schema, pathname, ext, mimetype) # print(req.get_uri(),schema, pathname, ext, get_mimetype(ext))
# print('----------------------------------------') # print('----------------------------------------')
if schema == self.protocal: if schema == self.protocal:
filepath = self.abspath(pathname) data = open(self.abspath(pathname)).read()
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()) data = Gio.MemoryInputStream.new_from_data(data.encode())
# ------- 更多功能的reponse ---------------- # ------- 更多功能的reponse ----------------
# res = WebKit2.URISchemeResponse.new(data, -1) # res = WebKit2.URISchemeResponse.new(data, -1)
# res.set_content_type(mimetype) # res.set_content_type(get_mimetype(ext))
# res.set_http_headers('text/html') # res.set_http_headers('text/html')
# res.set_status(200) # res.set_status(200)
# req.finish_with_response(res) # req.finish_with_response(res)
# ---------------------------------------- # ----------------------------------------
# 简单的response # 简单的response
req.finish(data, -1, mimetype) req.finish(data, -1, get_mimetype(ext))
def create_protocal(root): def create_protocal(root):

View File

@ -125,16 +125,10 @@ class WebEngine(WebKit2.WebView):
def load(self, url = '/index.html'): def load(self, url = '/index.html'):
if url.startswith('http:') or url.startswith('https:'): if self.root is None:
self.load_uri(url) raise EnvironmentError('web root dir not set!')
else: else:
if self.root is None: self.load_uri(f"app://{url}")
raise EnvironmentError('web root dir not set!')
else:
if url.startswith('/'):
self.load_uri(f"app://{url}")
else:
raise ValueError('url must starts with "/"')