From 5a0566688ae8d1a527a5989f2d95628e35914507 Mon Sep 17 00:00:00 2001 From: Sam Whited Date: Thu, 23 Jul 2015 00:42:24 -0500 Subject: [PATCH] Don't duplicate HEAD/GET response code --- server.py | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/server.py b/server.py index 1288a53..f942b11 100755 --- a/server.py +++ b/server.py @@ -117,7 +117,7 @@ class HttpHandler(BaseHTTPRequestHandler): self.send_response(403,'invalid slot') self.end_headers() - def do_GET(self): + def do_GET(self, body=True): global config path = normalize_path(self.path[1:]) slashcount = path.count('/') @@ -135,29 +135,14 @@ class HttpHandler(BaseHTTPRequestHandler): fs = os.fstat(f.fileno()) self.send_header("Content-Length", str(fs.st_size)) self.end_headers() - shutil.copyfileobj(f, self.wfile) + if body: + shutil.copyfileobj(f, self.wfile) except FileNotFoundError: self.send_response(404,'file not found') self.end_headers() def do_HEAD(self): - global config - path = normalize_path(self.path[1:]) - slashcount = path.count('/') - if path[0] in ('/', '\\') or slashcount < 1 or slashcount > 2: - self.send_response(404,'file not found') - self.end_headers() - else: - try: - filename = os.path.join(config['storage_path'], path) - self.send_response(200,'OK') - self.send_header("Content-Type", 'application/octet-stream') - self.send_header("Content-Disposition", 'attachment; filename="{}"'.format(os.path.basename(filename))) - self.send_header("Content-Length",str(os.path.getsize(filename))) - self.end_headers() - except FileNotFoundError: - self.send_response(404,'file not found') - self.end_headers() + self.do_GET(body=False) class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):