Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: swarm_client/third_party/requests/packages/urllib3/filepost.py

Issue 69143004: Delete swarm_client. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 # urllib3/filepost.py
2 # Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
3 #
4 # This module is part of urllib3 and is released under
5 # the MIT License: http://www.opensource.org/licenses/mit-license.php
6
7 import codecs
8 import mimetypes
9
10 from uuid import uuid4
11 from io import BytesIO
12
13 from .packages import six
14 from .packages.six import b
15 from .fields import RequestField
16
17 writer = codecs.lookup('utf-8')[3]
18
19
20 def choose_boundary():
21 """
22 Our embarassingly-simple replacement for mimetools.choose_boundary.
23 """
24 return uuid4().hex
25
26
27 def iter_field_objects(fields):
28 """
29 Iterate over fields.
30
31 Supports list of (k, v) tuples and dicts, and lists of
32 :class:`~urllib3.fields.RequestField`.
33
34 """
35 if isinstance(fields, dict):
36 i = six.iteritems(fields)
37 else:
38 i = iter(fields)
39
40 for field in i:
41 if isinstance(field, RequestField):
42 yield field
43 else:
44 yield RequestField.from_tuples(*field)
45
46
47 def iter_fields(fields):
48 """
49 Iterate over fields.
50
51 .. deprecated ::
52
53 The addition of `~urllib3.fields.RequestField` makes this function
54 obsolete. Instead, use :func:`iter_field_objects`, which returns
55 `~urllib3.fields.RequestField` objects, instead.
56
57 Supports list of (k, v) tuples and dicts.
58
59 """
60 if isinstance(fields, dict):
61 return ((k, v) for k, v in six.iteritems(fields))
62
63 return ((k, v) for k, v in fields)
64
65
66 def encode_multipart_formdata(fields, boundary=None):
67 """
68 Encode a dictionary of ``fields`` using the multipart/form-data MIME format.
69
70 :param fields:
71 Dictionary of fields or list of (key, :class:`~urllib3.fields.RequestFie ld`).
72
73 :param boundary:
74 If not specified, then a random boundary will be generated using
75 :func:`mimetools.choose_boundary`.
76 """
77 body = BytesIO()
78 if boundary is None:
79 boundary = choose_boundary()
80
81 for field in iter_field_objects(fields):
82 body.write(b('--%s\r\n' % (boundary)))
83
84 writer(body).write(field.render_headers())
85 data = field.data
86
87 if isinstance(data, int):
88 data = str(data) # Backwards compatibility
89
90 if isinstance(data, six.text_type):
91 writer(body).write(data)
92 else:
93 body.write(data)
94
95 body.write(b'\r\n')
96
97 body.write(b('--%s--\r\n' % (boundary)))
98
99 content_type = str('multipart/form-data; boundary=%s' % boundary)
100
101 return body.getvalue(), content_type
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698