diff --git a/bobo/src/bobo.py b/bobo/src/bobo.py index c30f22c..ca7b4f3 100644 --- a/bobo/src/bobo.py +++ b/bobo/src/bobo.py @@ -249,15 +249,8 @@ def __call__(self, environ, start_response): # Maybe middleware can be more tricky? request.charset = 'utf8' - def start_bobo_response(status, headers): - headers = list(headers or ()) - for i, (k, v) in enumerate(headers): - if k.lower() == "location" and isinstance(v, unicode): - headers[i] = k, v.encode("utf-8") - start_response(status, headers) - return self.bobo_response(request, request.path_info, request.method - )(environ, start_bobo_response) + )(environ, start_response) def build_response(self, request, method, data): """Build a response object from raw data. @@ -355,6 +348,8 @@ def redirect(url, status=302, body=None, """ if body is None: body = u'See %s' % url + if isinstance(url, unicode): + url = url.encode('utf-8') response = webob.Response(status=status, headerlist=[('Location', url)]) response.content_type = content_type response.unicode_body = body diff --git a/bobodoctestumentation/src/bobodoctestumentation/main.test b/bobodoctestumentation/src/bobodoctestumentation/main.test index cba04f6..c9773bf 100644 --- a/bobodoctestumentation/src/bobodoctestumentation/main.test +++ b/bobodoctestumentation/src/bobodoctestumentation/main.test @@ -336,3 +336,11 @@ Ordering >>> app = makeapp(bobo_resources='bobo.testmodule1') >>> app.get('/o').text 'f3' + + +Automatic encoding of redirect destinations +------------------------------------------- + + >>> response = bobo.redirect(u"http://www.\u0113xample.com/") + >>> response.headers["location"] + 'http://www.\xc4\x93xample.com/' diff --git a/bobodoctestumentation/src/bobodoctestumentation/more.txt b/bobodoctestumentation/src/bobodoctestumentation/more.txt index deb79bb..34408cd 100644 --- a/bobodoctestumentation/src/bobodoctestumentation/more.txt +++ b/bobodoctestumentation/src/bobodoctestumentation/more.txt @@ -1093,22 +1093,5 @@ Automatic encoding of redirect destinations ------------------------------------------- Since URLs are often computed based on request data, it's easy for -applications to generate Unicode URLs. If we get this, UTF-8 is -generated automatically:: - - import bobo - - @bobo.get("/redirect-me") - def redirect(): - return bobo.redirect(u"http://www.\u0113xample.com/") - - -.. -> src - - >>> update_module('redirection', src) - >>> app = webtest.TestApp(bobo.Application( - ... bobo_resources='redirection')) - - >>> response = app.get("/redirect-me", status=302) - >>> response.headers["location"] - 'http://www.\xc4\x93xample.com/' +applications to generate Unicode URLs. For this reason, unicode URL's +passed to ``bobo.redirect`` are UTF-8 encoded.