Fix pdf retrieval

This commit is contained in:
Paul Mathieu 2022-07-26 17:27:22 +02:00
parent e7ab08d682
commit 4a8aba4bab

8
web.py
View File

@ -67,15 +67,17 @@ def parse_request(request):
class MyServer(BaseHTTPRequestHandler): class MyServer(BaseHTTPRequestHandler):
def do_GET(self): def do_GET(self):
if re.match(r'/data/\w+\.pdf', self.path) is not None: match = re.match(r'/data/(\w+\.pdf)', self.path)
if not os.path.exists(self.path): if match is not None:
pdf_path = os.path.join(OUT_DIR, match.groups()[0])
if not os.path.exists(pdf_path):
self.send_response(404) self.send_response(404)
self.end_headers() self.end_headers()
return return
self.send_response(200) self.send_response(200)
self.send_header("Content-type", "application/pdf") self.send_header("Content-type", "application/pdf")
self.end_headers() self.end_headers()
with open(self.path, 'rb') as f: with open(pdf_path, 'rb') as f:
self.wfile.write(f.read()) self.wfile.write(f.read())
return return