2017-11-02 12:43:46 +00:00
|
|
|
import cgi
|
|
|
|
|
|
|
|
class IllegalMethodException(BaseException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class InvalidArgumentException(BaseException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Request:
|
2017-11-04 13:21:06 +00:00
|
|
|
def __init__(self, env = None):
|
|
|
|
if env:
|
|
|
|
self.fromEnv(env)
|
|
|
|
|
|
|
|
def fromEnv(self, env):
|
2017-11-02 12:43:46 +00:00
|
|
|
if env["REQUEST_METHOD"] == "POST":
|
|
|
|
self.args = cgi.parse_qs(env['wsgi.input'].readline().decode(), True)
|
|
|
|
elif env["REQUEST_METHOD"] == "GET":
|
|
|
|
self.args = cgi.parse_qs(env['QUERY_STRING'], True)
|
|
|
|
else:
|
|
|
|
raise IllegalMethodException()
|
|
|
|
|
|
|
|
self.conn = False
|
|
|
|
self.val = False
|
|
|
|
self.cfrm = None
|
|
|
|
self.cto = None
|
|
|
|
|
|
|
|
self.json = "json" in self.args
|
|
|
|
|
|
|
|
split = env["PATH_INFO"].split("/")
|
|
|
|
split = [i.strip() for i in split]
|
|
|
|
|
|
|
|
while "" in split:
|
|
|
|
split.remove("")
|
|
|
|
|
|
|
|
if len(split) > 2:
|
|
|
|
output = "<h1>400 Bad Request</h1>\n"
|
|
|
|
output += "Only one (validate) or two (conn) arguments may be passed as path."
|
|
|
|
raise InvalidArgumentException(output)
|
|
|
|
|
|
|
|
if len(split) > 0:
|
|
|
|
if len(split) == 1:
|
|
|
|
self.val = True
|
|
|
|
else:
|
|
|
|
self.conn = True
|
|
|
|
self.cto = split[1].encode("latin-1").decode("utf-8")
|
|
|
|
self.cfrm = split[0].encode("latin-1").decode("utf-8")
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.rtype = "conn" if self.conn else "val" if self.val else self.args["type"][0].lower()
|
|
|
|
except:
|
|
|
|
output = "<h1>400 Bad Request</h1>\n"
|
|
|
|
output += "A request type must be provided."
|
|
|
|
raise InvalidArgumentException(output)
|
|
|
|
|