From d20f3b543433e97ea0f9346f6e3e64bd3715c402 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Tue, 21 Jun 2022 03:29:22 +0200 Subject: [PATCH] Discard Last-Modified on error, other fixes --- modules/http/file-handlers/binary.py | 36 +++++++++++++--------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/modules/http/file-handlers/binary.py b/modules/http/file-handlers/binary.py index 806d6ef..889ba19 100644 --- a/modules/http/file-handlers/binary.py +++ b/modules/http/file-handlers/binary.py @@ -4,14 +4,13 @@ def handleBinary(env): fileExt = env["fileExt"] connection = env["self"].connection length = 0 - lastModified = 0 + lastModified = None with fileLock: length = os.path.getsize(filePath) try: lastModified = os.path.getmtime(filePath) except: pass - lastModified = email.utils.formatdate(lastModified).replace("-0000","GMT") rangeDefined = False rangeStart = 0 @@ -28,23 +27,22 @@ def handleBinary(env): if fileExt in mimetypesBinary: mimetype = mimetypesBinary[fileExt] - if not rangeDefined: - simpleResponse(connection,"200 OK",{ - "Content-Length": str(length), - "Content-Type": mimetype, - "Accept-Ranges": "bytes", - "Date": env["requestTimeFormatted"], - "Last-Modified": lastModified - }) - else: - simpleResponse(connection,"206 Partial Content",{ - "Content-Range": "bytes " +str(rangeStart)+ "-" +str(rangeEnd - 1)+ "/" +str(length), - "Content-Length": str(rangeEnd - rangeStart), - "Content-Type": mimetype, - "Accept-Ranges": "bytes", - "Date": env["requestTimeFormatted"], - "Last-Modified": lastModified - }) + status = "200 OK" + headers = { + "Content-Length": str(length), + "Content-Type": mimetype, + "Accept-Ranges": "bytes", + "Date": env["requestTimeFormatted"] + } + + if lastModified != None: + headers["Last-Modified"] = email.utils.formatdate(lastModified).replace("-0000","GMT") + + if rangeDefined: + status = "206 Partial Content" + headers["Content-Range"] = "bytes " +str(rangeStart)+ "-" +str(rangeEnd - 1)+ "/" +str(length) + + simpleResponse(connection,status,headers) cByte = rangeStart while cByte < rangeEnd: