Revision 142133fb
b/snf-django-lib/snf_django/lib/api/proxy/__init__.py | ||
---|---|---|
40 | 40 |
import urllib |
41 | 41 |
import urlparse |
42 | 42 |
|
43 |
# We use proxy to delegate requests to another domain. Sending host specific |
|
44 |
# headers (Host, Cookie) may cause confusion to the server we proxy to. |
|
45 |
# |
|
46 |
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.10 |
|
47 |
# Connection and MUST NOT be communicated by proxies over further connections |
|
48 |
EXCLUDE_HEADERS = ['Host', 'Cookie', 'Connection'] |
|
49 |
|
|
43 | 50 |
|
44 | 51 |
def proxy(request, target): |
45 | 52 |
kwargs = {} |
... | ... | |
49 | 56 |
filter(lambda (k, v): forward_header(k), |
50 | 57 |
request.META.iteritems()))) |
51 | 58 |
|
59 |
# set X-Forwarded-For, if already set, pass it through, otherwise set it |
|
60 |
# to the current request remote address |
|
61 |
SOURCE_IP = request.META.get('REMOTE_ADDR', None) |
|
62 |
if SOURCE_IP and not 'X-Forwarded-For' in headers: |
|
63 |
headers['X-Forwarded-For'] = SOURCE_IP |
|
64 |
|
|
65 |
# request.META remains cleanup |
|
66 |
for k in headers.keys(): |
|
67 |
if '_' in k: |
|
68 |
headers.pop(k) |
|
69 |
|
|
70 |
for k in EXCLUDE_HEADERS: |
|
71 |
headers.pop(k, None) |
|
72 |
|
|
52 | 73 |
kwargs['headers'] = headers |
53 | 74 |
kwargs['body'] = request.raw_post_data |
54 | 75 |
|
b/snf-django-lib/snf_django/lib/api/proxy/utils.py | ||
---|---|---|
38 | 38 |
prefix = 'HTTP_' |
39 | 39 |
if k.startswith(prefix): |
40 | 40 |
k = k[len(prefix):].title().replace('_', '-') |
41 |
elif k in ('CONTENT_TYPE', 'CONTENT_LENGTH'): |
|
42 |
k = k.replace('_', '-') |
|
43 | 41 |
return k, v |
44 | 42 |
|
45 | 43 |
|
46 | 44 |
def forward_header(k): |
47 |
return k.lower() != "HOST" and not is_hop_by_hop(k) |
|
45 |
return k.upper() not in ['HOST', 'CONTENT_LENGTH', 'CONTENT_TYPE'] and \ |
|
46 |
not is_hop_by_hop(k) and not '.' in k |
Also available in: Unified diff