Commits

Bruce Kroeze committed ddf006c

Adding error checking in proxy server defs

Comments (0)

Files changed (1)

httpproxy/views.py

 from httpproxy.decorators import normalize_request, rewrite_response
 
 
-PROXY_FORMAT = u'http://%s:%d%s'
+PROXY_FORMAT = u'%s:%d%s'
 
 def proxy(request, proxy_server = None):
+    user = None
+    password = None
     if proxy_server is None:
         if hasattr(settings, 'PROXY_DOMAIN'):
             server = settings.PROXY_DOMAIN
             port = getattr(settings, 'PROXY_PORT', 80)
+            user = getattr(settings, 'PROXY_USER', None)
+            password = getattr(settings, 'PROXY_PASSWORD', None)
         else:
             raise UnknownProxyMode("You must specify the PROXY_DOMAIN in your settings")
 
             section = settings.PROXY_DOMAINS[proxy_server]
             server = section['server']
             port = section.get('port',80)
+            user = section.get('user', None)
+            password = section.get('password', None)
         else:
             raise UnknownProxyMode("You must specify the PROXY_DOMAINS in your settings if you are going to use multiple proxied servers.")
 
 
+    if not server.startswith('http'):
+        fmt = 'http://%s' % PROXY_FORMAT
+
     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 user is not None and password is not None:
+        conn.add_credentials(user, password)
 
     if request.method == 'GET':
         url_ending = '%s?%s' % (url, request.GET.urlencode())
-        url = PROXY_FORMAT % (server, port, url_ending)
+        url = fmt % (server, port, url_ending)
         response, content = conn.request(url, request.method)
 
     elif request.method == 'DELETE':
-        url = PROXY_FORMAT % (server, port, url)
+        url = fmt % (server, port, url)
         response, content = conn.request(url, request.method)
 
     elif request.method in ('POST', 'PUT'):
-        url = PROXY_FORMAT % (server, port, url)
+        url = fmt % (server, port, url)
         if request.method == 'POST':
             data = request.POST.urlencode()
         else: