fhttpy/modules/http/file-handlers/dir.py

41 lines
1.1 KiB
Python

global handleDir
def handleDir(env):
filePath = env["fPath"]
simpleResponse(
env["self"].connection,"200 OK",{
"Content-Type": "text/html; charset=UTF-8",
"Accept-Ranges": "none",
"Date": env["requestTimeFormatted"]
}
)
dirList = []
fileList = []
with fileLock:
for root,dirs,files in os.walk(filePath):
for file in dirs: dirList.append(p(root,file).replace(filePath + os.path.sep,"",1).replace(os.path.sep,"/") + "/")
for file in files: fileList.append(p(root,file).replace(filePath + os.path.sep,"",1).replace(os.path.sep,"/"))
break
dirList.sort(key=str.lower)
fileList.sort(key=str.lower)
env["self"].connection.sendall(('''\
<html>
<head>
<title>''' +html.escape(env["lPath"])+ '''</title>
</head>
<body>''').encode("utf-8"))
env["self"].connection.sendall(('''<a href="..">..</a><br>\n''').encode("utf-8"))
for l in [dirList,fileList]:
for lfile in l:
env["self"].connection.sendall(('''<a href="''' +html.escape(lfile)+ '''">''' +html.escape(lfile)+ '''</a><br>\n''').encode("utf-8"))
env["self"].connection.sendall(('''\
</body>
</html>''').encode("utf-8"))
fileHandlers[".d"] = handleDir