Serve archived pages
This commit is contained in:
commit
cd385be7df
|
@ -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()
|
||||
|
Loading…
Reference in New Issue