Moved Content-* header generation to extra function

This commit is contained in:
Sebastian Lohff 2012-06-06 15:54:47 +02:00
parent 4567570962
commit 9db41d5681
1 changed files with 21 additions and 14 deletions

View File

@ -53,6 +53,21 @@ class FileBaseHandler(BaseHTTPServer.BaseHTTPRequestHandler):
return True return True
return False 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): class FileHandler(FileBaseHandler):
filePath = "/dev/null" filePath = "/dev/null"
fileLength = 0 fileLength = 0
@ -62,10 +77,7 @@ class FileHandler(FileBaseHandler):
if self.checkAndDoRedirect(): if self.checkAndDoRedirect():
return return
self.send_response(200) self.send_response(200)
self.send_header('Content-Length', self.fileLength) self.sendContentHeaders(self.fileName, self.fileLength, self.startTime)
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.end_headers() self.end_headers()
def do_GET(self): def do_GET(self):
@ -93,16 +105,14 @@ class FileHandler(FileBaseHandler):
# now we can wind the file *brrrrrr* # now we can wind the file *brrrrrr*
myfile.seek(fromto[0]) myfile.seek(fromto[0])
fileLength = self.fileLength
if fromto != None: if fromto != None:
self.send_response(216) self.send_response(216)
self.send_header('Content-Range', 'bytes %s-%s/%s' % (fromto[0], fromto[1], self.fileLength)) 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: else:
self.send_response(200) self.send_response(200)
self.send_header('Content-Length', self.fileLength) self.sendContentHeaders(self.fileName, fileLength, self.startTime)
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.end_headers() self.end_headers()
block = self.getChunk(myfile, fromto) block = self.getChunk(myfile, fromto)
while block: while block:
@ -133,9 +143,7 @@ class TarFileHandler(FileBaseHandler):
if self.checkAndDoRedirect(): if self.checkAndDoRedirect():
return return
self.send_response(200) self.send_response(200)
self.send_header('Last-Modified', getDateStrNow()) self.sendContentHeaders(self.fileName, -1)
self.send_header('Content-Type', 'application/octet-stream')
self.send_header('Content-Disposition', 'attachment; filename="%s"' % self.fileName)
self.end_headers() self.end_headers()
def do_GET(self): def do_GET(self):
@ -154,8 +162,7 @@ class TarFileHandler(FileBaseHandler):
return return
self.send_response(200) self.send_response(200)
self.send_header('Last-Modified', getDateStrNow()) self.sendContentHeaders(self.fileName, -1)
self.send_header('Content-Type', 'application/octet-stream')
self.end_headers() self.end_headers()
block = True block = True