Skip to content

Commit

Permalink
add charset to url endoded string #1750
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Mar 27, 2017
1 parent 56132f9 commit 7d7e996
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion aiohttp/formdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def _gen_form_urlencoded(self):
charset = self._charset if self._charset is not None else 'utf-8'
return payload.BytesPayload(
urlencode(data, doseq=True, encoding=charset).encode(),
content_type='application/x-www-form-urlencoded')
content_type=('application/x-www-form-urlencoded; '
'charset=%s' % charset))

This comment has been minimized.

Copy link
@fafhrd91

fafhrd91 Mar 27, 2017

Author Member

@kxepal what do you think about this line? is this ok?

This comment has been minimized.

Copy link
@kxepal

kxepal Mar 27, 2017

Member

HTML5 spec tells that this is wrong usage of that MIME type:

Parameters on the application/x-www-form-urlencoded MIME type are ignored. In particular, this MIME type does not support the charset parameter.

And suggest to not use the MIME parameter, but a form field that defines the charset /o\

Another store fromrequest project: they had similar feature, but then decided to drop it.

Personally, I would like to stay on spec side unless we discover any case when this parameter could save some life...or just let server accept the request. People told that there are some old IIS servers acts like that, but I'm not sure home much old those were are and should we cary that stone on our neck today.

This comment has been minimized.

Copy link
@fafhrd91

fafhrd91 Mar 27, 2017

Author Member

oh f..k, then we need to allow to specify charset in query decoding code.
or just do not bother and always use utf-8 for decoding?


def _gen_form_data(self):
"""Encode a list of fields using the multipart/form-data MIME format"""
Expand Down
27 changes: 27 additions & 0 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,30 @@ def handler(request):
resp.close()


@asyncio.coroutine
def test_POST_DATA_formdats_with_charset(loop, test_client):
@asyncio.coroutine
def handler(request):
mp = yield from request.post()
assert 'name' in mp
from pprint import pprint
pprint(dict(request.headers))
return web.Response(text=mp['name'])

app = web.Application()
app.router.add_post('/', handler)
client = yield from test_client(app)

form = aiohttp.FormData(charset='koi8-r')
form.add_field('name', 'текст')

resp = yield from client.post('/', data=form)
assert 200 == resp.status
content = yield from resp.text()
assert content == 'текст'
resp.close()


@asyncio.coroutine
def test_POST_DATA_with_charset_post(loop, test_client):
@asyncio.coroutine
Expand Down Expand Up @@ -1347,6 +1371,9 @@ def handler(request):
assert request.content_type in ['application/pgp-keys',
'text/plain',
'application/octet-stream']
assert request.headers['content-disposition'] == (
"inline; filename=\"sample.key\"; filename*=utf-8''sample.key")

return web.HTTPOk()

app = web.Application()
Expand Down

0 comments on commit 7d7e996

Please sign in to comment.