Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
pip._vendor.urllib3.fields Namespace Reference

Functions

 guess_content_type (filename, default="application/octet-stream")
 
 format_header_param_rfc2231 (name, value)
 

Function Documentation

◆ format_header_param_rfc2231()

format_header_param_rfc2231 (   name,
  value 
)
Helper function to format and quote a single header parameter using the
strategy defined in RFC 2231.

Particularly useful for header parameters which might contain
non-ASCII values, like file names. This follows
`RFC 2388 Section 4.4 <https://tools.ietf.org/html/rfc2388#section-4.4>`_.

:param name:
    The name of the parameter, a string expected to be ASCII only.
:param value:
    The value of the parameter, provided as ``bytes`` or `str``.
:ret:
    An RFC-2231-formatted unicode string.

Definition at line 24 of file fields.py.

24def format_header_param_rfc2231(name, value):
25 """
26 Helper function to format and quote a single header parameter using the
27 strategy defined in RFC 2231.
28
29 Particularly useful for header parameters which might contain
30 non-ASCII values, like file names. This follows
31 `RFC 2388 Section 4.4 <https://tools.ietf.org/html/rfc2388#section-4.4>`_.
32
33 :param name:
34 The name of the parameter, a string expected to be ASCII only.
35 :param value:
36 The value of the parameter, provided as ``bytes`` or `str``.
37 :ret:
38 An RFC-2231-formatted unicode string.
39 """
40 if isinstance(value, six.binary_type):
41 value = value.decode("utf-8")
42
43 if not any(ch in value for ch in '"\\\r\n'):
44 result = u'%s="%s"' % (name, value)
45 try:
46 result.encode("ascii")
47 except (UnicodeEncodeError, UnicodeDecodeError):
48 pass
49 else:
50 return result
51
52 if six.PY2: # Python 2:
53 value = value.encode("utf-8")
54
55 # encode_rfc2231 accepts an encoded string and returns an ascii-encoded
56 # string in Python 2 but accepts and returns unicode strings in Python 3
57 value = email.utils.encode_rfc2231(value, "utf-8")
58 value = "%s*=%s" % (name, value)
59
60 if six.PY2: # Python 2:
61 value = value.decode("utf-8")
62
63 return value
64
65
for i

References pip._vendor.urllib3.fields.guess_content_type(), and i.

Here is the call graph for this function:

◆ guess_content_type()

guess_content_type (   filename,
  default = "application/octet-stream" 
)
Guess the "Content-Type" of a file.

:param filename:
    The filename to guess the "Content-Type" of using :mod:`mimetypes`.
:param default:
    If no "Content-Type" can be guessed, default to `default`.

Definition at line 10 of file fields.py.

10def guess_content_type(filename, default="application/octet-stream"):
11 """
12 Guess the "Content-Type" of a file.
13
14 :param filename:
15 The filename to guess the "Content-Type" of using :mod:`mimetypes`.
16 :param default:
17 If no "Content-Type" can be guessed, default to `default`.
18 """
19 if filename:
20 return mimetypes.guess_type(filename)[0] or default
21 return default
22
23

References i.

Referenced by pip._vendor.urllib3.fields.format_header_param_rfc2231().

Here is the caller graph for this function: