Skip to content

Commit

Permalink
Fix parsing and decoding of application/x-www-urlencoded payloads (#187)
Browse files Browse the repository at this point in the history
* Fix parsing and decoding of application/x-www-urlencoded payloads

* Fix NameError in test case
  • Loading branch information
spaceone authored and prologic committed Jan 8, 2017
1 parent dd604e3 commit 5fcd7f2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
16 changes: 14 additions & 2 deletions circuits/web/processors.py
Expand Up @@ -36,8 +36,20 @@ def process_multipart(request, params):

def process_urlencoded(request, params, encoding="utf-8"):
params.update(QueryStringParser(request.qs).result)
body = request.body.getvalue().decode(encoding)
params.update(QueryStringParser(body).result)
body = request.body.getvalue()
result = QueryStringParser(body).result
for key, value in result.items():
params[key.decode(encoding)] = _decode_value(value, encoding)


def _decode_value(value, encoding):
if isinstance(value, bytes):
value = value.decode(encoding)
elif isinstance(value, list):
value = [_decode_value(val, encoding) for val in value]
elif isinstance(value, dict):
value = dict((key.decode(encoding), _decode_value(val, encoding)) for key, val in value.iteritems())
return value


def process(request, params):
Expand Down
13 changes: 7 additions & 6 deletions tests/web/test_websockets.py
Expand Up @@ -3,6 +3,7 @@

from __future__ import print_function

import pytest

from circuits import Component
from circuits.web.controllers import Controller
Expand Down Expand Up @@ -49,7 +50,8 @@ def read(self, data):
self.response = data


def test(manager, watcher, webapp):
@pytest.mark.parametrize('chunksize', [BUFSIZE, BUFSIZE + 1, BUFSIZE * 2])
def test(manager, watcher, webapp, chunksize):
echo = Echo().register(webapp)
assert watcher.wait("registered", channel="wsserver")

Expand Down Expand Up @@ -81,11 +83,10 @@ def test(manager, watcher, webapp):
assert watcher.wait("read", channel="ws")
assert client.response == "Received: Hello!"

for size in (BUFSIZE, BUFSIZE + 1, BUFSZIE * 2):
data = "A" * (size + 1)
client.fire(write(data), "ws")
assert watcher.wait("read", channel="ws")
assert client.response == "Received: %s" % (data,)
data = "A" * (chunksize + 1)
client.fire(write(data), "ws")
assert watcher.wait("read", channel="ws")
assert client.response == "Received: %s" % (data,)

f = urlopen(webapp.server.http.base)
s = f.read()
Expand Down

0 comments on commit 5fcd7f2

Please sign in to comment.