1
0
mirror of synced 2024-11-24 06:20:12 +01:00

Fix parsing of API client content-type string.

This commit is contained in:
Jennifer Taylor 2021-05-08 01:49:42 +00:00
parent a2ceb5cd9e
commit 8b54d7ccd5
2 changed files with 39 additions and 1 deletions

View File

@ -42,6 +42,22 @@ class APIClient:
self.allow_stats = allow_stats
self.allow_scores = allow_scores
def __content_type_valid(self, content_type: str) -> bool:
if ';' in content_type:
left, right = content_type.split(';', 1)
left = left.strip().lower()
right = right.strip().lower()
if left == 'application/json' and ('=' in right):
identifier, charset = right.split('=', 1)
identifier = identifier.strip()
charset = charset.strip()
if identifier == 'charset' and charset == 'utf-8':
# This is valid.
return True
return False
def __exchange_data(self, request_uri: str, request_args: Dict[str, Any]) -> Dict[str, Any]:
if self.base_uri[-1:] != '/':
uri = f'{self.base_uri}/{request_uri}'
@ -66,7 +82,8 @@ class APIClient:
except Exception:
raise APIException('Failed to query remote server!')
if r.headers['content-type'] != 'application/json; charset=utf-8':
# Verify that content type is in the form of "application/json; charset=utf-8".
if not self.__content_type_valid(r.headers['content-type']):
raise APIException(f'API returned invalid content type \'{r.headers["content-type"]}\'!')
jsondata = r.json()

View File

@ -0,0 +1,21 @@
# vim: set fileencoding=utf-8
import unittest
from bemani.data.api.client import APIClient
class TestAPIClient(unittest.TestCase):
def test_content_type(self) -> None:
client = APIClient('https://127.0.0.1', 'token', False, False)
self.assertFalse(client._APIClient__content_type_valid('application/text'))
self.assertFalse(client._APIClient__content_type_valid('application/json'))
self.assertFalse(client._APIClient__content_type_valid('application/json; charset=shift-jis'))
self.assertTrue(client._APIClient__content_type_valid('application/json; charset=utf-8'))
self.assertTrue(client._APIClient__content_type_valid('application/json;charset=utf-8'))
self.assertTrue(client._APIClient__content_type_valid('application/json;charset = utf-8'))
self.assertTrue(client._APIClient__content_type_valid('application/json; charset = utf-8'))
self.assertTrue(client._APIClient__content_type_valid('application/json; charset=UTF-8'))
self.assertTrue(client._APIClient__content_type_valid('application/json;charset=UTF-8'))
self.assertTrue(client._APIClient__content_type_valid('application/json;charset = UTF-8'))
self.assertTrue(client._APIClient__content_type_valid('application/json; charset = UTF-8'))