commit 15efabff9a5b649dd6576c2a6e29b1ccb0c7d9ed from: Aleksey Ryndin date: Fri Sep 13 16:47:27 2024 UTC Fix: handle HTTPError and URLError commit - 9f7edfa033174934d993217ee0c3fde8502fc63d commit + 15efabff9a5b649dd6576c2a6e29b1ccb0c7d9ed blob - dd45d20eca583fc1e5830281346f47a43b4dcd63 blob + 6d6c02dd1e501e90365524b747af4b0764e33eed --- lonk.py +++ lonk.py @@ -3,7 +3,7 @@ from sqlite3 import connect as sqlite3_connect from json import loads as json_loads from pathlib import Path from sys import stdout -from urllib.error import HTTPError +from urllib.error import HTTPError, URLError from urllib.parse import urlencode, urlunsplit, urljoin, urlsplit, parse_qsl from urllib.request import urlopen from html.parser import HTMLParser @@ -230,6 +230,8 @@ def demo(lonk_url): else: header = f'## From: {honk["Handle"]}, {honk["Date"]}' collected[convoy] = (honk, header) + + print("20 text/gemini\r") print("# 𝓗 onk\n") line = f"=> {_build_url(path='atme')} @me" @@ -248,22 +250,19 @@ def demo(lonk_url): def proxy(mime, url): - try: - with urlopen(url, timeout=10) as f: - stdout.buffer.write(b"20 " + mime.encode() + b"\r\n") - while True: - content = f.read(512 * 1024) - if not content: - break - stdout.buffer.write(content) - except HTTPError as error: - print(f"43 Remote server return {error.code}: {error.reason}\r") + with urlopen(url, timeout=10) as f: + stdout.buffer.write(b"20 " + mime.encode() + b"\r\n") + while True: + content = f.read(512 * 1024) + if not content: + break + stdout.buffer.write(content) + def vgi(cert_hash): url = urlsplit(input().strip()) page = [part for part in url.path.split("/") if part][-1] if page == "lonk": - print("20 text/gemini\r") demo(url) elif page == "proxy": query = {pair[0]: pair[1] for pair in parse_qsl(url.query)} @@ -280,6 +279,11 @@ if __name__ == '__main__': cert_hash = environ.get("VGI_CERT_HASH") if cert_hash: - vgi(cert_hash) + try: + vgi(cert_hash) + except HTTPError as error: + print(f"43 Remote server return {error.code}: {error.reason}\r") + except URLError as error: + print(f"43 Error while trying to access remote server: {error.reason}\r") else: print("60 Certificate required\r")