Skip to content

Commit a1b0616

Browse files
Tolerate slightly invalid formats in query strings (Fixes miguelgrinberg#126)
1 parent 67798f7 commit a1b0616

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/microdot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,11 +404,11 @@ def _parse_urlencoded(self, urlencoded):
404404
if len(urlencoded) > 0:
405405
if isinstance(urlencoded, str):
406406
for k, v in [pair.split('=', 1)
407-
for pair in urlencoded.split('&')]:
407+
for pair in urlencoded.split('&') if pair]:
408408
data[urldecode_str(k)] = urldecode_str(v)
409409
elif isinstance(urlencoded, bytes): # pragma: no branch
410410
for k, v in [pair.split(b'=', 1)
411-
for pair in urlencoded.split(b'&')]:
411+
for pair in urlencoded.split(b'&') if pair]:
412412
data[urldecode_bytes(k)] = urldecode_bytes(v)
413413
return data
414414

tests/test_request.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ def test_args(self):
4545
self.assertEqual(req.args, MultiDict(
4646
{'foo': 'bar', 'abc': 'def', 'x': '/%%'}))
4747

48+
def test_badly_formatted_args(self):
49+
fd = get_request_fd('GET', '/?&foo=bar&abc=def&&&x=%2f%%')
50+
req = Request.create('app', fd, 'addr')
51+
self.assertEqual(req.query_string, '&foo=bar&abc=def&&&x=%2f%%')
52+
self.assertEqual(req.args, MultiDict(
53+
{'foo': 'bar', 'abc': 'def', 'x': '/%%'}))
54+
4855
def test_json(self):
4956
fd = get_request_fd('GET', '/foo', headers={
5057
'Content-Type': 'application/json'}, body='{"foo":"bar"}')

0 commit comments

Comments
 (0)