integrate parser into server

This commit is contained in:
randomuser 2021-07-19 17:15:39 -05:00
parent b6de76f44d
commit 6c711ae3da
1 changed files with 34 additions and 4 deletions

38
esgd.py
View File

@ -2,6 +2,9 @@ import socketserver
import subprocess import subprocess
import os import os
host = "localhost"
port = 70
class GopherError(BaseException): pass class GopherError(BaseException): pass
class RequestError(GopherError): pass class RequestError(GopherError): pass
@ -35,11 +38,11 @@ class GopherLine:
try: self.host = split[2] try: self.host = split[2]
except IndexError: except IndexError:
if location == True: self.host = "beepboop.systems" if location == True: self.host = host
try: self.port = split[3] try: self.port = split[3]
except IndexError: except IndexError:
if location == True: self.port = "70" if location == True: self.port = str(port)
def render(self): def render(self):
return "{}{}\t{}\t{}\t{}".format( return "{}{}\t{}\t{}\t{}".format(
self.item, self.item,
@ -117,6 +120,15 @@ def cgi(file, query, context):
return out.decode("utf-8").replace("\r", "").split("\n") return out.decode("utf-8").replace("\r", "").split("\n")
def gopherRenderer(fileArray):
gopherlines = []
for i in fileArray:
gopherlines.append(GopherLine(i))
returned = []
for i in gopherlines:
returned.append(i.render())
return returned
class gopherHandler(socketserver.BaseRequestHandler): class gopherHandler(socketserver.BaseRequestHandler):
def handle(self): def handle(self):
decoded = recieveRequest(self) decoded = recieveRequest(self)
@ -131,8 +143,26 @@ class gopherHandler(socketserver.BaseRequestHandler):
notFound(self) notFound(self)
return return
if fileCGI(file[0]): if fileCGI(file[0]):
sendFileArray(cgi(file[0], parsed[1], self), self) if not file[1]:
elif fileSendable(file[0]): sendFile(file[0], self) 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) else: notFound(self)
if __name__ == "__main__": if __name__ == "__main__":