Skip to content
Myr

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

3 files+67 added−3 removed

modified:requests/utils.py+26 added-2 removed
6 unmodified lines
7This module provides utility functions that are used within Requests8that are also useful for external consumption.9"""1011import cgi11import codecs12import collections13import contextlib14import io
430 unmodified lines
445 pragma_re.findall(content) +446 xml_re.findall(content))447448449def _parse_content_type_header(header):450 """Returns content type and parameters from given header451452 :param header: string453 :return: tuple containing content type and dictionary of454 parameters455 """456457 tokens = header.split(';')458 content_type, params = tokens[0].strip(), tokens[1:]459 params_dict = {}460 items_to_strip = "\"' "461462 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_dict472473474def get_encoding_from_headers(headers):475 """Returns encodings from given HTTP Header Dict.476477 :param headers: dictionary to extract encoding from.
4 unmodified lines
482483 if not content_type:484 return None485462 content_type, params = cgi.parse_header(content_type)486 content_type, params = _parse_content_type_header(content_type)487488 if 'charset' in params:489 return params['charset'].strip("'\"")490
482 unmodified lines
modified:tests/test_utils.py+40 added-1 removed
11 unmodified lines
12from requests.cookies import RequestsCookieJar13from requests.structures import CaseInsensitiveDict14from 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 lines
472473@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 ))508def test__parse_content_type_header(value, expected):509 assert _parse_content_type_header(value) == expected510511512@pytest.mark.parametrize(513 'value, expected', (514 (515 CaseInsensitiveDict(),516 None517 ),518 (
238 unmodified lines
modified:AUTHORS.rst+1 added
179 unmodified lines
180- 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>`_)