1 """Yet another http-to-gemini."""
4 import xml.etree.ElementTree as ET
5 from argparse import ArgumentParser
6 from email.message import Message
7 from http import HTTPStatus
8 from http.server import HTTPServer, BaseHTTPRequestHandler
9 from urllib.parse import parse_qs, urlparse, urljoin, urlencode, uses_relative, uses_netloc
10 from contextlib import contextmanager
13 uses_relative.append("gemini")
14 uses_netloc.append("gemini")
17 def _build_navigation(url=None):
18 form = ET.Element("form")
19 form.attrib.update(method="get")
20 input_ = ET.SubElement(form, "input")
26 "placeholder": "gemini://",
27 "autocomplete": "off",
32 input_.attrib.update(value=url)
33 input_ = ET.SubElement(form, "input")
34 input_.attrib.update(**{"type": "submit", "value": "go!"})
35 return ET.tostring(form) + b"\r\n"
38 class _HTTPServer(HTTPServer):
42 header_file_path=None,
43 footer_file_path=None,
46 robots_file_path=None,
49 super().__init__(*args, **kwargs)
50 with open(header_file_path, "rb") as f:
51 self.header_file_bytes = f.read()
52 with open(footer_file_path, "rb") as f:
53 self.footer_file_bytes = f.read()
54 with open(icon_file_path, "rb") as f:
55 self.icon_file_bytes = f.read()
56 with open(css_file_path, "rb") as f:
57 self.css_file_bytes = f.read()
58 with open(robots_file_path, "rb") as f:
59 self.robots_file_bytes = f.read()
63 def __init__(self, file):
72 def flush_bytes(self):
76 rv = ET.tostring(self.elem) + b"\r\n"
81 self.file.write(self.flush_bytes())
84 class _FlushBeforeWrite:
85 def __init__(self, elem):
87 self._file = elem.file
93 if self._elem is not None:
104 class _RequestHandler(BaseHTTPRequestHandler):
105 def _parse_path(self):
106 _, _, path, _, query, _ = urlparse(self.path)
107 return path, parse_qs(query) if query else {}
110 path, query = self._parse_path()
111 if path in {"/index.html", "/index.htm", "/index", "/"}:
112 url = query.get("url", [None])[0]
114 self.send_response(HTTPStatus.OK)
115 self.send_header("Content-type", "text/html")
118 self.server.header_file_bytes.replace(b"$URL", b"yet another http-to-gemini")
120 self.wfile.write(_build_navigation())
121 self.wfile.write(self.server.footer_file_bytes)
125 for _ in range(6): # first request + 5 consecutive redirects
126 parsed = urlparse(url)
127 if parsed.scheme != "gemini":
128 self.send_error(HTTPStatus.BAD_REQUEST, "Only gemini:// URLs are supported")
131 context = ssl.SSLContext()
132 context.check_hostname = False
133 context.verify_mode = ssl.CERT_NONE
134 with socket.create_connection((parsed.hostname, parsed.port or 1965)) as raw_s:
135 with context.wrap_socket(raw_s, server_hostname=parsed.hostname) as s:
136 s.sendall((url + '\r\n').encode("UTF-8"))
137 fp = s.makefile("rb")
138 splitted = fp.readline().decode("UTF-8").strip().split(maxsplit=1)
140 if status.startswith("3") and len(splitted) == 2:
142 url = urljoin(url, splitted[1])
144 if not status.startswith("2"):
146 HTTPStatus.INTERNAL_SERVER_ERROR,
147 f"Unsupported answer: {splitted[0]}",
148 splitted[1] if len(splitted) == 2 else None
151 mime = splitted[1].lower() if len(splitted) == 2 else "text/gemini"
152 if not mime.startswith("text/gemini"):
154 self.send_response(HTTPStatus.OK)
155 self.send_header("Content-type", mime)
157 self.wfile.write(fp.read())
160 m['content-type'] = mime
161 body = fp.read().decode(m.get_param('charset') or "UTF-8")
162 self._convert_gemini_to_html(url, body, mime)
166 raise RuntimeError("Too many redirects")
167 except Exception as error:
168 self.send_error(HTTPStatus.INTERNAL_SERVER_ERROR, str(error))
171 if path == "/favicon.ico":
172 self.send_response(HTTPStatus.OK)
173 self.send_header("Content-type", "image/x-icon")
175 self.wfile.write(self.server.icon_file_bytes)
178 if path == "/style.css":
179 self.send_response(HTTPStatus.OK)
180 self.send_header("Content-type", "text/css")
182 self.wfile.write(self.server.css_file_bytes)
185 if path == "/robots.txt":
186 self.send_response(HTTPStatus.OK)
187 self.send_header("Content-type", "text/plain")
189 self.wfile.write(self.server.robots_file_bytes)
192 self.send_error(HTTPStatus.NOT_FOUND, "File not found")
194 def _convert_gemini_to_html(self, url, body, mime):
195 # convert gemini (body) to html
196 self.send_response(HTTPStatus.OK)
197 self.send_header("Content-type", mime.replace("gemini", "html"))
199 self.wfile.write(self.server.header_file_bytes.replace(b"$URL", url.encode()))
200 self.wfile.write(_build_navigation(url))
201 with _Elem(self.wfile)() as pre:
202 with _Elem(self.wfile)() as ul:
203 for line in body.splitlines():
204 with _FlushBeforeWrite(ul)() as flush_before:
205 if line.startswith("```"):
207 pre.elem = ET.Element("pre")
210 flush_before.commit().write(pre.flush_bytes())
211 elif pre.elem is not None:
213 pre.elem.text += "\r\n"
214 pre.elem.text += line
215 elif line.startswith("=>") and line[2:].strip():
218 splitted = line[2:].strip().split(maxsplit=1)
219 target = urljoin(url, splitted[0])
220 a = ET.SubElement(p, "a")
221 if urlparse(target).scheme == "gemini":
222 a.attrib.update(href="/?" + urlencode({"url": target}))
224 a.attrib.update(target="_blank", href=target)
225 a.text = splitted[1] if len(splitted) > 1 else target
226 flush_before.commit().write(ET.tostring(p) + b"\r\n")
227 elif line.startswith("#") and len(line.split()) > 1:
228 splitted = line.split(maxsplit=1)
229 h = ET.Element("h" + str(len(splitted[0])))
231 flush_before.commit().write(ET.tostring(h) + b"\r\n")
232 elif line.startswith("* ") and line[2:].strip():
234 ul.elem = ET.Element("ul")
235 ET.SubElement(ul.elem, "li").text = line[2:].strip()
236 flush_before.cancel()
237 elif line.startswith("> ") and line[2:].strip():
238 blockquote = ET.Element("blockquote")
239 ET.SubElement(blockquote, "p").text = line[2:].strip()
240 flush_before.commit().write(ET.tostring(blockquote) + b"\r\n")
245 flush_before.commit().write(ET.tostring(p) + b"\r\n")
246 self.wfile.write(self.server.footer_file_bytes)
250 parser = ArgumentParser()
251 parser.add_argument("--address", default="127.0.0.1", help="bind to this address (default: %(default)s)")
252 parser.add_argument("--port", default=8000, type=int, help="bind to this port (default: %(default)s)")
253 parser.add_argument("--header", required=True, help="path to `index.html.header`")
254 parser.add_argument("--footer", required=True, help="path to `index.html.footer`")
255 parser.add_argument("--icon", required=True , help="path to `favicon.ico`")
256 parser.add_argument("--css", required=True, help="path to `style.css`")
257 parser.add_argument("--robots", required=True, help="path to `robots.txt`")
258 args = parser.parse_args()
261 (args.address, args.port),
263 header_file_path=args.header,
264 footer_file_path=args.footer,
265 icon_file_path=args.icon,
266 css_file_path=args.css,
267 robots_file_path=args.robots,
269 sock_host, sock_port = http_server.socket.getsockname()[:2]
270 print(f"HTTP server started ({sock_host}:{sock_port})...")
272 http_server.serve_forever()
273 except KeyboardInterrupt:
274 print("\nKeyboard interrupt received, exiting.")
277 if __name__ == '__main__':