From a883140db06211c99cd7b361a8232e9e12bce7ee Mon Sep 17 00:00:00 2001 From: Fierelier Date: Wed, 9 Nov 2022 01:00:36 +0100 Subject: [PATCH] Add handler for directories --- modules/http/file-handlers/dir.py | 41 +++++++++++++++++++++++++++++++ modules/http/main.mods | 1 + 2 files changed, 42 insertions(+) create mode 100644 modules/http/file-handlers/dir.py diff --git a/modules/http/file-handlers/dir.py b/modules/http/file-handlers/dir.py new file mode 100644 index 0000000..7fcf089 --- /dev/null +++ b/modules/http/file-handlers/dir.py @@ -0,0 +1,41 @@ +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.escape(env["lPath"])+ ''' + +''').encode("utf-8")) + + env["self"].connection.sendall(('''..
\n''').encode("utf-8")) + for l in [dirList,fileList]: + for lfile in l: + env["self"].connection.sendall(('''''' +html.escape(lfile)+ '''
\n''').encode("utf-8")) + + env["self"].connection.sendall(('''\ + +''').encode("utf-8")) + +fileHandlers[".d"] = handleDir \ No newline at end of file diff --git a/modules/http/main.mods b/modules/http/main.mods index 40832da..32a6e9b 100644 --- a/modules/http/main.mods +++ b/modules/http/main.mods @@ -6,6 +6,7 @@ # File handlers: ./file-handlers/mimetypes.py # List of file endings and the mimetypes they belong to ./file-handlers/binary.py # Images, video, audio, executables, etc... +./file-handlers/dir.py # Directories ./file-handlers/text.py # HTML, XML, TXT, etc... ./file-handlers/pyp.py # pyp, fhttpy's script format ./file-handlers/htaccess.py # .fhtpyaccess - can be used to override handlers on an entire folder \ No newline at end of file