Add support for Date and Last-Modified headers

This commit is contained in:
Fierelier 2022-06-08 11:21:12 +02:00
parent ef657a3436
commit c6bc9d5aea
2 changed files with 20 additions and 2 deletions

View File

@ -4,8 +4,14 @@ def handleBinary(env):
fileExt = env["fileExt"] fileExt = env["fileExt"]
connection = env["self"].connection connection = env["self"].connection
length = 0 length = 0
lastModified = 0
with fileLock: with fileLock:
length = os.path.getsize(filePath) length = os.path.getsize(filePath)
try:
lastModified = os.path.getmtime(filePath)
except:
pass
lastModified = email.utils.formatdate(lastModified).replace("-0000","GMT")
rangeDefined = False rangeDefined = False
rangeStart = 0 rangeStart = 0
@ -26,14 +32,18 @@ def handleBinary(env):
simpleResponse(connection,"200 OK",{ simpleResponse(connection,"200 OK",{
"Content-Length": str(length), "Content-Length": str(length),
"Content-Type": mimetype, "Content-Type": mimetype,
"Accept-Ranges": "bytes" "Accept-Ranges": "bytes",
"Date": env["requestTimeFormatted"],
"Last-Modified": lastModified
}) })
else: else:
simpleResponse(connection,"206 Partial Content",{ simpleResponse(connection,"206 Partial Content",{
"Content-Range": "bytes " +str(rangeStart)+ "-" +str(rangeEnd - 1)+ "/" +str(length), "Content-Range": "bytes " +str(rangeStart)+ "-" +str(rangeEnd - 1)+ "/" +str(length),
"Content-Length": str(rangeEnd - rangeStart), "Content-Length": str(rangeEnd - rangeStart),
"Content-Type": mimetype, "Content-Type": mimetype,
"Accept-Ranges": "bytes" "Accept-Ranges": "bytes",
"Date": env["requestTimeFormatted"],
"Last-Modified": lastModified
}) })
cByte = rangeStart cByte = rangeStart

View File

@ -1,15 +1,23 @@
global handleText global handleText
def handleText(env): def handleText(env):
data = b"" data = b""
lastModified = 0
with fileLock: with fileLock:
with open(env["fPath"],"rb") as textFile: with open(env["fPath"],"rb") as textFile:
data = textFile.read() data = textFile.read()
try:
lastModified = os.path.getmtime(filePath)
except:
pass
lastModified = email.utils.formatdate(lastModified).replace("-0000","GMT")
simpleResponse( simpleResponse(
env["self"].connection,"200 OK", env["self"].connection,"200 OK",
{ {
"Content-Type": mimetypesText[env["fileExt"]]+ "; charset=UTF-8", "Content-Type": mimetypesText[env["fileExt"]]+ "; charset=UTF-8",
"Accept-Ranges": "bytes" "Accept-Ranges": "bytes"
"Date": env["requestTimeFormatted"],
"Last-Modified": lastModified
},data },data
) )