Revision dd21b1c4 snf-django-lib/snf_django/lib/api/proxy/__init__.py

b/snf-django-lib/snf_django/lib/api/proxy/__init__.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
from django.http import HttpResponse
34
from django.http import HttpResponse, HttpResponseRedirect
35 35

  
36 36
from objpool.http import PooledHTTPConnection
37 37

  
......
50 50
EXCLUDE_HEADERS = ['Host', 'Cookie', 'Connection', 'X-Forwarded-Host']
51 51

  
52 52

  
53
def proxy(request, proxy_base=None, target_base=None):
53
def proxy(request, proxy_base=None, target_base=None, redirect=False):
54 54
    kwargs = {}
55 55

  
56 56
    if None in (proxy_base, target_base):
......
62 62
    target_base = str(target_base)
63 63

  
64 64
    parsed = urlparse.urlparse(target_base)
65
    target_base = '/' + parsed.path.strip('/')
65
    target_path = '/' + parsed.path.strip('/')
66 66
    proxy_base = proxy_base.strip('/')
67 67

  
68 68
    # prepare headers
......
93 93
        m = m.format(path, proxy_base)
94 94
        raise AssertionError(m)
95 95
    path = path.replace(proxy_base, '', 1)
96
    path = join_urls(target_base, path)
96

  
97
    # redirect to target instead of proxing
98
    if redirect:
99
        redirect_url = join_urls(target_base, path)
100
        qs = urllib.urlencode(request.GET)
101
        return HttpResponseRedirect('?'.join([redirect_url, qs]))
102

  
103
    path = join_urls(target_path, path)
97 104
    with PooledHTTPConnection(parsed.netloc, parsed.scheme) as conn:
98 105
        conn.request(
99 106
            request.method,

Also available in: Unified diff