From 0614f1a3dff92c44d2bf8b3815a26aab34d8883e Mon Sep 17 00:00:00 2001
From: Ivan Tyagov <ivan@nexedi.com>
Date: Thu, 11 Sep 2008 15:42:59 +0000
Subject: [PATCH] Use location header (redirect URL) for http codes between 300
 and 400 (redirects). Try to adjust its arguments in only if they are URLs 
 and point to proxy instance (i.e. it should point to remore instance).

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@23573 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5Wizard/Tool/WizardTool.py | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/product/ERP5Wizard/Tool/WizardTool.py b/product/ERP5Wizard/Tool/WizardTool.py
index a1af94413a..f60b970a8e 100644
--- a/product/ERP5Wizard/Tool/WizardTool.py
+++ b/product/ERP5Wizard/Tool/WizardTool.py
@@ -41,6 +41,7 @@ import xmlrpclib, socket, sys, traceback, urllib, urllib2, base64, cgi
 from AccessControl.SecurityManagement import newSecurityManager, noSecurityManager
 import zLOG
 import cookielib
+from urlparse import urlparse, urlunparse
 
 # global (RAM) cookie storage
 cookiejar = cookielib.CookieJar()
@@ -311,7 +312,7 @@ class WizardTool(BaseTool):
       header_dict['Cookie']  +=  '%s=%s;' %(cookie.name, cookie.value)
     # XXX: include cookies from local browser (like show/hide tabs) which are set directly
     # by client JavaScript code (i.e. not sent from server)
-    
+
     # add HTTP referer (especially useful in Localizer when changing language)
     header_dict['REFERER'] = self.REQUEST.get('HTTP_REFERER', None) or referer
     request = urllib2.Request(url, data, header_dict)
@@ -322,9 +323,17 @@ class WizardTool(BaseTool):
       metadata = f.info()
       response = self.REQUEST.RESPONSE
       if f.code> 300 and f.code <400:
-        # ignore 'location' header for redirects from server. If server requires redirect then 
-        # redirect it ONLY to root web siteor current location
-        response['location'] =  header_dict['REFERER']
+        # adjust return url which my contain proxy URLs as arguments
+        location = metadata.getheader('location')
+        parsed_url = list(urlparse(location))
+        local_site_url_prefix = urllib.quote('%s/portal_wizard/proxy' \
+                                              %self.getPortalObject().absolute_url())
+        remote_url_parsed = urlparse(self.getServerUrl())
+        remote_site_url_prefix = '%s://%s/kb' %(remote_url_parsed[0], remote_url_parsed[1])
+        # fix arguments for returned location URL
+        parsed_url[4] = parsed_url[4].replace(local_site_url_prefix, remote_site_url_prefix)
+        response['location'] = urlunparse(parsed_url)
+
       response.setStatus(f.code, f.msg)
       response.setHeader('content-type', metadata.getheader('content-type'))
       # FIXME this list should be confirmed with the RFC 2616.
-- 
2.30.9