move file serving logic to serveFile(...)

This commit is contained in:
randomuser 2021-07-20 02:04:52 -05:00
parent 581aa7d1d7
commit b690b8f571
1 changed files with 13 additions and 22 deletions

35
esgd.py
View File

@ -128,6 +128,18 @@ def gopherRenderer(fileArray):
returned.append(i.render())
return returned
def serveFile(file, query, gph, context):
if fileCGI(file):
if not gph:
sendFileArray(cgi(file, query, context), context)
else:
sendFileArray(gopherRenderer(cgi(file, query, context)), context)
elif fileSendable(file):
if not gph: sendFile(file, context)
else:
sendFileArray(gopherRenderer([i.rstrip() for i in open(file).readlines()]), context)
else: notFound(context)
class gopherHandler(socketserver.BaseRequestHandler):
def handle(self):
decoded = recieveRequest(self)
@ -141,28 +153,7 @@ class gopherHandler(socketserver.BaseRequestHandler):
except RequestError:
notFound(self)
return
if fileCGI(file[0]):
if not file[1]:
sendFileArray(
cgi(file[0], parsed[1], self), self
)
else:
sendFileArray(
gopherRenderer(
cgi(
file[0], parsed[1], self
)
), self
)
elif fileSendable(file[0]):
if not file[1]: sendFile(file[0], self)
else:
sendFileArray(
gopherRenderer(
[i.rstrip() for i in open(file[0]).readlines()]
), self
)
else: notFound(self)
serveFile(file[0], parsed[1], file[1], self)
if __name__ == "__main__":
with socketserver.TCPServer((host, port), gopherHandler) as server: