simplify the file sending code so the canonical implimentation is sendFileArray

This commit is contained in:
randomuser 2021-07-19 15:20:26 -05:00
parent f7b79fdc1b
commit bd50cd7e1e
1 changed files with 9 additions and 11 deletions

14
esgd.py
View File

@ -48,9 +48,12 @@ def invalid(context):
def sendFile(file, context):
with open(file, "r") as fd:
sendFileArray(fd.readlines(), context)
def sendFileArray(arr, context):
[context.request.sendall(
(i.rstrip() + "\r\n").encode("utf-8")
) for i in fd.readlines()]
) for i in arr]
context.request.sendall(b".\r\n")
def cgi(file, query, context):
@ -81,22 +84,17 @@ class gopherHandler(socketserver.BaseRequestHandler):
invalid(self)
return
print(self.client_address)
try: file = returnRelative(parsed[0])
except RequestError:
notFound(self)
return
if fileCGI(file[0]):
ret = cgi(file[0], parsed[1], self)
for i in ret:
self.request.sendall((i + '\r\n').encode('utf-8'))
self.request.sendall(b'.\r\n')
sendFileArray(cgi(file[0], parsed[1], self), self)
elif fileSendable(file[0]): sendFile(file[0], self)
else: notFound(self)
if __name__ == "__main__":
HOST, PORT = "localhost", 72
HOST, PORT = "localhost", 75
with socketserver.TCPServer((HOST, PORT), gopherHandler) as server:
server.serve_forever()