move all the functions in the global namespace to the gopherHandler() class
This commit is contained in:
parent
a2ac3369e4
commit
3c159a5729
62
esgd.py
62
esgd.py
|
@ -3,7 +3,7 @@ import subprocess
|
||||||
import os
|
import os
|
||||||
|
|
||||||
host = "localhost"
|
host = "localhost"
|
||||||
port = 71
|
port = 77
|
||||||
|
|
||||||
class GopherError(BaseException): pass
|
class GopherError(BaseException): pass
|
||||||
class RequestError(GopherError): pass
|
class RequestError(GopherError): pass
|
||||||
|
@ -51,14 +51,15 @@ class GopherLine:
|
||||||
self.port
|
self.port
|
||||||
)
|
)
|
||||||
|
|
||||||
def recieveRequest(context):
|
class gopherHandler(socketserver.BaseRequestHandler):
|
||||||
|
def recieveRequest(self):
|
||||||
data = b""
|
data = b""
|
||||||
while data[-2:] != b"\r\n":
|
while data[-2:] != b"\r\n":
|
||||||
data += context.request.recv(1)
|
data += self.request.recv(1)
|
||||||
decoded = data[:-2].decode("utf-8")
|
decoded = data[:-2].decode("utf-8")
|
||||||
return decoded
|
return decoded
|
||||||
|
|
||||||
def requestParser(request):
|
def requestParser(self, request):
|
||||||
if ".." in request: raise RequestError
|
if ".." in request: raise RequestError
|
||||||
try:
|
try:
|
||||||
if request[0] == "/": request = request[1:]
|
if request[0] == "/": request = request[1:]
|
||||||
|
@ -67,7 +68,7 @@ def requestParser(request):
|
||||||
try: return (request[0], request[1])
|
try: return (request[0], request[1])
|
||||||
except IndexError: return (request[0], "")
|
except IndexError: return (request[0], "")
|
||||||
|
|
||||||
def returnRelative(file):
|
def returnRelative(self, file):
|
||||||
gph = False
|
gph = False
|
||||||
if file == "":
|
if file == "":
|
||||||
ret = "gophermap"
|
ret = "gophermap"
|
||||||
|
@ -79,34 +80,34 @@ def returnRelative(file):
|
||||||
else: raise RequestError("unreachable state")
|
else: raise RequestError("unreachable state")
|
||||||
return (ret, gph)
|
return (ret, gph)
|
||||||
|
|
||||||
def fileSendable(file): return os.access(file, os.F_OK|os.R_OK)
|
def fileSendable(self, file): return os.access(file, os.F_OK|os.R_OK)
|
||||||
|
|
||||||
def fileCGI(file):
|
def fileCGI(self, file):
|
||||||
return os.access(file, os.F_OK|os.R_OK|os.X_OK)
|
return os.access(file, os.F_OK|os.R_OK|os.X_OK)
|
||||||
|
|
||||||
def notFound(context):
|
def notFound(self):
|
||||||
context.request.sendall(b"3error: file not found!\r\n.\r\n")
|
self.request.sendall(b"3error: file not found!\r\n.\r\n")
|
||||||
|
|
||||||
def invalid(context):
|
def invalid(self):
|
||||||
context.request.sendall(b"3error: selector contains '..'!\r\n.\r\n")
|
self.request.sendall(b"3error: selector contains '..'!\r\n.\r\n")
|
||||||
|
|
||||||
def fileToFileArray(file):
|
def fileToFileArray(self, file):
|
||||||
fd = open(file, "r")
|
fd = open(file, "r")
|
||||||
ret = [i.rstrip() for i in fd.readlines()]
|
ret = [i.rstrip() for i in fd.readlines()]
|
||||||
fd.close()
|
fd.close()
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def sendFileArray(arr, context):
|
def sendFileArray(self, arr):
|
||||||
[context.request.sendall(
|
[self.request.sendall(
|
||||||
(i.rstrip() + "\r\n").encode("utf-8")
|
(i.rstrip() + "\r\n").encode("utf-8")
|
||||||
) for i in arr]
|
) for i in arr]
|
||||||
context.request.sendall(b".\r\n")
|
self.request.sendall(b".\r\n")
|
||||||
|
|
||||||
def cgi(file, query, context):
|
def cgi(self, file, query):
|
||||||
env = {}
|
env = {}
|
||||||
env["QUERY_STRING"] = query
|
env["QUERY_STRING"] = query
|
||||||
env["SCRIPT_NAME"] = "/" + file
|
env["SCRIPT_NAME"] = "/" + file
|
||||||
env["REMOTE_ADDR"] = context.client_address[0]
|
env["REMOTE_ADDR"] = self.client_address[0]
|
||||||
|
|
||||||
proc = subprocess.Popen(
|
proc = subprocess.Popen(
|
||||||
[os.getcwd() + "/" + file],
|
[os.getcwd() + "/" + file],
|
||||||
|
@ -121,7 +122,7 @@ 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):
|
def gopherRenderer(self, fileArray):
|
||||||
gopherlines = []
|
gopherlines = []
|
||||||
for i in fileArray:
|
for i in fileArray:
|
||||||
gopherlines.append(GopherLine(i))
|
gopherlines.append(GopherLine(i))
|
||||||
|
@ -130,29 +131,28 @@ def gopherRenderer(fileArray):
|
||||||
returned.append(i.render())
|
returned.append(i.render())
|
||||||
return returned
|
return returned
|
||||||
|
|
||||||
def serveFile(file, query, gph, context):
|
def serveFile(self, file, query, gph):
|
||||||
if fileCGI(file): fa = cgi(file, query, context)
|
if self.fileCGI(file): fa = self.cgi(file, query)
|
||||||
elif fileSendable(file): fa = fileToFileArray(file)
|
elif self.fileSendable(file): fa = self.fileToFileArray(file)
|
||||||
else:
|
else:
|
||||||
notFound(context)
|
self.notFound()
|
||||||
return
|
return
|
||||||
if gph: fa = gopherRenderer(fa)
|
if gph: fa = self.gopherRenderer(fa)
|
||||||
sendFileArray(fa, context)
|
self.sendFileArray(fa)
|
||||||
|
|
||||||
class gopherHandler(socketserver.BaseRequestHandler):
|
|
||||||
def handle(self):
|
def handle(self):
|
||||||
decoded = recieveRequest(self)
|
decoded = self.recieveRequest()
|
||||||
try:
|
try:
|
||||||
parsed = requestParser(decoded)
|
parsed = self.requestParser(decoded)
|
||||||
except RequestError:
|
except RequestError:
|
||||||
invalid(self)
|
self.invalid()
|
||||||
return
|
return
|
||||||
|
|
||||||
try: file = returnRelative(parsed[0])
|
try: file = self.returnRelative(parsed[0])
|
||||||
except RequestError:
|
except RequestError:
|
||||||
notFound(self)
|
self.notFound()
|
||||||
return
|
return
|
||||||
serveFile(file[0], parsed[1], file[1], self)
|
self.serveFile(file[0], parsed[1], file[1])
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
with socketserver.TCPServer((host, port), gopherHandler) as server:
|
with socketserver.TCPServer((host, port), gopherHandler) as server:
|
||||||
|
|
Loading…
Reference in New Issue