|
|
3d8d28 |
|
|
|
05525d |
|
|
|
b838e2 |
import cgi
|
|
|
b838e2 |
import urllib.parse
|
|
|
b838e2 |
|
|
|
05525d |
import answer
|
|
|
b838e2 |
from model.model import Model
|
|
|
b838e2 |
|
|
|
05525d |
|
|
|
3d8d28 |
class Request:
|
|
|
b838e2 |
def __init__(self, server, env):
|
|
|
3d8d28 |
self.server = server
|
|
|
05525d |
self.env = env
|
|
|
05525d |
self.method = str(self.env["REQUEST_METHOD"])
|
|
|
e5b0ac |
self.protocol = self.server.config['protocol']
|
|
|
e5b0ac |
self.domain = self.server.config['domain']
|
|
|
b838e2 |
self.prefix = self.server.config['urlprefix']
|
|
|
05525d |
|
|
|
b838e2 |
self.urlvars = dict()
|
|
|
b838e2 |
self.postvars = dict()
|
|
|
b838e2 |
self.action = ''
|
|
|
05525d |
self.path = None
|
|
|
b838e2 |
self.cookie = ''
|
|
|
cbf076 |
self.connection = None
|
|
|
b838e2 |
self.model = None
|
|
|
b838e2 |
self.user = None
|
|
|
b838e2 |
self.answer = None
|
|
|
48408a |
self.session = None
|
|
|
b838e2 |
|
|
|
b838e2 |
# read url vars
|
|
|
b838e2 |
if 'QUERY_STRING' in self.env:
|
|
|
b838e2 |
urlvars = urllib.parse.parse_qs(self.env["QUERY_STRING"])
|
|
|
b838e2 |
for k, v in urlvars.items():
|
|
|
b838e2 |
assert k not in self.urlvars
|
|
|
b838e2 |
self.urlvars[k] = v
|
|
|
b838e2 |
|
|
|
b838e2 |
# read cookies
|
|
|
b838e2 |
self.cookie = self.env.get('HTTP_COOKIE', '')
|
|
|
b838e2 |
assert(type(self.cookie) is str)
|
|
|
b838e2 |
|
|
|
b838e2 |
# read path
|
|
|
e5b0ac |
path = self.env["REQUEST_URI"].split('?')[0]
|
|
|
b838e2 |
if path.startswith(self.prefix):
|
|
|
b838e2 |
path = path[len(self.prefix):]
|
|
|
b838e2 |
path_list = [x for x in path.split('/') if x]
|
|
|
b838e2 |
path_list_filtered = list()
|
|
|
b838e2 |
for i in path_list:
|
|
|
b838e2 |
if i == '..':
|
|
|
b838e2 |
if path_list_filtered:
|
|
|
b838e2 |
path_list_filtered.pop();
|
|
|
b838e2 |
elif i != '.':
|
|
|
b838e2 |
path_list_filtered.append(i)
|
|
|
b838e2 |
self.path = path_list_filtered
|
|
|
b838e2 |
|
|
|
b838e2 |
self.answer = answer.Answer(self)
|
|
|
b838e2 |
self.server.sessions.attach_session(self)
|
|
|
e5b0ac |
|
|
|
572081 |
def read_postvars(self):
|
|
|
e5b0ac |
if self.method == 'POST':
|
|
|
e5b0ac |
postvars = cgi.FieldStorage(
|
|
|
e5b0ac |
fp = self.env["wsgi.input"],
|
|
|
e5b0ac |
environ = self.env )
|
|
|
e5b0ac |
duplicates = set()
|
|
|
e5b0ac |
for k in postvars:
|
|
|
e5b0ac |
kk = str(k)
|
|
|
e5b0ac |
if kk in self.postvars:
|
|
|
e5b0ac |
duplicates.add(kk)
|
|
|
e5b0ac |
del self.postvars[kk]
|
|
|
e5b0ac |
elif not kk in duplicates:
|
|
|
e5b0ac |
self.postvars[kk] = postvars[kk].value
|
|
|
e5b0ac |
|
|
|
e5b0ac |
# get action
|
|
|
e5b0ac |
if 'action' in self.postvars:
|
|
|
e5b0ac |
action = self.postvars['action']
|
|
|
e5b0ac |
del self.postvars['action']
|
|
|
e5b0ac |
if not action is None:
|
|
|
e5b0ac |
self.action = str(action)
|
|
|
e5b0ac |
|
|
|
b838e2 |
def create_model(self, connection):
|
|
|
b838e2 |
self.connection = connection
|
|
|
b838e2 |
self.model = Model(self.connection, self.answer, self.session.user_id if self.session else 0)
|
|
|
b838e2 |
if self.session:
|
|
|
b838e2 |
self.user = self.model.users.get_by_id(self.session.user_id)
|
|
|
b838e2 |
if not self.user:
|
|
|
b838e2 |
self.server.sessions.close_session(self)
|
|
|
3976df |
self.model = Model(self.connection, self.answer, 0)
|
|
|
b838e2 |
|
|
|
e5b0ac |
def get_urlpath(self, path = None, domain = False):
|
|
|
b838e2 |
if path is None:
|
|
|
b838e2 |
path = self.path
|
|
|
b838e2 |
assert(type(path) is list)
|
|
|
e5b0ac |
urlpath = self.prefix + '/' + '/'.join(path)
|
|
|
e5b0ac |
if domain:
|
|
|
e5b0ac |
urlpath = 'https://' + self.domain + urlpath
|
|
|
e5b0ac |
return urlpath
|
|
|
b838e2 |
|
|
|
e5b0ac |
def get_urlpath_escaped(self, path = None, domain = False):
|
|
|
e5b0ac |
return self.answer.e( self.get_urlpath(path, domain) )
|
|
|
b838e2 |
|
|
|
b838e2 |
def translate(self, text):
|
|
|
b838e2 |
return self.answer.translate(text)
|
|
|
b838e2 |
def t(self, text):
|
|
|
b838e2 |
return self.translate(text)
|