Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 61 lines (42 sloc) 1.884 kb
101aa12 initial import
Justine Tunney authored
1 r"""
2
3 occupywallst.middleware
4 ~~~~~~~~~~~~~~~~~~~~~~~
5
6 Django middleware definitions.
7
8 """
9
10 import traceback
11
9457a60 don't cache stuff until i figure out how to handle caching properly
Justine Tunney authored
12 from django.utils.cache import add_never_cache_headers
13
101aa12 initial import
Justine Tunney authored
14
9457a60 don't cache stuff until i figure out how to handle caching properly
Justine Tunney authored
15 class NeverCache(object):
16 def process_response(self, request, response):
17 add_never_cache_headers(response)
18 return response
971f7c7 Add XForwardedForMiddleware middleware
Justine Tunney authored
19
20
21 class XForwardedForMiddleware(object):
22 """Replace ``REMOTE_ADDR`` with ``HTTP_X_FORWARDED_FOR``
23
24 When reverse proxying from nginx, we receive a tcp connection from
25 localhost which isn't the client's real ip address. Normally
26 reverse proxies are configured to set the ``X-Forwarded-For``
27 header which gives us the actual client ip.
28 """
29
30 def process_request(self, request):
31 if 'HTTP_X_FORWARDED_FOR' in request.META:
32 request.META['REMOTE_ADDR'] = request.META['HTTP_X_FORWARDED_FOR']
33 request.META['REMOTE_HOST'] = None
8733356 sql logging stuff
Justine Tunney authored
34
35
70c0ab4 @thirtyseven Merge branch 'master' of https://github.com/jart/occupywallst
thirtyseven authored
36 class CsrfCookieWhenLoggedIn(object):
37 """Tell Django to set CSRF cookie on all pages when logged in
38
39 Normally Django only sets the CSRF cookie when you use the CSRF
40 protection template tag. Because we use Ajax for just about
41 everything, we need to ensure this cookie is always set once the
42 user logs in.
8733356 sql logging stuff
Justine Tunney authored
43 """
44
45 def process_response(self, request, response):
70c0ab4 @thirtyseven Merge branch 'master' of https://github.com/jart/occupywallst
thirtyseven authored
46 if response.status_code == 200 and request.method == 'GET':
47 if request.user.is_authenticated():
48 request.META["CSRF_COOKIE_USED"] = True
8733356 sql logging stuff
Justine Tunney authored
49 return response
fa5d0a3 i wish ted had told me the captcha didn't bloody work :'(
Justine Tunney authored
50
51
52 class ReCaptchaMiddleware(object):
53 """Add IP to ReCaptcha POSTs as workaround to django forms"""
54
55 def process_request(self, request):
56 if request.method == 'POST':
57 if 'recaptcha_challenge_field' in request.POST:
58 data = request.POST.copy()
59 data['recaptcha_magic_ip_field'] = request.META['REMOTE_ADDR']
60 request.POST = data
Something went wrong with that request. Please try again.