Compare commits

...

3 Commits

Author SHA1 Message Date
Fierelier 1b36386eae Add support for keep-alive (Doesn't work right now, idk why) 2022-06-21 03:30:34 +02:00
Fierelier d5664f33e1 Ditto 2022-06-21 03:30:07 +02:00
Fierelier d20f3b5434 Discard Last-Modified on error, other fixes 2022-06-21 03:29:22 +02:00
5 changed files with 102 additions and 91 deletions

View File

@ -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:

View File

@ -1,7 +1,8 @@
global handleText
def handleText(env):
filePath = env["fPath"]
data = b""
lastModified = 0
lastModified = None
with fileLock:
with open(env["fPath"],"rb") as textFile:
data = textFile.read()
@ -9,16 +10,18 @@ def handleText(env):
lastModified = os.path.getmtime(filePath)
except:
pass
lastModified = email.utils.formatdate(lastModified).replace("-0000","GMT")
headers = {
"Content-Type": mimetypesText[env["fileExt"]]+ "; charset=UTF-8",
"Accept-Ranges": "none",
"Date": env["requestTimeFormatted"]
}
if lastModified != None:
headers["Last-Modified"] = email.utils.formatdate(lastModified).replace("-0000","GMT")
simpleResponse(
env["self"].connection,"200 OK",
{
"Content-Type": mimetypesText[env["fileExt"]]+ "; charset=UTF-8",
"Accept-Ranges": "none",
"Date": env["requestTimeFormatted"],
"Last-Modified": lastModified
},data
env["self"].connection,"200 OK",headers,data
)
for t in mimetypesText:

View File

@ -114,6 +114,9 @@ def simpleResponse(connection,status,headers = None,content = None,autolength =
if content != None and autolength == True:
headers["Content-Length"] = str(len(content))
if allowKeepAlive and not ("Connection" in headers):
headers["Connection"] = "keep-alive"
headers["Keep-Alive"] = "timeout=" +str(timeout)
response = 'HTTP/1.1 ' +status+ '\r\n'
for header in headers:
response += header + ": " +headers[header] + "\r\n"

View File

@ -12,74 +12,79 @@ pathHandlers = {}
global clientLoopIn
def clientLoopIn(self):
env = {}
env["self"] = self
env["requestTime"] = time.time()
env["header"] = getHeaderFromConnection(self.connection)
env["protocolHeaderList"],env["headerList"] = parseHeader(env["header"])
env["cmd"] = env["protocolHeaderList"][0]
env["path"],env["args"] = parseHeaderPath(env["protocolHeaderList"][1])
env["pathFixed"] = fixUserPath(env["path"])
env["lPath"] = env["pathFixed"].replace("/",os.path.sep)
env["fPath"] = p(indexPath,env["lPath"])
env["fileExt"] = "."
if not env["pathFixed"] == "" and not os.path.isfile(env["fPath"]) and os.path.isdir(env["fPath"]) and env["pathFixed"][-1] != "/": env["pathFixed"] += "/" # This is dirty, since it possibly circumvents .fhtpyaccess (You can see if a folder exists or not by probing)
if "/" + env["pathFixed"] != env["path"]:
newPath = "/" + pathToURL(env["pathFixed"])
rawArgs = env["protocolHeaderList"][1].split("?",1)
while True:
env = {}
env["self"] = self
env["requestTime"] = time.time()
env["header"] = getHeaderFromConnection(self.connection)
env["protocolHeaderList"],env["headerList"] = parseHeader(env["header"])
env["cmd"] = env["protocolHeaderList"][0]
env["path"],env["args"] = parseHeaderPath(env["protocolHeaderList"][1])
env["pathFixed"] = fixUserPath(env["path"])
if len(rawArgs) > 1:
newPath += "?" +rawArgs[-1]
env["lPath"] = env["pathFixed"].replace("/",os.path.sep)
env["fPath"] = p(indexPath,env["lPath"])
env["fileExt"] = "."
refer(self.connection,newPath)
return
if env["pathFixed"] in pathHandlers:
pathHandlers[env["pathFixed"]](env)
return
if not os.path.isfile(env["fPath"]):
if not os.path.isdir(env["fPath"]):
handle404(env)
if not env["pathFixed"] == "" and not os.path.isfile(env["fPath"]) and os.path.isdir(env["fPath"]) and env["pathFixed"][-1] != "/": env["pathFixed"] += "/" # This is dirty, since it possibly circumvents .fhtpyaccess (You can see if a folder exists or not by probing)
if "/" + env["pathFixed"] != env["path"]:
newPath = "/" + pathToURL(env["pathFixed"])
rawArgs = env["protocolHeaderList"][1].split("?",1)
if len(rawArgs) > 1:
newPath += "?" +rawArgs[-1]
refer(self.connection,newPath)
return
found = False
for file in indexFiles:
if os.path.isfile(p(env["fPath"],file)):
found = file
break
if env["pathFixed"] in pathHandlers:
pathHandlers[env["pathFixed"]](env)
return
if found == False:
env["fileExt"] = ".d"
env["lPath"] = p(env["lPath"],".")
env["fPath"] = p(indexPath,env["lPath"])
if not os.path.isfile(env["fPath"]):
if not os.path.isdir(env["fPath"]):
handle404(env)
return
found = False
for file in indexFiles:
if os.path.isfile(p(env["fPath"],file)):
found = file
break
if found == False:
env["fileExt"] = ".d"
env["lPath"] = p(env["lPath"],".")
env["fPath"] = p(indexPath,env["lPath"])
else:
env["lPath"] = p(env["lPath"],found)
env["fPath"] = p(indexPath,env["lPath"])
lPathSplit = env["lPath"].rsplit(os.path.sep,1)[-1].rsplit(".",1)
if len(lPathSplit) > 1:
env["fileExt"] = lPathSplit[-1].lower()
else:
env["lPath"] = p(env["lPath"],found)
env["fPath"] = p(indexPath,env["lPath"])
lPathSplit = env["lPath"].rsplit(os.path.sep,1)[-1].rsplit(".",1)
if len(lPathSplit) > 1:
env["fileExt"] = lPathSplit[-1].lower()
else:
lPathSplit = env["lPath"].rsplit(os.path.sep,1)[-1].rsplit(".",1)
if len(lPathSplit) > 1:
env["fileExt"] = lPathSplit[-1].lower()
env["fPathDir"] = pUp(env["fPath"])
env["requestTimeFormatted"] = email.utils.formatdate(int(env["requestTime"])).replace("-0000","GMT")
env["handler"] = False
if env["fileExt"] in fileHandlers:
env["handler"] = fileHandlers[env["fileExt"]]
elif ".*" in fileHandlers:
env["handler"] = fileHandlers[".*"]
if triggerEvent("handleHTTP",env) == False: return
if env["handler"]:
env["handler"](env)
else:
handle404(env)
return
env["fPathDir"] = pUp(env["fPath"])
env["requestTimeFormatted"] = email.utils.formatdate(int(env["requestTime"])).replace("-0000","GMT")
env["handler"] = False
if env["fileExt"] in fileHandlers:
env["handler"] = fileHandlers[env["fileExt"]]
elif ".*" in fileHandlers:
env["handler"] = fileHandlers[".*"]
if triggerEvent("handleHTTP",env) == False: return
if env["handler"]:
env["handler"](env)
else:
handle404(env)
if allowKeepAlive:
if "connection" in env["headerList"] and env["headerList"]["connection"] == "keep-alive":
continue
break

View File

@ -3,4 +3,6 @@ maxHeaderLength = 4096
global indexPath
indexPath = p(sp,"index")
global readBufferSize
readBufferSize = 32768
readBufferSize = 32768
global allowKeepAlive
allowKeepAlive = False