Added HEAD support

This commit is contained in:
Sebastian Lohff 2012-04-09 15:32:57 +02:00
parent f6cfefb3ea
commit e5108d0f63
1 changed files with 27 additions and 4 deletions

View File

@ -10,6 +10,7 @@ __version__ = '0.3.2'
import argparse import argparse
import BaseHTTPServer import BaseHTTPServer
import commands import commands
import datetime
import urllib import urllib
import os import os
import SocketServer import SocketServer
@ -17,20 +18,42 @@ import socket
from stat import ST_SIZE from stat import ST_SIZE
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
import sys import sys
import time
def getDateStrNow():
""" Get the current time formatted for HTTP header """
now = datetime.datetime.fromtimestamp(time.mktime(time.gmtime()))
return now.strftime("%a, %d %b %Y %H:%M:%S GMT")
class FileHandler(BaseHTTPServer.BaseHTTPRequestHandler): class FileHandler(BaseHTTPServer.BaseHTTPRequestHandler):
fileName = "Undefined" fileName = "Undefined"
filePath = "/dev/null" filePath = "/dev/null"
fileLength = 0 fileLength = 0
startTime = getDateStrNow()
blockSize = 1024 * 1024 blockSize = 1024 * 1024
def do_GET(self):
def checkAndDoRedirect(self):
""" If request didn't request self.fileName redirect to self.fileName.
Returns True if a redirect was issued. """
if urllib.unquote(self.path) != "/" + self.fileName: if urllib.unquote(self.path) != "/" + self.fileName:
self.send_response(302) self.send_response(302)
self.send_header('Location', '/' + self.fileName) self.send_header('Location', '/' + self.fileName)
self.end_headers() self.end_headers()
return return True
return False
def do_HEAD(self):
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.end_headers()
def do_GET(self):
myfile = open(self.filePath, 'rb') myfile = open(self.filePath, 'rb')
# find out if this is a continuing download # find out if this is a continuing download
@ -61,7 +84,7 @@ class FileHandler(BaseHTTPServer.BaseHTTPRequestHandler):
self.send_response(200) self.send_response(200)
self.send_header('Content-Length', self.fileLength) self.send_header('Content-Length', self.fileLength)
self.send_header('Content-Disposition', 'attachment; filename="%s"' % self.fileName) self.send_header('Content-Disposition', 'attachment; filename="%s"' % self.fileName)
self.send_header('Content-type', 'application/octet-stream') self.send_header('Content-Type', 'application/octet-stream')
self.send_header('Content-Transfer-Encoding', ' binary') self.send_header('Content-Transfer-Encoding', ' binary')
self.end_headers() self.end_headers()
block = self.getChunk(myfile, fromto) block = self.getChunk(myfile, fromto)