Skip to content

Commit

Permalink
Merge pull request #1416 from liberapay/python3-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Changaco authored Jan 30, 2019
2 parents 05349cb + 8db0531 commit da3c14e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 25 deletions.
22 changes: 8 additions & 14 deletions liberapay/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,22 +372,16 @@ def _decode_body(self):
# The monkey-patch below is only for Pando 0.45, it should be removed after that
def make_franken_uri(path, qs):
if path:
try:
if type(path) is bytes:
path.decode('ascii')
else:
path = path.encode('ascii')
except UnicodeError:
path = urlquote(path, '%/').encode('ascii')
if type(path) is bytes:
path = urlquote(path, string.punctuation).encode('ascii')
else:
path = urlquote(path, string.punctuation, 'latin1').encode('ascii')

if qs:
try:
if type(qs) is bytes:
qs.decode('ascii')
else:
qs = qs.encode('ascii')
except UnicodeError:
qs = urlquote_plus(qs, '%=&').encode('ascii')
if type(qs) is bytes:
qs = urlquote_plus(qs, string.punctuation).encode('ascii')
else:
qs = urlquote_plus(qs, string.punctuation, 'latin1').encode('ascii')
qs = b'?' + qs

return path + qs
Expand Down
3 changes: 2 additions & 1 deletion liberapay/wireup.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,8 @@ def tell_sentry(exception, state, allow_reraise=True):
'method': request.method,
'url': request.line.uri.decoded,
'headers': {
k: b', '.join(v) for k, v in request.headers.items()
k.decode('ascii', 'repr'): b', '.join(v).decode('ascii', 'repr')
for k, v in request.headers.items()
if k != b'Cookie'
},
}
Expand Down
14 changes: 4 additions & 10 deletions tests/py/test_state_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,25 +195,19 @@ def test_null_byte_results_in_400(self):
assert r.code == 400, r.text

def test_quoted_unicode_path_is_okay(self):
r = self.client.GET('/about/%C3%A9', raise_immediately=False)
assert r.code == 404, r.text
r = self.client.GET('', PATH_INFO='/about/%C3%A9', raise_immediately=False)
assert r.code == 404, r.text

def test_unquoted_unicode_path_is_okay(self):
r = self.client.GET('/about/é'.encode('utf8'), raise_immediately=False)
assert r.code == 404, r.text
r = self.client.GET('', PATH_INFO='/about/é', raise_immediately=False)
path = '/about/é'.encode('utf8').decode('latin1')
r = self.client.GET('', PATH_INFO=path, raise_immediately=False)
assert r.code == 404, r.text

def test_quoted_unicode_querystring_is_okay(self):
r = self.client.GET('/', QUERY_STRING=b'%C3%A9=%C3%A9', raise_immediately=False)
assert r.code == 200, r.text
r = self.client.GET('/', QUERY_STRING='%C3%A9=%C3%A9', raise_immediately=False)
assert r.code == 200, r.text

def test_unquoted_unicode_querystring_is_okay(self):
r = self.client.GET('/', QUERY_STRING='é=é'.encode('utf8'), raise_immediately=False)
assert r.code == 200, r.text
r = self.client.GET('/', QUERY_STRING='é=é', raise_immediately=False)
qs = 'é=é'.encode('utf8').decode('latin1')
r = self.client.GET('/', QUERY_STRING=qs, raise_immediately=False)
assert r.code == 200, r.text

0 comments on commit da3c14e

Please sign in to comment.