commit cd385be7df5f12242aaa65391f0fdfbc86713af6 Author: Peter J. Holzer Date: Tue Sep 15 09:01:40 2020 +0200 Serve archived pages diff --git a/awd b/awd new file mode 100755 index 0000000..b14fdf2 --- /dev/null +++ b/awd @@ -0,0 +1,58 @@ +#!/usr/bin/python3 + +import argparse +import http +import http.server as hs +import ssl + +import psycopg2 +import psycopg2.extras + +class WSqlShRH(hs.BaseHTTPRequestHandler): + def do_GET(self): + self.log_message(f"checking {self.path}") + db.rollback() + csr = db.cursor(cursor_factory=psycopg2.extras.DictCursor) + csr.execute("select * from documents where url = %s", + (args.baseurl + self.path,)) + dr = csr.fetchone() + if dr: + self.send_response(dr["code"]) + self.send_header("Content-Type", dr["content_type"]) + self.send_header("Last-Modified", dr["last_modified"]) + self.end_headers() + csr.execute("select * from contents where sha512 = %s", + (dr["content_sha512"],)) + cr = csr.fetchone() + if cr: + self.wfile.write(cr["content"]) + else: + self.log_message(f"content missing for {dr['sha512']}") + else: + self.error404() + + + def error404(self): + self.send_response(http.HTTPStatus.NOT_FOUND) + self.send_header("Content-Type", "text/html; charset=utf-8") + self.end_headers() + self.wfile.write(b"

404

Nothing to see here

") + + + +ap = argparse.ArgumentParser() +ap.add_argument("--baseurl", required=True) +args = ap.parse_args() + +db = psycopg2.connect("") + +srv = hs.HTTPServer(("127.0.0.1", 8888), WSqlShRH) +srv.socket = ssl.wrap_socket( + srv.socket, + server_side=True, + certfile="localhost.crt", + keyfile="localhost.key", + ssl_version=ssl.PROTOCOL_TLSv1_2 + ) +srv.serve_forever() +