Commit a4e47f36 authored by Ivan Tyagov's avatar Ivan Tyagov

Add simple support for HTTP codes between 300 and 400 (redirects).


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@23485 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 5e7f9ff3
...@@ -44,7 +44,8 @@ import cookielib ...@@ -44,7 +44,8 @@ import cookielib
# global (RAM) cookie storage # global (RAM) cookie storage
cookiejar = cookielib.CookieJar() cookiejar = cookielib.CookieJar()
referer = None
def _setSuperSecurityManager(self): def _setSuperSecurityManager(self):
""" Change to super user account. """ """ Change to super user account. """
user = self.getWrappedOwner() user = self.getWrappedOwner()
...@@ -148,7 +149,12 @@ class WizardTool(BaseTool): ...@@ -148,7 +149,12 @@ class WizardTool(BaseTool):
subpath = path[:-1] subpath = path[:-1]
subpath.reverse() subpath.reverse()
request.set('traverse_subpath', subpath) request.set('traverse_subpath', subpath)
# initialize our root proxy URL which we use for a referer
global referer
path[:-1] = [] path[:-1] = []
if referer is None:
referer = '%s/portal_wizard/proxy/%s/view' %(self.getPortalObject().absolute_url(), \
'/'.join(subpath[:3]))
def _getProxyURL(self, subpath='', query=''): def _getProxyURL(self, subpath='', query=''):
# Helper method to construct an URL appropriate for proxying a request. # Helper method to construct an URL appropriate for proxying a request.
...@@ -206,18 +212,19 @@ class WizardTool(BaseTool): ...@@ -206,18 +212,19 @@ class WizardTool(BaseTool):
# and errors automatically. This is necessary because the proxy # and errors automatically. This is necessary because the proxy
# should pass all results to a client as they are. # should pass all results to a client as they are.
simple_opener_director = urllib2.OpenerDirector() simple_opener_director = urllib2.OpenerDirector()
for name in ('ProxyHandler', 'UnknownHandler', 'HTTPHandler', for name in ('ProxyHandler', 'UnknownHandler', \
'FTPHandler', 'FileHandler', 'HTTPSHandler'): 'HTTPHandler', 'FTPHandler',
'FileHandler', 'HTTPSHandler',):
handler = getattr(urllib2, name, None) handler = getattr(urllib2, name, None)
if handler is not None: if handler is not None:
simple_opener_director.add_handler(handler()) simple_opener_director.add_handler(handler())
# add cookie support # add cookie support
simple_opener_director.add_handler(urllib2.HTTPCookieProcessor(cookiejar)) simple_opener_director.add_handler(urllib2.HTTPCookieProcessor(cookiejar))
security.declareProtected(Permissions.View, 'proxy') security.declareProtected(Permissions.View, 'proxy')
def proxy(self, **kw): def proxy(self, **kw):
"""Proxy a request to a server.""" """Proxy a request to a server."""
global cookiejar global cookiejar, referer
if self.REQUEST['REQUEST_METHOD'] != 'GET': if self.REQUEST['REQUEST_METHOD'] != 'GET':
# XXX this depends on the internal of HTTPRequest. # XXX this depends on the internal of HTTPRequest.
pos = self.REQUEST.stdin.tell() pos = self.REQUEST.stdin.tell()
...@@ -229,7 +236,7 @@ class WizardTool(BaseTool): ...@@ -229,7 +236,7 @@ class WizardTool(BaseTool):
data = None data = None
content_type = self.REQUEST.get_header('content-type') content_type = self.REQUEST.get_header('content-type')
# XXX if ":method" trick is used, then remove it from subpath. # XXX if ":method" trick is used, then remove it from subpath.
if self.REQUEST.traverse_subpath: if self.REQUEST.traverse_subpath:
if data is not None: if data is not None:
...@@ -280,6 +287,8 @@ class WizardTool(BaseTool): ...@@ -280,6 +287,8 @@ class WizardTool(BaseTool):
# XXX: include cookies from local browser (like show/hide tabs) which are set directly # XXX: include cookies from local browser (like show/hide tabs) which are set directly
# by client JavaScript code (i.e. not sent from server) # 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) request = urllib2.Request(url, data, header_dict)
f = self.simple_opener_director.open(request) f = self.simple_opener_director.open(request)
...@@ -287,10 +296,14 @@ class WizardTool(BaseTool): ...@@ -287,10 +296,14 @@ class WizardTool(BaseTool):
data = f.read() data = f.read()
metadata = f.info() metadata = f.info()
response = self.REQUEST.RESPONSE 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']
response.setStatus(f.code, f.msg) response.setStatus(f.code, f.msg)
response.setHeader('content-type', metadata.getheader('content-type')) response.setHeader('content-type', metadata.getheader('content-type'))
# FIXME this list should be confirmed with the RFC 2616. # FIXME this list should be confirmed with the RFC 2616.
for k in ('location', 'uri', 'cache-control', 'last-modified', for k in ('uri', 'cache-control', 'last-modified',
'etag', 'if-matched', 'if-none-match', 'etag', 'if-matched', 'if-none-match',
'if-range', 'content-language', 'content-range' 'if-range', 'content-language', 'content-range'
'content-location', 'content-md5', 'expires', 'content-location', 'content-md5', 'expires',
...@@ -298,7 +311,6 @@ class WizardTool(BaseTool): ...@@ -298,7 +311,6 @@ class WizardTool(BaseTool):
'content-length', 'age'): 'content-length', 'age'):
if k in metadata: if k in metadata:
response.setHeader(k, metadata.getheader(k)) response.setHeader(k, metadata.getheader(k))
return data return data
finally: finally:
f.close() f.close()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment