1"""
2requests._internal_utils
3~~~~~~~~~~~~~~
4
5Provides utility functions that are consumed internally by Requests
6which depend on extremely few external helpers (such as compat)
7"""
8
9import re
10
11from .compat import builtin_str
12
13_VALID_HEADER_NAME_RE_BYTE = re.compile(rb"^[^:\s][^:\r\n]*$")
14_VALID_HEADER_NAME_RE_STR = re.compile(r"^[^:\s][^:\r\n]*$")
15_VALID_HEADER_VALUE_RE_BYTE = re.compile(rb"^\S[^\r\n]*$|^$")
16_VALID_HEADER_VALUE_RE_STR = re.compile(r"^\S[^\r\n]*$|^$")
17
18_HEADER_VALIDATORS_STR = (_VALID_HEADER_NAME_RE_STR, _VALID_HEADER_VALUE_RE_STR)
19_HEADER_VALIDATORS_BYTE = (_VALID_HEADER_NAME_RE_BYTE, _VALID_HEADER_VALUE_RE_BYTE)
20HEADER_VALIDATORS = {
21 bytes: _HEADER_VALIDATORS_BYTE,
22 str: _HEADER_VALIDATORS_STR,
23}
24
25
26def to_native_string(string, encoding="ascii"):
27 """Given a string object, regardless of type, returns a representation of
28 that string in the native string type, encoding and decoding where
29 necessary. This assumes ASCII unless told otherwise.
30 """
31 if isinstance(string, builtin_str):
32 out = string
33 else:
34 out = string.decode(encoding)
35
36 return out
37
38
39def unicode_is_ascii(u_string):
40 """Determine if unicode string only contains ASCII characters.
41
42 :param str u_string: unicode string to check. Must be unicode
43 and not Python 2 `str`.
44 :rtype: bool
45 """
46 assert isinstance(u_string, str)
47 try:
48 u_string.encode("ascii")
49 return True
50 except UnicodeEncodeError:
51 return False