Serve archived pages

This commit is contained in:
Peter J. Holzer 2020-09-15 09:01:40 +02:00 committed by Peter J. Holzer
commit cd385be7df
1 changed files with 58 additions and 0 deletions

58
awd Executable file
View File

@ -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"<h1>404</h1><p>Nothing to see here</p>")
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()