-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.py
119 lines (102 loc) · 3.06 KB
/
request.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import simplejson
import base64
import logging
import re
import httpagentparser
from django.core.mail import mail_managers
logger = logging.getLogger(__name__)
def get_ip(request):
"""
Get the appropriate IP address of a request object.
This was written because a load balancer rewrites the
request header with the IP address of the original request
as a different property
"""
if 'HTTP_X_FORWARDED_FOR' in request.META:
ip = request.META['HTTP_X_FORWARDED_FOR']
else:
ip = request.META['REMOTE_ADDR']
return (ip + ',').split(',')[0]
def get_GET(request, BASE64='base64'):
"""
Given a request, this decodes the base64 attributes (if they exist)
"""
if BASE64 in request.GET:
try:
encoded = request.GET.get(BASE64, '')
# convert to ascii
encoded.encode('ascii', 'ignore')
# decode from base64
decoded = base64.decodestring(encoded)
# replace invalid json
decoded.replace('undefined', 'null')
# decode json
get = simplejson.loads(decoded)
except Exception as inst:
print inst
logger.error('Error in base64 padding\n%s\n%s\n%s' % (inst, request.GET.get(BASE64), request))
raise ValueError
for key, value in request.GET.items():
if key != BASE64:
get[key] = value
return get
else:
raw = request.GET.copy()
d = {}
for k,v in raw.items():
try:
d[k] = simplejson.loads(v)
except simplejson.JSONDecodeError:
d[k] = str(v)
return d
def get_os_browser(request):
ua = request.META.get('HTTP_USER_AGENT', '')
os = get_os(ua)
browser = get_browser(ua)
ua_info = httpagentparser.detect(ua)
return {
'os': {
'type': get_os(ua),
'vsn': major_minor(ua_info.get('os', {}).get('version', ''))
},
'browser': {
'type': get_browser(ua),
'vsn': major_minor(ua_info.get('browser', {}).get('version', ''))
},
'is_mobile': is_mobile(ua),
}
MAJOR_MINOR_RE = re.compile(r'(?:^| )(\d+)\.?(\d+)?')
def major_minor(vsn):
m = MAJOR_MINOR_RE.match(vsn)
if m:
return m.groups()
else:
vsn
def get_os(ua):
if ua.find('iPhone') != -1:
return 'iphone'
if ua.find('iPad') != -1:
return 'ipad'
if ua.find('Android') != -1:
return 'android'
elif ua.find('Macintosh') != -1:
return 'mac'
elif ua.find('Windows') != -1:
return 'windows'
elif ua.find('Linux') != -1:
return 'linux'
else:
return None
def get_browser(ua):
if ua.find('Chrome') != -1:
return 'Chrome'
if ua.find('Firefox') != -1:
return 'Firefox'
if ua.find('MSIE') != -1:
return 'Internet Explorer'
if ua.find('Safari') != -1:
return 'Safari'
else:
return None
def is_mobile(ua):
return (get_os(ua) or '').lower() in ['iphone', 'android',]