/
interceptor.py
executable file
·62 lines (54 loc) · 1.68 KB
/
interceptor.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
#!/usr/bin/env python
'''
Apache redirect all dynamic requests to this script.
On each dynamic request, this script invokes corresponding function
according to request path.
'''
import re
import cgitb
import os
import logging
import user
import avatar
import util
import request
cgitb.enable()
logging.getLogger().setLevel(logging.ERROR)
def intercept():
'''
This function will be invoked when a dynamic request comes.
It firstly gets request path from evironment variables,
then invokes corresponding functions according to request path.
'''
env = os.environ
# Get request uri from environment variables
uri = env['REQUEST_URI']
# Get request path, leaving out query string
path = uri.split('?', 1)[0]
# Construct HTTPResquest object
req = request.HTTPRequest()
# Invoke corresponding functions according to request path
if path == '/register':
user.register(req)
elif path == '/login':
user.login(req)
elif path == '/logout':
user.logout(req)
elif path == '/info':
user.showinfo(req)
elif path == '/upload':
avatar.upload_avatar(req)
elif re.compile(r'/avatar/(?P<md5>[\w]+)').match(path):
# Get 32-char long md5, leaving out file ext or other chars.
# If the length of md5 part is less than 32, get all the part.
md5 = path[8:40]
avatar.back_avatar(md5)
else:
util.msg_redirect('/static/homepage.html','unsupported url')
if __name__ == '__main__':
try:
intercept()
except:
logging.exception('')
util.msg_redirect('http://' +os.environ['HTTP_HOST'],
'Oops, Someone have eaten your page!')