2021-07-18 23:24:08 -05:00
|
|
|
import socketserver
|
2021-07-19 15:11:31 -05:00
|
|
|
import subprocess
|
2021-07-18 23:24:08 -05:00
|
|
|
import os
|
|
|
|
|
|
|
|
class GopherError(BaseException): pass
|
|
|
|
class RequestError(GopherError): pass
|
|
|
|
|
2021-07-19 15:11:31 -05:00
|
|
|
print(os.getcwd())
|
|
|
|
|
2021-07-18 23:24:08 -05:00
|
|
|
def recieveRequest(context):
|
|
|
|
data = b''
|
|
|
|
while data[-2:] != b'\r\n':
|
|
|
|
data += context.request.recv(1)
|
|
|
|
decoded = data[:-2].decode('utf-8')
|
|
|
|
return decoded
|
|
|
|
|
|
|
|
def requestParser(request):
|
2021-07-19 15:11:31 -05:00
|
|
|
if '..' in request: raise RequestError
|
2021-07-18 23:24:08 -05:00
|
|
|
try:
|
|
|
|
if request[0] == '/': request = request[1:]
|
|
|
|
except IndexError: pass
|
|
|
|
request = request.replace('?', '\t').split('\t')
|
|
|
|
try: return (request[0], request[1])
|
|
|
|
except IndexError: return (request[0], "")
|
|
|
|
|
|
|
|
def returnRelative(file):
|
|
|
|
gph = False
|
|
|
|
if file == "":
|
|
|
|
ret = "gophermap"
|
|
|
|
gph = True
|
|
|
|
elif os.path.isdir(file):
|
|
|
|
ret = file + "/gophermap"
|
|
|
|
gph = True
|
|
|
|
elif os.path.isfile(file): ret = file
|
|
|
|
else: raise RequestError('unreachable state')
|
|
|
|
return (ret, gph)
|
|
|
|
|
|
|
|
def fileSendable(file): return os.access(file, os.F_OK|os.R_OK)
|
|
|
|
|
|
|
|
def fileCGI(file):
|
|
|
|
return os.access(file, os.F_OK|os.R_OK|os.X_OK)
|
|
|
|
|
|
|
|
def notFound(context):
|
|
|
|
context.request.sendall(b"3error: file not found!\r\n.\r\n")
|
|
|
|
|
2021-07-19 15:11:31 -05:00
|
|
|
def invalid(context):
|
|
|
|
context.request.sendall(b"3error: selector contains '..'!\r\n.\r\n")
|
|
|
|
|
2021-07-18 23:24:08 -05:00
|
|
|
def sendFile(file, context):
|
|
|
|
with open(file, "r") as fd:
|
2021-07-19 15:20:26 -05:00
|
|
|
sendFileArray(fd.readlines(), context)
|
|
|
|
|
|
|
|
def sendFileArray(arr, context):
|
|
|
|
[context.request.sendall(
|
|
|
|
(i.rstrip() + "\r\n").encode("utf-8")
|
|
|
|
) for i in arr]
|
|
|
|
context.request.sendall(b".\r\n")
|
2021-07-18 23:24:08 -05:00
|
|
|
|
2021-07-19 15:11:31 -05:00
|
|
|
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')
|
|
|
|
|
2021-07-18 23:24:08 -05:00
|
|
|
class gopherHandler(socketserver.BaseRequestHandler):
|
|
|
|
def handle(self):
|
|
|
|
decoded = recieveRequest(self)
|
2021-07-19 15:11:31 -05:00
|
|
|
try:
|
|
|
|
parsed = requestParser(decoded)
|
|
|
|
except RequestError:
|
|
|
|
invalid(self)
|
|
|
|
return
|
|
|
|
|
2021-07-18 23:24:08 -05:00
|
|
|
try: file = returnRelative(parsed[0])
|
|
|
|
except RequestError:
|
|
|
|
notFound(self)
|
|
|
|
return
|
2021-07-19 15:11:31 -05:00
|
|
|
if fileCGI(file[0]):
|
2021-07-19 15:20:26 -05:00
|
|
|
sendFileArray(cgi(file[0], parsed[1], self), self)
|
2021-07-19 15:11:31 -05:00
|
|
|
elif fileSendable(file[0]): sendFile(file[0], self)
|
2021-07-18 23:24:08 -05:00
|
|
|
else: notFound(self)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-07-19 15:20:26 -05:00
|
|
|
HOST, PORT = "localhost", 75
|
2021-07-18 23:24:08 -05:00
|
|
|
|
|
|
|
with socketserver.TCPServer((HOST, PORT), gopherHandler) as server:
|
|
|
|
server.serve_forever()
|