MCPcopy Create free account
hub / github.com/EasyIME/PIME / _parse_header

Function _parse_header

python/python3/tornado/httputil.py:948–980  ·  view source on GitHub ↗

r"""Parse a Content-type like header. Return the main content-type and a dictionary of options. >>> d = "form-data; foo=\"b\\\\a\\\"r\"; file*=utf-8''T%C3%A4st" >>> ct, d = _parse_header(d) >>> ct 'form-data' >>> d['file'] == r'T\u00e4st'.encode('ascii').decode('unicode_esc

(line: str)

Source from the content-addressed store, hash-verified

946
947
948def _parse_header(line: str) -> Tuple[str, Dict[str, str]]:
949 r"""Parse a Content-type like header.
950
951 Return the main content-type and a dictionary of options.
952
953 >>> d = "form-data; foo=\"b\\\\a\\\"r\"; file*=utf-8''T%C3%A4st"
954 >>> ct, d = _parse_header(d)
955 >>> ct
956 'form-data'
957 >>> d['file'] == r'T\u00e4st'.encode('ascii').decode('unicode_escape')
958 True
959 >>> d['foo']
960 'b\\a"r'
961 """
962 parts = _parseparam(";" + line)
963 key = next(parts)
964 # decode_params treats first argument special, but we already stripped key
965 params = [("Dummy", "value")]
966 for p in parts:
967 i = p.find("=")
968 if i >= 0:
969 name = p[:i].strip().lower()
970 value = p[i + 1 :].strip()
971 params.append((name, native_str(value)))
972 decoded_params = email.utils.decode_params(params)
973 decoded_params.pop(0) # get rid of the dummy again
974 pdict = {}
975 for name, decoded_value in decoded_params:
976 value = email.utils.collapse_rfc2231_value(decoded_value)
977 if len(value) >= 2 and value[0] == '"' and value[-1] == '"':
978 value = value[1:-1]
979 pdict[name] = value
980 return key, pdict
981
982
983def _encode_header(key: str, pdict: Dict[str, str]) -> str:

Callers 1

Calls 4

_parseparamFunction · 0.85
nextFunction · 0.50
findMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected