Merge pull request #4442 from DemetriosBairaktaris/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Reduce overall memory usage by removing cgi dependency and writing a protected function
authored by Ian Stapleton Cordasco · committed by GitHub · parents 24092b1, e0ab287 · Browse files · 3 files·+67 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+67 added−3 removed
- tests:+40 added−1 removed
- other:+27 added−2 removed
Provenance
- Commit
3dc84cd- Tree
b8f8515- Author
Ian Stapleton Cordasco <graffatcolmingov@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
24092b1- Parent 2
e0ab287
3 files changed+67 added−3 removed
modified:requests/utils.py+26 added-2 removed
6 unmodified lines7 This module provides utility functions that are used within Requests8 that are also useful for external consumption.9 """10 11-import cgi11 import codecs12 import collections13 import contextlib14 import io430 unmodified lines445 pragma_re.findall(content) +446 xml_re.findall(content))447 448 449+def _parse_content_type_header(header):450+ """Returns content type and parameters from given header451+452+ :param header: string453+ :return: tuple containing content type and dictionary of454+ parameters455+ """456+457+ tokens = header.split(';')458+ content_type, params = tokens[0].strip(), tokens[1:]459+ params_dict = {}460+ items_to_strip = "\"' "461+462+ for param in params:463+ param = param.strip()464+ if param:465+ key, value = param, True466+ index_of_equals = param.find("=")467+ if index_of_equals != -1:468+ key = param[:index_of_equals].strip(items_to_strip)469+ value = param[index_of_equals + 1:].strip(items_to_strip)470+ params_dict[key] = value471+ return content_type, params_dict472+473+474 def get_encoding_from_headers(headers):475 """Returns encodings from given HTTP Header Dict.476 477 :param headers: dictionary to extract encoding from.4 unmodified lines482 483 if not content_type:484 return None485 462- content_type, params = cgi.parse_header(content_type)486+ content_type, params = _parse_content_type_header(content_type)487 488 if 'charset' in params:489 return params['charset'].strip("'\"")490 482 unmodified linesmodified:tests/test_utils.py+40 added-1 removed
11 unmodified lines12 from requests.cookies import RequestsCookieJar13 from requests.structures import CaseInsensitiveDict14 from requests.utils import (15 address_in_network, dotted_netmask, extract_zipped_paths,16- get_auth_from_url, get_encoding_from_headers,16+ get_auth_from_url, _parse_content_type_header, get_encoding_from_headers,17 get_encodings_from_content, get_environ_proxies,18 guess_filename, guess_json_utf, is_ipv4_address,19 is_valid_cidr, iter_slices, parse_dict_header,20 parse_header_links, prepend_scheme_if_needed,451 unmodified lines472 473 @pytest.mark.parametrize(474 'value, expected', (475 (476+ 'application/xml',477+ ('application/xml', {})478+ ),479+ (480+ 'application/json ; charset=utf-8',481+ ('application/json', {'charset': 'utf-8'})482+ ),483+ (484+ 'text/plain',485+ ('text/plain', {})486+ ),487+ (488+ 'multipart/form-data; boundary = something ; boundary2=\'something_else\' ; no_equals ',489+ ('multipart/form-data', {'boundary': 'something', 'boundary2': 'something_else', 'no_equals': True})490+ ),491+ (492+ 'multipart/form-data; boundary = something ; boundary2="something_else" ; no_equals ',493+ ('multipart/form-data', {'boundary': 'something', 'boundary2': 'something_else', 'no_equals': True})494+ ),495+ (496+ 'multipart/form-data; boundary = something ; \'boundary2=something_else\' ; no_equals ',497+ ('multipart/form-data', {'boundary': 'something', 'boundary2': 'something_else', 'no_equals': True})498+ ),499+ (500+ 'multipart/form-data; boundary = something ; "boundary2=something_else" ; no_equals ',501+ ('multipart/form-data', {'boundary': 'something', 'boundary2': 'something_else', 'no_equals': True})502+ ),503+ (504+ 'application/json ; ; ',505+ ('application/json', {})506+ )507+ ))508+def test__parse_content_type_header(value, expected):509+ assert _parse_content_type_header(value) == expected510+511+512+@pytest.mark.parametrize(513+ 'value, expected', (514+ (515 CaseInsensitiveDict(),516 None517 ),518 (238 unmodified linesmodified:AUTHORS.rst+1 added
179 unmodified lines180 - Matt Liu <liumatt@gmail.com> (`@mlcrazy <https://github.com/mlcrazy>`_)181 - Taylor Hoff <primdevs@protonmail.com> (`@PrimordialHelios <https://github.com/PrimordialHelios>`_)182 - Arthur Vigil (`@ahvigil <https://github.com/ahvigil>`_)183 - Nehal J Wani (`@nehaljwani <https://github.com/nehaljwani>`_)184+- Demetrios Bairaktaris (`@DemetriosBairaktaris <https://github.com/demetriosbairaktaris>`_)