summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbrowser.py35
-rw-r--r--gemini.py16
2 files changed, 16 insertions, 35 deletions
diff --git a/browser.py b/browser.py
index 21e9293..16f1c29 100755
--- a/browser.py
+++ b/browser.py
@@ -11,8 +11,6 @@ class GViewport(QtWidgets.QTextBrowser):
hoverUrlChanged = QtCore.Signal(str)
def __init__(self, address_bar):
- self._current_url = None
- self._last_redirect = (QtCore.QUrl(), {})
self.address_bar = address_bar
self._hover_url = None
QtWidgets.QTextBrowser.__init__(self)
@@ -20,49 +18,20 @@ class GViewport(QtWidgets.QTextBrowser):
def mouseMoveEvent(self, event):
cur = self.cursorForPosition(event.localPos().toPoint())
hover_url = cur.charFormat().anchorHref()
- hover_url = gemini.urljoin(self._current_url, hover_url)
+ hover_url = gemini.urljoin(self.address_bar.text(), hover_url)
if hover_url != self._hover_url:
self._hover_url = hover_url
self.hoverUrlChanged.emit(self._hover_url)
return super().mouseMoveEvent(event)
def loadResource(self, type_, url):
- current_url = QtCore.QUrl(self._current_url)
- if self._last_redirect[0].toString() == url.toString():
- gem = self._last_redirect[1]
- else:
- if not url.scheme():
- url.setScheme(current_url.scheme())
- if not url.host():
- url.setHost(current_url.host())
- if url.port() == -1:
- url.setPort(1965)
- if not url.path().startswith('/'):
- url.setPath(current_url.path().rsplit('/', 1)[0] + '/' + url.path())
- gem = gemini.get(gemini.urljoin(current_url.toString(), url.toString()))
+ gem = gemini.get(url.toString())
if 'body' in gem:
html = gemini.gem2html(gem['body'])
else:
html = '<h1>{} {}</h1>'.format(gem['status'], gem['meta'])
- self._current_url = url.toString()
return html
- def setSource(self, url):
- if url.scheme() != 'gemini':
- return
- gem = gemini.get(gemini.urljoin(self._current_url, url.toString()))
- while gem['status'][0] == '3':
- url = QtCore.QUrl(gem['meta'])
- if url.port() == 1965:
- url.setPort(-1)
- print('redirect: {}'.format(url))
- gem = gemini.get(gemini.urljoin(self._current_url, url.toString()))
- self._last_redirect = (url, gem)
- if url.port() == 1965:
- url.setPort(-1)
- print('setSource: {}'.format(url))
- return super().setSource(url)
-
def setRawSource(self):
return self.setSource(QtCore.QUrl(self.address_bar.text()))
diff --git a/gemini.py b/gemini.py
index 58f3358..beb2d5c 100644
--- a/gemini.py
+++ b/gemini.py
@@ -78,7 +78,19 @@ def urljoin(base: str, url: str) -> str:
url = re.sub('^gemini:', 'http:', url)
return re.sub('^http:', 'gemini:', urllib.parse.urljoin(base, url))
-def get(url: str) -> dict:
+def get(url: str, follow_redirects: bool = True) -> dict:
+ response = _get(url)
+ if follow_redirects is True:
+ count = 0
+ while response['status'][0] == '3':
+ count += 1
+ if count > 20:
+ raise Exception('Redirect loop')
+ print('redirect: {}'.format(response['meta']))
+ response = _get(response['meta'])
+ return response
+
+def _get(url: str) -> dict:
url_parts = urllib.parse.urlsplit(url)
if len(url_parts.path) == 0:
return {
@@ -97,7 +109,7 @@ def get(url: str) -> dict:
port = 1965 if url_parts.port is None else url_parts.port
with socket.create_connection((url_parts.hostname, port)) as sock:
with context.wrap_socket(sock, server_hostname=url_parts.hostname) as ssock:
- ssock.sendall('gemini://{}{}\r\n'.format(url_parts.hostname, url_parts.path).encode('utf8'))
+ ssock.sendall('{url}\r\n'.format(url=url).encode('utf8'))
fp = ssock.makefile(mode='rb')
header = fp.readline(1027)
status, meta = header.decode('utf8').split(None, 1)