gemini.git

going-flying.com gemini git repository

summary

tree

log

refs

fc1f9b10bf10de588aeccaab20dd7053f401ca54 - Matthew Ernisse - 1598914186

try to build a rfc proxy

view tree

view raw

diff --git a/cgi-bin/rfc b/cgi-bin/rfc
new file mode 100644
index 0000000..7227418
--- /dev/null
+++ b/cgi-bin/rfc
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+
+import os
+import re
+import requests
+import sys
+
+def check_requested_path():
+	''' Molly Brown sends us the rest of the requested url on stdin, so
+	read that and check to make sure it is a request for an RFC.
+	'''
+	stdin = sys.stdin.read(1026)
+	stdin = stdin.strip()
+
+	if len(stdin) > 1024:
+		raise ValueError('URL longer than 1024 bytes.')
+
+	matches = re.search(r'^rfc\d+\.txt', stdin)
+	if not matches:
+		raise ValueError('Request must be a RFC text file.')
+
+	return stdin
+
+
+def get_rfc(rfc):
+	''' Return the text contents of an RFC. '''
+	resp = requests.get(
+		f'https://tools.ietf.org/rfc/{rfc}.txt',
+		headers={
+			'User-Agent': 'gemini-rfc-proxy/matt@going-flying.com'
+		}
+	)
+
+	resp.raise_for_status()
+	return resp.text
+
+
+if __name__ == "__main__":
+	try:
+		rfc = check_requested_path()
+		body = get_rfc(rfc)
+	except ValueError, e:
+		print(f'59 BAD REQUEST {e}')
+		sys.exit()
+
+	except HTTPError, e:
+		if e.errno > 399 and e.errno < 500:
+			print(f'43 {e.strerror}')
+			sys.exit()
+		else:
+			print(f'53 {e.strerror}')
+			sys.exit()
+
+	print('20 text/plain\r\n')
+	print(body)