def __init__(self, query_string=None, mutable=False, encoding=None): super(QueryDict, self).__init__() if not encoding: encoding = settings.DEFAULT_CHARSET self.encoding = encoding query_string = query_string or '' parse_qsl_kwargs = { 'keep_blank_values': True, 'fields_limit': settings.DATA_UPLOAD_MAX_NUMBER_FIELDS, 'encoding': encoding, } if six.PY3: if isinstance(query_string, bytes): # query_string normally contains URL-encoded data, a subset of ASCII. try: query_string = query_string.decode(encoding) except UnicodeDecodeError: # ... but some user agents are misbehaving :-( query_string = query_string.decode('iso-8859-1') for key, value in limited_parse_qsl(query_string, **parse_qsl_kwargs): self.appendlist(key, value) else: for key, value in limited_parse_qsl(query_string, **parse_qsl_kwargs): try: value = value.decode(encoding) except UnicodeDecodeError: value = value.decode('iso-8859-1') self.appendlist(force_text(key, encoding, errors='replace'), value) self._mutable = mutable
def test_parse_qsl_encoding(self): result = limited_parse_qsl('key=\u0141%E9', encoding='latin-1') self.assertEqual(result, [('key', '\u0141\xE9')]) result = limited_parse_qsl('key=\u0141%C3%A9', encoding='utf-8') self.assertEqual(result, [('key', '\u0141\xE9')]) result = limited_parse_qsl('key=\u0141%C3%A9', encoding='ascii') self.assertEqual(result, [('key', '\u0141\ufffd\ufffd')]) result = limited_parse_qsl('key=\u0141%E9-', encoding='ascii') self.assertEqual(result, [('key', '\u0141\ufffd-')]) result = limited_parse_qsl('key=\u0141%E9-', encoding='ascii', errors='ignore') self.assertEqual(result, [('key', '\u0141-')])
def __call__(self, scope): #FIXME: handle bad utf-8 encoded urls query_string = limited_parse_qsl(scope['query_string'].decode()) for key, val in query_string: if key == 'token': try: token = Token.objects.get(key=val) scope['user'] = token.user except Token.DoesNotExist: pass close_old_connections() return self.inner(scope)
def test_parse_qsl(self): tests = [ ('', []), ('&', []), ('&&', []), ('=', [('', '')]), ('=a', [('', 'a')]), ('a', [('a', '')]), ('a=', [('a', '')]), ('&a=b', [('a', 'b')]), ('a=a+b&b=b+c', [('a', 'a b'), ('b', 'b c')]), ('a=1&a=2', [('a', '1'), ('a', '2')]), (';a=b', [(';a', 'b')]), ('a=a+b;b=b+c', [('a', 'a b;b=b c')]), ] for original, expected in tests: with self.subTest(original): result = limited_parse_qsl(original, keep_blank_values=True) self.assertEqual(result, expected, 'Error parsing %r' % original) expect_without_blanks = [v for v in expected if len(v[1])] result = limited_parse_qsl(original, keep_blank_values=False) self.assertEqual(result, expect_without_blanks, 'Error parsing %r' % original)
def wrapper(self, request, *args, **kwargs): _data = request.data.copy() for key, value in _data.items(): if isinstance(value, (list, tuple)) and len(value) == 1: _data[key] = value[0] encoded_data = urlencode(_data, True) _mutable = request.query_params._mutable # pylint: disable=protected-access request.query_params._mutable = True # pylint: disable=protected-access for key, value in limited_parse_qsl(encoded_data): request.query_params.appendlist(key, value) request.query_params._mutable = _mutable # pylint: disable=protected-access return func_to_decorate(self, request, *args, **kwargs)
def sso_service_successful_login(context): response = context.response user = context.user client_util = SSOClientUtils(settings.DISCOURSE_SSO_SECRET, settings.DISCOURSE_SSO_REDIRECT) nonce_val = nonce_service.generate_nonce() sso_url = client_util.generate_sso_url(nonce_val, False) parsed_qsl = limited_parse_qsl(sso_url.split('?')[1]) gen = ForumProducerUtils(sso_key=settings.DISCOURSE_SSO_SECRET, consumer_url=settings.DISCOURSE_SSO_REDIRECT, user=user, sso=parsed_qsl[0][1], sig=parsed_qsl[1][1]) final_session_payload = gen.get_signed_payload() sso_login_url = gen.get_sso_redirect(final_session_payload) assert len(response.redirect_chain) == 2 assert response.redirect_chain[1][0] == sso_login_url
def __init__(self, query_string=None, mutable=False, encoding=None): super().__init__() self.encoding = encoding or settings.DEFAULT_CHARSET query_string = query_string or '' parse_qsl_kwargs = { 'keep_blank_values': True, 'fields_limit': settings.DATA_UPLOAD_MAX_NUMBER_FIELDS, 'encoding': self.encoding, } if isinstance(query_string, bytes): # query_string normally contains URL-encoded data, a subset of ASCII. try: query_string = query_string.decode(self.encoding) except UnicodeDecodeError: # ... but some user.txt agents are misbehaving :-( query_string = query_string.decode('iso-8859-1') for key, value in limited_parse_qsl(query_string, **parse_qsl_kwargs): self.appendlist(key, value) self._mutable = mutable
def infer_identifier(url, source): if source == 'MAL': # structure is: protocol://myanimelist.net/(type)/(identifier) return int(url.split('/')[4]) elif source == 'AniDB': _, _, _, query_encoded, _ = _urlsplit(url) query_params = dict(limited_parse_qsl(query_encoded)) return query_params.get('aid', None) elif source == 'Manga-News': # structure is: protocol://www.manga-news.com/index.php/serie/(identifier) return url.split('/')[5] elif source == 'Icotaku': # structure is: protocol://anime.icotaku.com/anime/(identifier)/(name) return int(url.split('/')[4]) elif source == 'Animeka': # structure is # protocol://www.animeka.com/animes/detail/(identifier).html return url.split('/')[5][:-5] elif source == 'VGMdb': # structure is the same as MAL. return int(url.split('/')[4]) else: raise ValueError('Unknown source')
def test_parse_qsl_field_limit(self): with self.assertRaises(TooManyFieldsSent): limited_parse_qsl('&'.join(['a=a'] * 11), fields_limit=10) limited_parse_qsl('&'.join(['a=a'] * 10), fields_limit=10)