Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
simplify; only encode for bobo.redirect; separate documentation from …
Browse files Browse the repository at this point in the history
…test
  • Loading branch information
freddrake committed Nov 21, 2014
1 parent 35f4c29 commit ab62e19
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 27 deletions.
11 changes: 3 additions & 8 deletions bobo/src/bobo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions bobodoctestumentation/src/bobodoctestumentation/main.test
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
21 changes: 2 additions & 19 deletions bobodoctestumentation/src/bobodoctestumentation/more.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit ab62e19

Please sign in to comment.