diff options
author | ibizaman <ibizapeanut@gmail.com> | 2022-05-28 00:02:47 -0700 |
---|---|---|
committer | Calum Lind <calumlind+deluge@gmail.com> | 2022-06-12 16:08:36 +0100 |
commit | b0dba97fec8dccdaa45f1112f84068cf7415a4ed (patch) | |
tree | 6e9b97f30d523768b8dc261005a1dd540f4d6fa7 | |
parent | d7c520c85ead7960eebbbe9bf883f8d16a7e92a6 (diff) | |
download | deluge-b0dba97fec8dccdaa45f1112f84068cf7415a4ed.tar.gz deluge-b0dba97fec8dccdaa45f1112f84068cf7415a4ed.tar.bz2 deluge-b0dba97fec8dccdaa45f1112f84068cf7415a4ed.zip |
[Web] Accept charset in content-type for json messages
Trac: https://dev.deluge-torrent.org/ticket/3521
Closes: https://github.com/deluge-torrent/deluge/pull/385
-rw-r--r-- | deluge/tests/test_json_api.py | 18 | ||||
-rw-r--r-- | deluge/ui/web/json_api.py | 3 |
2 files changed, 20 insertions, 1 deletions
diff --git a/deluge/tests/test_json_api.py b/deluge/tests/test_json_api.py index c6eedd331..41efb0206 100644 --- a/deluge/tests/test_json_api.py +++ b/deluge/tests/test_json_api.py @@ -98,6 +98,24 @@ class TestJSON: with pytest.raises(JSONException): json._on_json_request(request) + def test_on_json_request_valid_content_type(self): + """Ensure content-type application/json is accepted""" + json = JSON() + request = MagicMock() + request.getHeader.return_value = b'application/json' + json_data = {'method': 'some.method', 'id': 0, 'params': []} + request.json = json_lib.dumps(json_data).encode() + json._on_json_request(request) + + def test_on_json_request_valid_content_type_with_charset(self): + """Ensure content-type parameters such as charset are ignored""" + json = JSON() + request = MagicMock() + request.getHeader.return_value = b'application/json;charset=utf-8' + json_data = {'method': 'some.method', 'id': 0, 'params': []} + request.json = json_lib.dumps(json_data).encode() + json._on_json_request(request) + @pytest.mark.usefixtures('daemon', 'client', 'component') class TestJSONCustomUserTestCase: diff --git a/deluge/ui/web/json_api.py b/deluge/ui/web/json_api.py index c487ddf3c..3f256140e 100644 --- a/deluge/ui/web/json_api.py +++ b/deluge/ui/web/json_api.py @@ -6,6 +6,7 @@ # See LICENSE for more details. # +import cgi import json import logging import os @@ -190,7 +191,7 @@ class JSON(resource.Resource, component.Component): Handler to take the json data as a string and pass it on to the _handle_request method for further processing. """ - content_type = request.getHeader(b'content-type').decode() + content_type, _ = cgi.parse_header(request.getHeader(b'content-type').decode()) if content_type != 'application/json': message = 'Invalid JSON request content-type: %s' % content_type raise JSONException(message) |