-
Notifications
You must be signed in to change notification settings - Fork 4
/
middlewares.py
87 lines (74 loc) · 2.61 KB
/
middlewares.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from uuid import uuid1
class Session:
"""Session middleware class
"""
def __init__(self):
self.SESSIONS = {}
self.PRE = True
self.POST = False
def __call__(self, *args):
return self.session_middleware(*args)
def session_middleware(self, request, response):
"""Add session ids to self.SESSION
"""
browser_cookies = request["header"]["Cookie"]
if (browser_cookies and "sid" in browser_cookies and
browser_cookies["sid"] in self.SESSIONS):
return request, response
cookie = str(uuid1())
response["Set-Cookie"] = "sid=" + cookie
self.SESSIONS[cookie] = {}
return request, response
def add_session(self, request, content):
"""ADD SESSION
Add session id to self.SESSIONS
"""
browser_cookies = request["header"]["Cookie"]
if "sid" in browser_cookies:
sid = browser_cookies["sid"]
if sid in self.SESSIONS:
self.SESSIONS[sid] = content
def get_session(self, request):
"""GET SESSION
Get session id from self.SESSIONS
"""
browser_cookies = request["header"]["Cookie"]
if browser_cookies and "sid" in browser_cookies:
sid = browser_cookies["sid"]
if sid in self.SESSIONS:
return self.SESSIONS[sid]
def del_session(self, request):
"""DEL SESSIONS
Delete session from self.SESSIONS
"""
browser_cookies = request["header"]["Cookie"]
if "sid" in browser_cookies:
sid = browser_cookies["sid"]
if sid in self.SESSIONS:
del self.SESSIONS[sid]
class Logger:
"""Logger middleware
Logs all request, response to a file
"""
def __init__(self, DEBUG=False, FILENAME="magicserver.log"):
self.PRE = False
self.POST = True
self.DEBUG = DEBUG
self.FILENAME = FILENAME
def __call__(self, *args):
return self.logger(*args)
def logger(self, request, response):
ip = request["header"]["Host"].split(":")[0]
date = response["Date"]
method = request["method"]
path = request["path"]
status = response["status"]
log = "{} - - [{}] \"{} {}\" {}\n".format(ip, date, method,
path, status)
self.write_print_logs(log)
return request, response
def write_print_logs(self, log):
if self.DEBUG:
print(log, end="")
with open(self.FILENAME, mode="a") as log_data:
log_data.write(log)