2022-05-02 07:44:40 +00:00
|
|
|
try:
|
|
|
|
from urllib.parse import parse_qs
|
|
|
|
except:
|
|
|
|
from cgi import parse_qs
|
2018-10-18 17:04:05 +00:00
|
|
|
|
|
|
|
class IllegalMethodException(BaseException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class InvalidArgumentException(BaseException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Request:
|
|
|
|
def __init__(self, env = None):
|
|
|
|
if env:
|
|
|
|
self.fromEnv(env)
|
|
|
|
|
|
|
|
def fromEnv(self, env):
|
|
|
|
if env["REQUEST_METHOD"] == "POST":
|
2022-05-02 07:44:40 +00:00
|
|
|
self.args = parse_qs(env['wsgi.input'].readline().decode(), True)
|
2018-10-18 17:04:05 +00:00
|
|
|
elif env["REQUEST_METHOD"] == "GET":
|
2022-05-02 07:44:40 +00:00
|
|
|
self.args = parse_qs(env['QUERY_STRING'], True)
|
2018-10-18 17:04:05 +00:00
|
|
|
else:
|
|
|
|
raise IllegalMethodException()
|
|
|
|
|
|
|
|
self.path = env["PATH_INFO"].split("/")
|
|
|
|
|
|
|
|
while "" in self.path:
|
|
|
|
self.path.remove("")
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.endpoint = self.path[0]
|
|
|
|
except:
|
|
|
|
self.endpoint = "index.html"
|
|
|
|
|
|
|
|
class Response:
|
|
|
|
def __init__(self, status, ctype, content):
|
|
|
|
self.status = status
|
|
|
|
self.headers = [("Content-Type", ctype)]
|
|
|
|
self.content = content
|
|
|
|
|
|
|
|
def addHeader(self, name, value):
|
|
|
|
self.headers.append((name, value))
|
|
|
|
|