the server is somewhat usable now

This commit is contained in:
randomuser 2021-07-19 15:11:31 -05:00
parent fbeb1d2584
commit f7b79fdc1b
1 changed files with 40 additions and 4 deletions

42
esgd.py
View File

@ -1,9 +1,12 @@
import socketserver import socketserver
import subprocess
import os import os
class GopherError(BaseException): pass class GopherError(BaseException): pass
class RequestError(GopherError): pass class RequestError(GopherError): pass
print(os.getcwd())
def recieveRequest(context): def recieveRequest(context):
data = b'' data = b''
while data[-2:] != b'\r\n': while data[-2:] != b'\r\n':
@ -12,6 +15,7 @@ def recieveRequest(context):
return decoded return decoded
def requestParser(request): def requestParser(request):
if '..' in request: raise RequestError
try: try:
if request[0] == '/': request = request[1:] if request[0] == '/': request = request[1:]
except IndexError: pass except IndexError: pass
@ -39,6 +43,9 @@ def fileCGI(file):
def notFound(context): def notFound(context):
context.request.sendall(b"3error: file not found!\r\n.\r\n") context.request.sendall(b"3error: file not found!\r\n.\r\n")
def invalid(context):
context.request.sendall(b"3error: selector contains '..'!\r\n.\r\n")
def sendFile(file, context): def sendFile(file, context):
with open(file, "r") as fd: with open(file, "r") as fd:
[context.request.sendall( [context.request.sendall(
@ -46,21 +53,50 @@ def sendFile(file, context):
) for i in fd.readlines()] ) for i in fd.readlines()]
context.request.sendall(b".\r\n") context.request.sendall(b".\r\n")
def cgi(file, query, context):
env = {}
env['QUERY_STRING'] = query
env['SCRIPT_NAME'] = "/" + file
env['REMOTE_ADDR'] = context.client_address[0]
proc = subprocess.Popen(
[os.getcwd() + "/" + file],
stdout = subprocess.PIPE,
env = env
)
try: out, err = proc.communicate(timeout=10)
except TimeoutExpired:
proc.kill()
out, err = proc.communicate()
return out.decode('utf-8').replace('\r', '').split('\n')
class gopherHandler(socketserver.BaseRequestHandler): class gopherHandler(socketserver.BaseRequestHandler):
def handle(self): def handle(self):
decoded = recieveRequest(self) decoded = recieveRequest(self)
try:
parsed = requestParser(decoded) parsed = requestParser(decoded)
except RequestError:
invalid(self)
return
print(self.client_address)
try: file = returnRelative(parsed[0]) try: file = returnRelative(parsed[0])
except RequestError: except RequestError:
notFound(self) notFound(self)
return return
if fileCGI(file[0]):
if fileSendable(file[0]): sendFile(file[0], self) 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')
elif fileSendable(file[0]): sendFile(file[0], self)
else: notFound(self) else: notFound(self)
if __name__ == "__main__": if __name__ == "__main__":
HOST, PORT = "localhost", 85 HOST, PORT = "localhost", 72
with socketserver.TCPServer((HOST, PORT), gopherHandler) as server: with socketserver.TCPServer((HOST, PORT), gopherHandler) as server:
server.serve_forever() server.serve_forever()