From 9c6a59e1008c87f17578a57e4f56bf23eccccb6b Mon Sep 17 00:00:00 2001 From: Marcel Hellkamp Date: Fri, 13 May 2011 22:24:03 +0200 Subject: fix: "SERVER_NAME is substituted for '127.0.0.1' for relative redirects" issue #159 SERVER_NAME is only required for HTTP/1.0 clients that do not define a HTTP_HOST header. The HOST header, if present, should be correct. --- bottle.py | 19 ++++++++++--------- test/test_environ.py | 5 ++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/bottle.py b/bottle.py index f449e18..60e4e98 100755 --- a/bottle.py +++ b/bottle.py @@ -119,7 +119,7 @@ class DictProperty(object): return self def __get__(self, obj, cls): - if not obj: return self + if obj is None: return self key, storage = self.key, getattr(obj, self.attr) if key not in storage: storage[key] = self.getter(obj) return storage[key] @@ -835,15 +835,14 @@ class Request(threading.local, DictMixin): The fragment is always empty because it is not visible to the server. ''' env = self.environ - host = env.get('HTTP_X_FORWARDED_HOST') or env.get('HTTP_HOST', '') http = env.get('wsgi.url_scheme', 'http') - port = env.get('SERVER_PORT') - if ':' in host: # Overrule SERVER_POST (proxy support) - host, port = host.rsplit(':', 1) - if not host or host == '127.0.0.1': - host = env.get('SERVER_NAME', host) - if port and http+port not in ('http80', 'https443'): - host += ':' + port + host = env.get('HTTP_X_FORWARDED_HOST') or env.get('HTTP_HOST') + if not host: + # HTTP 1.1 requires a Host-header. This is for HTTP/1.0 clients. + host = env.get('SERVER_NAME', '127.0.0.1') + port = env.get('SERVER_PORT') + if port and port != ('80' if http == 'http' else '443'): + host += ':' + port spath = self.environ.get('SCRIPT_NAME','').rstrip('/') + '/' rpath = self.path.lstrip('/') path = urlquote(urljoin(spath, rpath)) @@ -852,6 +851,8 @@ class Request(threading.local, DictMixin): @property def url(self): """ Full URL as requested by the client. """ + if not hasattr(self.urlparts, 'geturl'): + print repr(self), repr(self.urlparts) return self.urlparts.geturl() @property diff --git a/test/test_environ.py b/test/test_environ.py index 3612027..f917e70 100755 --- a/test/test_environ.py +++ b/test/test_environ.py @@ -242,6 +242,8 @@ class TestResponse(unittest.TestCase): if name.title() == 'Set-Cookie'] self.assertTrue('name=;' in cookies[0]) + + class TestRedirect(unittest.TestCase): def assertRedirect(self, target, result, query=None, status=303, **args): @@ -251,7 +253,6 @@ class TestRedirect(unittest.TestCase): args[key.replace('_', '.', 1)] = args[key] del args[key] env.update(args) - wsgiref.util.setup_testing_defaults(env) request.bind(env) try: bottle.redirect(target, **(query or {})) @@ -313,8 +314,6 @@ class TestRedirect(unittest.TestCase): HTTP_X_FORWARDED_HOST='example.com') self.assertRedirect('./test.html', 'http://example.com/test.html', SERVER_NAME='example.com') - self.assertRedirect('./test.html', 'http://example.com/test.html', - HTTP_HOST='example.com:80') self.assertRedirect('./test.html', 'http://example.com:81/test.html', HTTP_HOST='example.com:81') self.assertRedirect('./test.html', 'http://127.0.0.1:81/test.html', -- cgit v1.2.1