Commits

Bruce Kroeze committed 601ff53

adding multiple-domain support

  • Participants
  • Parent commits 1776d57

Comments (0)

Files changed (1)

httpproxy/views.py

 import httplib2
- 
+
 from django.http import HttpResponse
 
 from httpproxy import settings
-from httpproxy.exceptions import UnkownProxyMode
+from httpproxy.exceptions import UnknownProxyMode
 from httpproxy.decorators import normalize_request, rewrite_response
- 
 
-PROXY_FORMAT = u'http://%s:%d%s' % (settings.PROXY_DOMAIN, settings.PROXY_PORT, u'%s')
 
-def proxy(request):
+PROXY_FORMAT = u'http://%s:%d%s'
+
+def proxy(request, proxy_server = None):
+    if proxy_server is None:
+        if hasattr(settings, 'PROXY_DOMAIN'):
+            server = settings.PROXY_DOMAIN
+            port = getattr(settings, 'PROXY_PORT', 80)
+        else:
+            raise UnknownProxyMode("You must specify the PROXY_DOMAIN in your settings")
+
+    else:
+        if hasattr(settings, 'PROXY_DOMAINS'):
+            section = settings.PROXY_DOMAINS[proxy_server]
+            server = section['server']
+            port = section.get('port',80)
+        else:
+            raise UnknownProxyMode("You must specify the PROXY_DOMAINS in your settings if you are going to use multiple proxied servers.")
+
+
     conn = httplib2.Http()
     url = request.path
-    
+
     # Optionally provide authentication for server
     try:
         conn.add_credentials(settings.PROXY_USER, settings.PROXY_PASSWORD)
     except AttributeError:
         pass
-    
+
     if request.method == 'GET':
         url_ending = '%s?%s' % (url, request.GET.urlencode())
-        url = PROXY_FORMAT % url_ending
+        url = PROXY_FORMAT % (server, port, url_ending)
         response, content = conn.request(url, request.method)
-    elif request.method == 'POST':
-        url = PROXY_FORMAT % url
-        data = request.POST.urlencode()
+
+    elif request.method == 'DELETE':
+        url = PROXY_FORMAT % (server, port, url)
+        response, content = conn.request(url, request.method)
+
+    elif request.method in ('POST', 'PUT'):
+        url = PROXY_FORMAT % (server, port, url)
+        if request.method == 'POST':
+            data = request.POST.urlencode()
+        else:
+            data = request.PUT.urlencode()
         response, content = conn.request(url, request.method, data)
+
     return HttpResponse(content, status=int(response['status']), mimetype=response['content-type'])
 
 
     try:
         decorator = getattr(__import__('httpproxy' + '.decorators', fromlist=proxy_mode), proxy_mode)
     except AttributeError, e:
-        raise UnkownProxyMode('The proxy mode "%s" could not be found. Please specify a corresponding decorator function in "%s.decorators".' % (proxy_mode, 'httpproxy'))
+        raise UnknownProxyMode('The proxy mode "%s" could not be found. Please specify a corresponding decorator function in "%s.decorators".' % (proxy_mode, 'httpproxy'))
     else:
         proxy = decorator(proxy)