summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Singleton <matt@xcolour.net>2022-01-16 21:45:30 -0600
committerMatt Singleton <matt@xcolour.net>2022-01-16 21:53:09 -0600
commit704be7d2e7db33635fc9de684c8b3916cd798c68 (patch)
tree188ad6c77eec042809f963fcc52100c649a5967a
parent1ef62c2c3044b5b11dae92a08be5d83a780392b4 (diff)
support input forms
-rw-r--r--TODO.md4
-rwxr-xr-xbrowser.py8
-rw-r--r--gemini.py26
-rw-r--r--input_template.html16
4 files changed, 49 insertions, 5 deletions
diff --git a/TODO.md b/TODO.md
index 93e8d78..450236c 100644
--- a/TODO.md
+++ b/TODO.md
@@ -9,6 +9,6 @@
- [x] nice typography
- [x] html templates
- [ ] deal with non utf8 charsets
- - [ ] handle all response codes
+ - [x] handle all response codes
- [x] errors (4x, 5x)
- - [ ] search
+ - [x] search
diff --git a/browser.py b/browser.py
index f6107e9..f5475ab 100755
--- a/browser.py
+++ b/browser.py
@@ -22,8 +22,9 @@ class GeminiPage(QtWebEngineCore.QWebEnginePage):
class GeminiSchemeHandler(QtWebEngineCore.QWebEngineUrlSchemeHandler):
def requestStarted(self, request):
- print(request.requestUrl().toString())
- gem = gemini.get(request.requestUrl().toString())
+ request_url = gemini.hack_url(request.requestUrl().toString())
+ print(request_url)
+ gem = gemini.get(request_url)
print(gem['status'], gem['meta'])
buf = QtCore.QBuffer(parent=request)
buf.open(QtCore.QIODevice.WriteOnly)
@@ -39,7 +40,8 @@ class GUrlBar(QtWidgets.QLineEdit):
QtWidgets.QLineEdit.__init__(self)
def setUrl(self, url):
- return self.setText(url.toDisplayString())
+ url = gemini.hack_url(url.toDisplayString())
+ return self.setText(url)
class GBrowser(QtWidgets.QMainWindow):
diff --git a/gemini.py b/gemini.py
index 275eab9..7bedda5 100644
--- a/gemini.py
+++ b/gemini.py
@@ -24,6 +24,9 @@ def gem2html(gem: dict) -> str:
parser = fsm.Parser(gem['body'].split('\n'), body)
parser.parse()
params['body'] = body.getvalue()
+ elif gem['status'][0] == '1':
+ template = string.Template(open('input_template.html').read())
+ params['meta'] = gem['meta']
else:
template = string.Template(open('error_template.html').read())
if gem['status'] == '00':
@@ -63,6 +66,29 @@ def get(url: str, follow_redirects: bool = True) -> dict:
return response
+def hack_url(url: str) -> str:
+ """
+ An ugly hack to reformat input queries the way gemini wants them:
+ ?<query>
+ Rather than the default way an html get form renders them:
+ ?<inputname>=<query>
+ I don't think this ever *should* break but I guess it *could*.
+ """
+ url_parts = urllib.parse.urlsplit(url)
+ query = urllib.parse.parse_qs(url_parts.query)
+ if len(query) == 1 and '__client_internal_input' in query and len(query['__client_internal_input']) == 1:
+ query = str(query['__client_internal_input'][0])
+ url = urllib.parse.urlunsplit((
+ url_parts.scheme,
+ url_parts.netloc,
+ url_parts.path,
+ query,
+ url_parts.fragment,
+ ))
+ url_parts = urllib.parse.urlsplit(url)
+ return url
+
+
def _parse_meta(meta: str) -> dict:
mime, _, params_text = meta.lower().strip().partition(';')
params = {}
diff --git a/input_template.html b/input_template.html
new file mode 100644
index 0000000..defbb7a
--- /dev/null
+++ b/input_template.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html lang="en">
+ <html>
+ <head>
+ <meta charset="$charset"/>
+ <style type="text/css">
+$css
+ </style>
+ </head>
+ <body>
+ <form action="" method="get">
+ <label for="input">$meta</label>
+ <input type="text" name="__client_internal_input" id="input" required>
+ <input type="submit">
+ </body>
+ </html>