summaryrefslogtreecommitdiffstats
path: root/requests/packages/urllib3/filepost.py
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2013-09-29 17:48:14 -0400
committerThibaut Horel <thibaut.horel@gmail.com>2013-09-29 17:48:14 -0400
commitef7a80865f253852f7c7b01f04bc02b695ead67b (patch)
treeea718f746b5f9db4624057d1f0aa1df9abee8bd4 /requests/packages/urllib3/filepost.py
parent19346fa9068878af516cdb670bea4f791337507b (diff)
downloadlastfm-ef7a80865f253852f7c7b01f04bc02b695ead67b.tar.gz
Giving up on Python 2.5 support
Diffstat (limited to 'requests/packages/urllib3/filepost.py')
-rw-r--r--requests/packages/urllib3/filepost.py94
1 files changed, 62 insertions, 32 deletions
diff --git a/requests/packages/urllib3/filepost.py b/requests/packages/urllib3/filepost.py
index 2ffea8b..4575582 100644
--- a/requests/packages/urllib3/filepost.py
+++ b/requests/packages/urllib3/filepost.py
@@ -1,71 +1,101 @@
# urllib3/filepost.py
-# Copyright 2008-2011 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
+# Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
#
# This module is part of urllib3 and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
import codecs
-import mimetools
import mimetypes
-try:
- from cStringIO import StringIO
-except ImportError:
- from StringIO import StringIO # pylint: disable-msg=W0404
+from uuid import uuid4
+from io import BytesIO
+from .packages import six
+from .packages.six import b
+from .fields import RequestField
writer = codecs.lookup('utf-8')[3]
-def get_content_type(filename):
- return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
+def choose_boundary():
+ """
+ Our embarassingly-simple replacement for mimetools.choose_boundary.
+ """
+ return uuid4().hex
+
+
+def iter_field_objects(fields):
+ """
+ Iterate over fields.
+
+ Supports list of (k, v) tuples and dicts, and lists of
+ :class:`~urllib3.fields.RequestField`.
+
+ """
+ if isinstance(fields, dict):
+ i = six.iteritems(fields)
+ else:
+ i = iter(fields)
+
+ for field in i:
+ if isinstance(field, RequestField):
+ yield field
+ else:
+ yield RequestField.from_tuples(*field)
+
+
+def iter_fields(fields):
+ """
+ Iterate over fields.
+
+ .. deprecated ::
+
+ The addition of `~urllib3.fields.RequestField` makes this function
+ obsolete. Instead, use :func:`iter_field_objects`, which returns
+ `~urllib3.fields.RequestField` objects, instead.
+
+ Supports list of (k, v) tuples and dicts.
+
+ """
+ if isinstance(fields, dict):
+ return ((k, v) for k, v in six.iteritems(fields))
+
+ return ((k, v) for k, v in fields)
def encode_multipart_formdata(fields, boundary=None):
"""
- Encode a dictionary of ``fields`` using the multipart/form-data mime format.
+ Encode a dictionary of ``fields`` using the multipart/form-data MIME format.
:param fields:
- Dictionary of fields. The key is treated as the field name, and the
- value as the body of the form-data. If the value is a tuple of two
- elements, then the first element is treated as the filename of the
- form-data section.
+ Dictionary of fields or list of (key, :class:`~urllib3.fields.RequestField`).
:param boundary:
If not specified, then a random boundary will be generated using
:func:`mimetools.choose_boundary`.
"""
- body = StringIO()
+ body = BytesIO()
if boundary is None:
- boundary = mimetools.choose_boundary()
+ boundary = choose_boundary()
- for fieldname, value in fields.iteritems():
- body.write('--%s\r\n' % (boundary))
+ for field in iter_field_objects(fields):
+ body.write(b('--%s\r\n' % (boundary)))
- if isinstance(value, tuple):
- filename, data = value
- writer(body).write('Content-Disposition: form-data; name="%s"; '
- 'filename="%s"\r\n' % (fieldname, filename))
- body.write('Content-Type: %s\r\n\r\n' %
- (get_content_type(filename)))
- else:
- data = value
- writer(body).write('Content-Disposition: form-data; name="%s"\r\n'
- % (fieldname))
- body.write('Content-Type: text/plain\r\n\r\n')
+ writer(body).write(field.render_headers())
+ data = field.data
if isinstance(data, int):
data = str(data) # Backwards compatibility
- if isinstance(data, unicode):
+ if isinstance(data, six.text_type):
writer(body).write(data)
else:
body.write(data)
- body.write('\r\n')
+ body.write(b'\r\n')
- body.write('--%s--\r\n' % (boundary))
+ body.write(b('--%s--\r\n' % (boundary)))
- content_type = 'multipart/form-data; boundary=%s' % boundary
+ content_type = str('multipart/form-data; boundary=%s' % boundary)
return body.getvalue(), content_type