1from __future__
import absolute_import
7from .packages
import six
12 Guess the "Content-Type" of a file.
15 The filename to guess the "Content-Type" of using :mod:`mimetypes`.
17 If no "Content-Type" can be guessed, default to `default`.
26 Helper function to format and quote a single header parameter using the
27 strategy defined in RFC 2231.
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>`_.
34 The name of the parameter, a string expected to be ASCII only.
36 The value of the parameter, provided as ``bytes`` or `str``.
38 An RFC-2231-formatted unicode string.
43 if not any(ch
in value
for ch
in '"\\\r\n'):
44 result =
u'%s="%s"' % (name, value)
47 except (UnicodeEncodeError, UnicodeDecodeError):
58 value =
"%s*=%s" % (name, value)
66_HTML5_REPLACEMENTS = {
69 u"\u005C":
u"\u005C\u005C",
76 for cc
in range(0x00, 0x1F + 1)
97 Helper function to format and quote a single header parameter using the
100 Particularly useful for header parameters which might contain
101 non-ASCII values, like file names. This follows the `HTML5 Working Draft
102 Section 4.10.22.7`_ and matches the behavior of curl and modern browsers.
104 .. _HTML5 Working Draft Section 4.10.22.7:
105 https://w3c.github.io/html/sec-forms.html#multipart-form-data
108 The name of the parameter, a string expected to be ASCII only.
110 The value of the parameter, provided as ``bytes`` or `str``.
112 A unicode string, stripped of troublesome characters.
119 return u'%s="%s"' % (name, value)
123format_header_param = format_header_param_html5
126class RequestField(object):
128 A data container for request body parameters.
131 The name of this request field. Must be unicode.
135 An optional filename of the request field. Must be unicode.
137 An optional dict-like object of headers to initially use for the field.
138 :param header_formatter:
139 An optional callable that is used to encode and format the headers. By
140 default, this is :func:`format_header_param_html5`.
149 header_formatter=format_header_param_html5,
152 self._filename = filename
156 self.headers = dict(headers)
157 self.header_formatter = header_formatter
160 def from_tuples(cls, fieldname, value, header_formatter=format_header_param_html5):
162 A :class:`~urllib3.fields.RequestField` factory from old-style tuple parameters.
164 Supports constructing :class:`~urllib3.fields.RequestField` from
165 parameter of key/value strings AND key/filetuple. A filetuple is a
166 (filename, data, MIME type) tuple where the MIME type is optional.
170 'fakefile': ('foofile.txt', 'contents of foofile'),
171 'realfile': ('barfile.txt', open('realfile').read()),
172 'typedfile': ('bazfile.bin', open('bazfile').read(), 'image/jpeg'),
173 'nonamefile': 'contents of nonamefile field',
175 Field names and filenames must be unicode.
179 filename, data, content_type = value
181 filename, data = value
189 fieldname, data, filename=filename, header_formatter=header_formatter
197 Overridable helper function to format a single header parameter. By
198 default, this calls ``self.header_formatter``.
201 The name of the parameter, a string expected to be ASCII only.
203 The value of the parameter, provided as a unicode string.
206 return self.header_formatter(name, value)
210 Helper function to format and quote a single header.
212 Useful for single headers that are composed of multiple items. E.g.,
213 'Content-Disposition' fields.
216 A sequence of (k, v) tuples or a :class:`dict` of (k, v) to format
217 as `k1="v1"; k2="v2"; ...`.
220 iterable = header_parts
224 for name, value
in iterable:
225 if value
is not None:
228 return u"; ".join(parts)
232 Renders the headers for this request field.
236 sort_keys = [
"Content-Disposition",
"Content-Type",
"Content-Location"]
237 for sort_key
in sort_keys:
238 if self.headers.get(sort_key,
False):
239 lines.append(
u"%s: %s" % (sort_key, self.headers[sort_key]))
241 for header_name, header_value
in self.headers.items():
242 if header_name
not in sort_keys:
247 return u"\r\n".join(lines)
250 self, content_disposition=None, content_type=None, content_location=None
253 Makes this request field into a multipart request field.
255 This method overrides "Content-Disposition", "Content-Type" and
256 "Content-Location" headers to the request parameter.
259 The 'Content-Type' of the request body.
260 :param content_location:
261 The 'Content-Location' of the request body.
264 self.headers[
"Content-Disposition"] = content_disposition
or u"form-data"
265 self.headers[
"Content-Disposition"] +=
u"; ".join(
269 ((
u"name", self._name), (
u"filename", self._filename))
273 self.headers[
"Content-Type"] = content_type
274 self.headers[
"Content-Location"] = content_location
format_header_param_rfc2231(name, value)
guess_content_type(filename, default="application/octet-stream")