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 user_agent = self.headers.get("User-Agent", "")
111 for prefix in ("facebook", "meta", ):
112 if user_agent.startswith(prefix):
113 self.send_error(HTTPStatus.FORBIDDEN, "Crawlers are not allowed (see robots.txt)")
116 path, query = self._parse_path()
117 if path in {"/index.html", "/index.htm", "/index", "/"}:
118 url = query.get("url", [None])[0]
120 self.send_response(HTTPStatus.OK)
121 self.send_header("Content-type", "text/html")
124 self.server.header_file_bytes.replace(b"$URL", b"yet another http-to-gemini")
126 self.wfile.write(_build_navigation())
127 self.wfile.write(self.server.footer_file_bytes)
131 for _ in range(6): # first request + 5 consecutive redirects
132 parsed = urlparse(url)
133 if parsed.scheme != "gemini":
134 self.send_error(HTTPStatus.BAD_REQUEST, "Only gemini:// URLs are supported")
137 context = ssl.SSLContext()
138 context.check_hostname = False
139 context.verify_mode = ssl.CERT_NONE
140 with socket.create_connection((parsed.hostname, parsed.port or 1965)) as raw_s:
141 with context.wrap_socket(raw_s, server_hostname=parsed.hostname) as s:
142 s.sendall((url + '\r\n').encode("UTF-8"))
143 fp = s.makefile("rb")
144 splitted = fp.readline().decode("UTF-8").strip().split(maxsplit=1)
146 if status.startswith("3") and len(splitted) == 2:
148 url = urljoin(url, splitted[1])
150 if not status.startswith("2"):
152 HTTPStatus.INTERNAL_SERVER_ERROR,
153 f"Unsupported answer: {splitted[0]}",
154 splitted[1] if len(splitted) == 2 else None
157 mime = splitted[1].lower() if len(splitted) == 2 else "text/gemini"
158 if not mime.startswith("text/gemini"):
160 self.send_response(HTTPStatus.OK)
161 self.send_header("Content-type", mime)
163 self.wfile.write(fp.read())
166 m['content-type'] = mime
167 body = fp.read().decode(m.get_param('charset') or "UTF-8")
168 self._convert_gemini_to_html(url, body, mime)
172 raise RuntimeError("Too many redirects")
173 except Exception as error:
174 self.send_error(HTTPStatus.INTERNAL_SERVER_ERROR, str(error))
177 if path == "/favicon.ico":
178 self.send_response(HTTPStatus.OK)
179 self.send_header("Content-type", "image/x-icon")
181 self.wfile.write(self.server.icon_file_bytes)
184 if path == "/style.css":
185 self.send_response(HTTPStatus.OK)
186 self.send_header("Content-type", "text/css")
188 self.wfile.write(self.server.css_file_bytes)
191 if path == "/robots.txt":
192 self.send_response(HTTPStatus.OK)
193 self.send_header("Content-type", "text/plain")
195 self.wfile.write(self.server.robots_file_bytes)
198 self.send_error(HTTPStatus.NOT_FOUND, "File not found")
200 def _convert_gemini_to_html(self, url, body, mime):
201 # convert gemini (body) to html
202 self.send_response(HTTPStatus.OK)
203 self.send_header("Content-type", mime.replace("gemini", "html"))
205 self.wfile.write(self.server.header_file_bytes.replace(b"$URL", url.encode()))
206 self.wfile.write(_build_navigation(url))
207 with _Elem(self.wfile)() as pre:
208 with _Elem(self.wfile)() as ul:
209 for line in body.splitlines():
210 with _FlushBeforeWrite(ul)() as flush_before:
211 if line.startswith("```"):
213 pre.elem = ET.Element("pre")
216 flush_before.commit().write(pre.flush_bytes())
217 elif pre.elem is not None:
219 pre.elem.text += "\r\n"
220 pre.elem.text += line
221 elif line.startswith("=>") and line[2:].strip():
224 splitted = line[2:].strip().split(maxsplit=1)
225 target = urljoin(url, splitted[0])
226 a = ET.SubElement(p, "a")
227 if urlparse(target).scheme == "gemini":
228 a.attrib.update(href="/?" + urlencode({"url": target}))
230 a.attrib.update(target="_blank", href=target)
231 a.text = splitted[1] if len(splitted) > 1 else target
232 flush_before.commit().write(ET.tostring(p) + b"\r\n")
233 elif line.startswith("#") and len(line.split()) > 1:
234 splitted = line.split(maxsplit=1)
235 h = ET.Element("h" + str(len(splitted[0])))
237 flush_before.commit().write(ET.tostring(h) + b"\r\n")
238 elif line.startswith("* ") and line[2:].strip():
240 ul.elem = ET.Element("ul")
241 ET.SubElement(ul.elem, "li").text = line[2:].strip()
242 flush_before.cancel()
243 elif line.startswith("> ") and line[2:].strip():
244 blockquote = ET.Element("blockquote")
245 ET.SubElement(blockquote, "p").text = line[2:].strip()
246 flush_before.commit().write(ET.tostring(blockquote) + b"\r\n")
251 flush_before.commit().write(ET.tostring(p) + b"\r\n")
252 self.wfile.write(self.server.footer_file_bytes)
256 parser = ArgumentParser()
257 parser.add_argument("--address", default="127.0.0.1", help="bind to this address (default: %(default)s)")
258 parser.add_argument("--port", default=8000, type=int, help="bind to this port (default: %(default)s)")
259 parser.add_argument("--header", required=True, help="path to `index.html.header`")
260 parser.add_argument("--footer", required=True, help="path to `index.html.footer`")
261 parser.add_argument("--icon", required=True , help="path to `favicon.ico`")
262 parser.add_argument("--css", required=True, help="path to `style.css`")
263 parser.add_argument("--robots", required=True, help="path to `robots.txt`")
264 args = parser.parse_args()
267 (args.address, args.port),
269 header_file_path=args.header,
270 footer_file_path=args.footer,
271 icon_file_path=args.icon,
272 css_file_path=args.css,
273 robots_file_path=args.robots,
275 sock_host, sock_port = http_server.socket.getsockname()[:2]
276 print(f"HTTP server started ({sock_host}:{sock_port})...")
278 http_server.serve_forever()
279 except KeyboardInterrupt:
280 print("\nKeyboard interrupt received, exiting.")
283 if __name__ == '__main__':