Revision e3ff6830 snf-django-lib/snf_django/lib/api/proxy/__init__.py
b/snf-django-lib/snf_django/lib/api/proxy/__init__.py | ||
---|---|---|
50 | 50 |
EXCLUDE_HEADERS = ['Host', 'Cookie', 'Connection', 'X-Forwarded-Host'] |
51 | 51 |
|
52 | 52 |
|
53 |
def proxy(request, target):
|
|
53 |
def proxy(request, proxy_base=None, target_base=None):
|
|
54 | 54 |
kwargs = {} |
55 | 55 |
|
56 |
if None in (proxy_base, target_base): |
|
57 |
m = "proxy() needs both proxy_base and target_base argument not None" |
|
58 |
raise AssertionError(m) |
|
59 |
|
|
60 |
parsed = urlparse.urlparse(target_base) |
|
61 |
target_base = '/' + parsed.path.strip('/') |
|
62 |
proxy_base = proxy_base.strip('/') |
|
63 |
|
|
56 | 64 |
# prepare headers |
57 | 65 |
headers = dict(map(lambda (k, v): fix_header(k, v), |
58 | 66 |
filter(lambda (k, v): forward_header(k), |
... | ... | |
75 | 83 |
kwargs['headers'] = headers |
76 | 84 |
kwargs['body'] = request.raw_post_data |
77 | 85 |
|
78 |
p = urlparse.urlparse(target) |
|
79 |
path = join_urls(p.path, request.path) |
|
80 |
with PooledHTTPConnection(p.netloc, p.scheme) as conn: |
|
86 |
path = request.path.lstrip('/') |
|
87 |
if path.startswith(proxy_base): |
|
88 |
m = "request path '{0}' does not start with proxy_base '{1}'" |
|
89 |
m = m.format(path, proxy_base) |
|
90 |
path = path.replace(proxy_base, '', 1) |
|
91 |
path = join_urls(target_base, path) |
|
92 |
with PooledHTTPConnection(parsed.netloc, parsed.scheme) as conn: |
|
81 | 93 |
conn.request( |
82 | 94 |
request.method, |
83 | 95 |
'?'.join([path, urllib.urlencode(request.GET)]), **kwargs) |
Also available in: Unified diff