From 9db41d5681447e71b60ce4bb978036781142dced Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Wed, 6 Jun 2012 15:54:47 +0200 Subject: [PATCH] Moved Content-* header generation to extra function --- servefile | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/servefile b/servefile index 1690ca8..dde07a5 100755 --- a/servefile +++ b/servefile @@ -53,6 +53,21 @@ class FileBaseHandler(BaseHTTPServer.BaseHTTPRequestHandler): return True return False + def sendContentHeaders(self, fileName, fileLength, lastModified=None): + """ Send default Content headers for given fileName and fileLength. + + If no lastModified is given the current date is taken. If + fileLength is lesser than 0 no Content-Length will be sent.""" + if not lastModified: + lastModified = getDateStrNow() + + if fileLength >= 0: + self.send_header('Content-Length', str(fileLength)) + self.send_header('Last-Modified', lastModified) + self.send_header('Content-Type', 'application/octet-stream') + self.send_header('Content-Disposition', 'attachment; filename="%s"' % fileName) + self.send_header('Content-Transfer-Encoding', 'binary') + class FileHandler(FileBaseHandler): filePath = "/dev/null" fileLength = 0 @@ -62,10 +77,7 @@ class FileHandler(FileBaseHandler): if self.checkAndDoRedirect(): return self.send_response(200) - self.send_header('Content-Length', self.fileLength) - self.send_header('Last-Modified', self.startTime) - self.send_header('Content-Type', 'application/octet-stream') - self.send_header('Content-Disposition', 'attachment; filename="%s"' % self.fileName) + self.sendContentHeaders(self.fileName, self.fileLength, self.startTime) self.end_headers() def do_GET(self): @@ -93,16 +105,14 @@ class FileHandler(FileBaseHandler): # now we can wind the file *brrrrrr* myfile.seek(fromto[0]) + fileLength = self.fileLength if fromto != None: self.send_response(216) self.send_header('Content-Range', 'bytes %s-%s/%s' % (fromto[0], fromto[1], self.fileLength)) - self.send_header('Content-Length', fromto[1]-fromto[0]+1) + fileLength = fromto[1] - fromto[0] + 1 else: self.send_response(200) - self.send_header('Content-Length', self.fileLength) - self.send_header('Content-Disposition', 'attachment; filename="%s"' % self.fileName) - self.send_header('Content-Type', 'application/octet-stream') - self.send_header('Content-Transfer-Encoding', 'binary') + self.sendContentHeaders(self.fileName, fileLength, self.startTime) self.end_headers() block = self.getChunk(myfile, fromto) while block: @@ -133,9 +143,7 @@ class TarFileHandler(FileBaseHandler): if self.checkAndDoRedirect(): return self.send_response(200) - self.send_header('Last-Modified', getDateStrNow()) - self.send_header('Content-Type', 'application/octet-stream') - self.send_header('Content-Disposition', 'attachment; filename="%s"' % self.fileName) + self.sendContentHeaders(self.fileName, -1) self.end_headers() def do_GET(self): @@ -154,8 +162,7 @@ class TarFileHandler(FileBaseHandler): return self.send_response(200) - self.send_header('Last-Modified', getDateStrNow()) - self.send_header('Content-Type', 'application/octet-stream') + self.sendContentHeaders(self.fileName, -1) self.end_headers() block = True