From 01b4fd03f3c2c09eb0337aeade83daf93856c408 Mon Sep 17 00:00:00 2001 From: Matt Singleton Date: Mon, 7 Sep 2020 14:31:13 -0500 Subject: use stdlib url parsing in gemini module --- gemini.py | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) (limited to 'gemini.py') diff --git a/gemini.py b/gemini.py index c0d09c1..58f3358 100644 --- a/gemini.py +++ b/gemini.py @@ -1,10 +1,12 @@ +import re import socket import ssl +import urllib.parse -def htmlescape(text): +def htmlescape(text: str) -> str: return text.replace('<', '<').replace('>', '>') -def gem2html(gem): +def gem2html(gem: str) -> str: html = [] state = 'text' blanklines = 0 @@ -69,33 +71,33 @@ def gem2html(gem): html.append('

{}

'.format(htmlescape(line))) return '\n'.join(html) -def absolute_url(base, url): - """ - modifies `url` in place - """ - if not url.scheme(): - url.setScheme(base.scheme()) - if not url.host(): - url.setHost(base.host()) - if not url.path().startswith('/'): - url.setPath(base.path().rsplit('/', 1)[0] + '/' + url.path()) - if url.port() == -1: - url.setPort(1965) - return url +def urljoin(base: str, url: str) -> str: + if base is None: + return url + base = re.sub('^gemini:', 'http:', base) + url = re.sub('^gemini:', 'http:', url) + return re.sub('^http:', 'gemini:', urllib.parse.urljoin(base, url)) -def get(url): - if len(url.path()) == 0: - url.setPath('/') +def get(url: str) -> dict: + url_parts = urllib.parse.urlsplit(url) + if len(url_parts.path) == 0: return { 'status': '32', - 'meta': url.toDisplayString(), + 'meta': urllib.parse.urlunsplit(( + url_parts.scheme, + url_parts.netloc, + '/', + url_parts.query, + url_parts.fragment, + )) } context = ssl.create_default_context() context.check_hostname = False context.verify_mode = ssl.CERT_NONE - with socket.create_connection((url.host(), url.port())) as sock: - with context.wrap_socket(sock, server_hostname=url.host()) as ssock: - ssock.sendall('gemini://{}{}\r\n'.format(url.host(), url.path()).encode('utf8')) + 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')) fp = ssock.makefile(mode='rb') header = fp.readline(1027) status, meta = header.decode('utf8').split(None, 1) -- cgit v1.2.3