comparison env/lib/python3.7/site-packages/urllib3/fields.py @ 5:9b1c78e6ba9c draft default tip

"planemo upload commit 6c0a8142489327ece472c84e558c47da711a9142"
author shellac
date Mon, 01 Jun 2020 08:59:25 -0400
parents 79f47841a781
children
comparison
equal deleted inserted replaced
4:79f47841a781 5:9b1c78e6ba9c
1 from __future__ import absolute_import
2 import email.utils
3 import mimetypes
4 import re
5
6 from .packages import six
7
8
9 def guess_content_type(filename, default="application/octet-stream"):
10 """
11 Guess the "Content-Type" of a file.
12
13 :param filename:
14 The filename to guess the "Content-Type" of using :mod:`mimetypes`.
15 :param default:
16 If no "Content-Type" can be guessed, default to `default`.
17 """
18 if filename:
19 return mimetypes.guess_type(filename)[0] or default
20 return default
21
22
23 def format_header_param_rfc2231(name, value):
24 """
25 Helper function to format and quote a single header parameter using the
26 strategy defined in RFC 2231.
27
28 Particularly useful for header parameters which might contain
29 non-ASCII values, like file names. This follows RFC 2388 Section 4.4.
30
31 :param name:
32 The name of the parameter, a string expected to be ASCII only.
33 :param value:
34 The value of the parameter, provided as ``bytes`` or `str``.
35 :ret:
36 An RFC-2231-formatted unicode string.
37 """
38 if isinstance(value, six.binary_type):
39 value = value.decode("utf-8")
40
41 if not any(ch in value for ch in '"\\\r\n'):
42 result = u'%s="%s"' % (name, value)
43 try:
44 result.encode("ascii")
45 except (UnicodeEncodeError, UnicodeDecodeError):
46 pass
47 else:
48 return result
49
50 if six.PY2: # Python 2:
51 value = value.encode("utf-8")
52
53 # encode_rfc2231 accepts an encoded string and returns an ascii-encoded
54 # string in Python 2 but accepts and returns unicode strings in Python 3
55 value = email.utils.encode_rfc2231(value, "utf-8")
56 value = "%s*=%s" % (name, value)
57
58 if six.PY2: # Python 2:
59 value = value.decode("utf-8")
60
61 return value
62
63
64 _HTML5_REPLACEMENTS = {
65 u"\u0022": u"%22",
66 # Replace "\" with "\\".
67 u"\u005C": u"\u005C\u005C",
68 u"\u005C": u"\u005C\u005C",
69 }
70
71 # All control characters from 0x00 to 0x1F *except* 0x1B.
72 _HTML5_REPLACEMENTS.update(
73 {
74 six.unichr(cc): u"%{:02X}".format(cc)
75 for cc in range(0x00, 0x1F + 1)
76 if cc not in (0x1B,)
77 }
78 )
79
80
81 def _replace_multiple(value, needles_and_replacements):
82 def replacer(match):
83 return needles_and_replacements[match.group(0)]
84
85 pattern = re.compile(
86 r"|".join([re.escape(needle) for needle in needles_and_replacements.keys()])
87 )
88
89 result = pattern.sub(replacer, value)
90
91 return result
92
93
94 def format_header_param_html5(name, value):
95 """
96 Helper function to format and quote a single header parameter using the
97 HTML5 strategy.
98
99 Particularly useful for header parameters which might contain
100 non-ASCII values, like file names. This follows the `HTML5 Working Draft
101 Section 4.10.22.7`_ and matches the behavior of curl and modern browsers.
102
103 .. _HTML5 Working Draft Section 4.10.22.7:
104 https://w3c.github.io/html/sec-forms.html#multipart-form-data
105
106 :param name:
107 The name of the parameter, a string expected to be ASCII only.
108 :param value:
109 The value of the parameter, provided as ``bytes`` or `str``.
110 :ret:
111 A unicode string, stripped of troublesome characters.
112 """
113 if isinstance(value, six.binary_type):
114 value = value.decode("utf-8")
115
116 value = _replace_multiple(value, _HTML5_REPLACEMENTS)
117
118 return u'%s="%s"' % (name, value)
119
120
121 # For backwards-compatibility.
122 format_header_param = format_header_param_html5
123
124
125 class RequestField(object):
126 """
127 A data container for request body parameters.
128
129 :param name:
130 The name of this request field. Must be unicode.
131 :param data:
132 The data/value body.
133 :param filename:
134 An optional filename of the request field. Must be unicode.
135 :param headers:
136 An optional dict-like object of headers to initially use for the field.
137 :param header_formatter:
138 An optional callable that is used to encode and format the headers. By
139 default, this is :func:`format_header_param_html5`.
140 """
141
142 def __init__(
143 self,
144 name,
145 data,
146 filename=None,
147 headers=None,
148 header_formatter=format_header_param_html5,
149 ):
150 self._name = name
151 self._filename = filename
152 self.data = data
153 self.headers = {}
154 if headers:
155 self.headers = dict(headers)
156 self.header_formatter = header_formatter
157
158 @classmethod
159 def from_tuples(cls, fieldname, value, header_formatter=format_header_param_html5):
160 """
161 A :class:`~urllib3.fields.RequestField` factory from old-style tuple parameters.
162
163 Supports constructing :class:`~urllib3.fields.RequestField` from
164 parameter of key/value strings AND key/filetuple. A filetuple is a
165 (filename, data, MIME type) tuple where the MIME type is optional.
166 For example::
167
168 'foo': 'bar',
169 'fakefile': ('foofile.txt', 'contents of foofile'),
170 'realfile': ('barfile.txt', open('realfile').read()),
171 'typedfile': ('bazfile.bin', open('bazfile').read(), 'image/jpeg'),
172 'nonamefile': 'contents of nonamefile field',
173
174 Field names and filenames must be unicode.
175 """
176 if isinstance(value, tuple):
177 if len(value) == 3:
178 filename, data, content_type = value
179 else:
180 filename, data = value
181 content_type = guess_content_type(filename)
182 else:
183 filename = None
184 content_type = None
185 data = value
186
187 request_param = cls(
188 fieldname, data, filename=filename, header_formatter=header_formatter
189 )
190 request_param.make_multipart(content_type=content_type)
191
192 return request_param
193
194 def _render_part(self, name, value):
195 """
196 Overridable helper function to format a single header parameter. By
197 default, this calls ``self.header_formatter``.
198
199 :param name:
200 The name of the parameter, a string expected to be ASCII only.
201 :param value:
202 The value of the parameter, provided as a unicode string.
203 """
204
205 return self.header_formatter(name, value)
206
207 def _render_parts(self, header_parts):
208 """
209 Helper function to format and quote a single header.
210
211 Useful for single headers that are composed of multiple items. E.g.,
212 'Content-Disposition' fields.
213
214 :param header_parts:
215 A sequence of (k, v) tuples or a :class:`dict` of (k, v) to format
216 as `k1="v1"; k2="v2"; ...`.
217 """
218 parts = []
219 iterable = header_parts
220 if isinstance(header_parts, dict):
221 iterable = header_parts.items()
222
223 for name, value in iterable:
224 if value is not None:
225 parts.append(self._render_part(name, value))
226
227 return u"; ".join(parts)
228
229 def render_headers(self):
230 """
231 Renders the headers for this request field.
232 """
233 lines = []
234
235 sort_keys = ["Content-Disposition", "Content-Type", "Content-Location"]
236 for sort_key in sort_keys:
237 if self.headers.get(sort_key, False):
238 lines.append(u"%s: %s" % (sort_key, self.headers[sort_key]))
239
240 for header_name, header_value in self.headers.items():
241 if header_name not in sort_keys:
242 if header_value:
243 lines.append(u"%s: %s" % (header_name, header_value))
244
245 lines.append(u"\r\n")
246 return u"\r\n".join(lines)
247
248 def make_multipart(
249 self, content_disposition=None, content_type=None, content_location=None
250 ):
251 """
252 Makes this request field into a multipart request field.
253
254 This method overrides "Content-Disposition", "Content-Type" and
255 "Content-Location" headers to the request parameter.
256
257 :param content_type:
258 The 'Content-Type' of the request body.
259 :param content_location:
260 The 'Content-Location' of the request body.
261
262 """
263 self.headers["Content-Disposition"] = content_disposition or u"form-data"
264 self.headers["Content-Disposition"] += u"; ".join(
265 [
266 u"",
267 self._render_parts(
268 ((u"name", self._name), (u"filename", self._filename))
269 ),
270 ]
271 )
272 self.headers["Content-Type"] = content_type
273 self.headers["Content-Location"] = content_location