1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00
pip/src/pip/_vendor/urllib3/request.py

171 lines
5.8 KiB
Python
Raw Normal View History

2016-01-19 00:28:54 +01:00
from __future__ import absolute_import
2013-08-15 15:33:47 +02:00
from .filepost import encode_multipart_formdata
from .packages.six.moves.urllib.parse import urlencode
2013-08-15 15:33:47 +02:00
2019-10-09 16:16:37 +02:00
__all__ = ["RequestMethods"]
2013-08-15 15:33:47 +02:00
class RequestMethods(object):
"""
Convenience mixin for classes who implement a :meth:`urlopen` method, such
2020-11-19 15:20:52 +01:00
as :class:`urllib3.HTTPConnectionPool` and
:class:`urllib3.PoolManager`.
2013-08-15 15:33:47 +02:00
Provides behavior for making common types of HTTP request methods and
decides which type of request field encoding to use.
Specifically,
:meth:`.request_encode_url` is for sending requests whose fields are
encoded in the URL (such as GET, HEAD, DELETE).
2013-08-15 15:33:47 +02:00
:meth:`.request_encode_body` is for sending requests whose fields are
encoded in the *body* of the request using multipart or www-form-urlencoded
(such as for POST, PUT, PATCH).
:meth:`.request` is for making any kind of request, it will look up the
appropriate encoding format and use one of the above two methods to make
the request.
Initializer parameters:
:param headers:
Headers to include with all requests, unless other headers are given
explicitly.
"""
2019-10-09 16:16:37 +02:00
_encode_url_methods = {"DELETE", "GET", "HEAD", "OPTIONS"}
2013-08-15 15:33:47 +02:00
def __init__(self, headers=None):
self.headers = headers or {}
2019-10-09 16:16:37 +02:00
def urlopen(
self,
method,
url,
body=None,
headers=None,
encode_multipart=True,
multipart_boundary=None,
**kw
): # Abstract
raise NotImplementedError(
"Classes extending RequestMethods must implement "
"their own ``urlopen`` method."
)
2013-08-15 15:33:47 +02:00
def request(self, method, url, fields=None, headers=None, **urlopen_kw):
"""
Make a request using :meth:`urlopen` with the appropriate encoding of
``fields`` based on the ``method`` used.
This is a convenience method that requires the least amount of manual
effort. It can be used in most situations, while still having the
option to drop down to more specific methods when necessary, such as
2013-08-15 15:33:47 +02:00
:meth:`request_encode_url`, :meth:`request_encode_body`,
or even the lowest level :meth:`urlopen`.
"""
method = method.upper()
2019-10-09 16:16:37 +02:00
urlopen_kw["request_url"] = url
2018-06-21 15:08:05 +02:00
2013-08-15 15:33:47 +02:00
if method in self._encode_url_methods:
2019-10-09 16:16:37 +02:00
return self.request_encode_url(
method, url, fields=fields, headers=headers, **urlopen_kw
)
2013-08-15 15:33:47 +02:00
else:
2019-10-09 16:16:37 +02:00
return self.request_encode_body(
method, url, fields=fields, headers=headers, **urlopen_kw
)
2013-08-15 15:33:47 +02:00
2019-10-09 16:16:37 +02:00
def request_encode_url(self, method, url, fields=None, headers=None, **urlopen_kw):
2013-08-15 15:33:47 +02:00
"""
Make a request using :meth:`urlopen` with the ``fields`` encoded in
the url. This is useful for request methods like GET, HEAD, DELETE, etc.
"""
2016-01-19 00:28:54 +01:00
if headers is None:
headers = self.headers
2019-10-09 16:16:37 +02:00
extra_kw = {"headers": headers}
2016-01-19 00:28:54 +01:00
extra_kw.update(urlopen_kw)
2013-08-15 15:33:47 +02:00
if fields:
2019-10-09 16:16:37 +02:00
url += "?" + urlencode(fields)
2016-01-19 00:28:54 +01:00
return self.urlopen(method, url, **extra_kw)
2013-08-15 15:33:47 +02:00
2019-10-09 16:16:37 +02:00
def request_encode_body(
self,
method,
url,
fields=None,
headers=None,
encode_multipart=True,
multipart_boundary=None,
**urlopen_kw
):
2013-08-15 15:33:47 +02:00
"""
Make a request using :meth:`urlopen` with the ``fields`` encoded in
the body. This is useful for request methods like POST, PUT, PATCH, etc.
When ``encode_multipart=True`` (default), then
2020-11-19 15:20:52 +01:00
:func:`urllib3.encode_multipart_formdata` is used to encode
the payload with the appropriate content type. Otherwise
2020-11-19 15:20:52 +01:00
:func:`urllib.parse.urlencode` is used with the
2013-08-15 15:33:47 +02:00
'application/x-www-form-urlencoded' content type.
Multipart encoding must be used when posting files, and it's reasonably
safe to use it in other times too. However, it may break request
signing, such as with OAuth.
2013-08-15 15:33:47 +02:00
Supports an optional ``fields`` parameter of key/value strings AND
key/filetuple. A filetuple is a (filename, data, MIME type) tuple where
the MIME type is optional. For example::
2013-08-15 15:33:47 +02:00
fields = {
'foo': 'bar',
'fakefile': ('foofile.txt', 'contents of foofile'),
'realfile': ('barfile.txt', open('realfile').read()),
'typedfile': ('bazfile.bin', open('bazfile').read(),
'image/jpeg'),
'nonamefile': 'contents of nonamefile field',
}
When uploading a file, providing a filename (the first parameter of the
2018-06-21 15:08:05 +02:00
tuple) is optional but recommended to best mimic behavior of browsers.
2013-08-15 15:33:47 +02:00
Note that if ``headers`` are supplied, the 'Content-Type' header will
be overwritten because it depends on the dynamic random boundary string
2013-08-15 15:33:47 +02:00
which is used to compose the body of the request. The random boundary
string can be explicitly set with the ``multipart_boundary`` parameter.
"""
if headers is None:
headers = self.headers
2019-10-09 16:16:37 +02:00
extra_kw = {"headers": {}}
2014-12-02 00:48:38 +01:00
if fields:
2019-10-09 16:16:37 +02:00
if "body" in urlopen_kw:
2016-01-19 00:28:54 +01:00
raise TypeError(
2019-10-09 16:16:37 +02:00
"request got values for both 'fields' and 'body', can only specify one."
)
2014-12-02 00:48:38 +01:00
if encode_multipart:
2019-10-09 16:16:37 +02:00
body, content_type = encode_multipart_formdata(
fields, boundary=multipart_boundary
)
2014-12-02 00:48:38 +01:00
else:
2019-10-09 16:16:37 +02:00
body, content_type = (
urlencode(fields),
"application/x-www-form-urlencoded",
)
2014-12-02 00:48:38 +01:00
2019-10-09 16:16:37 +02:00
extra_kw["body"] = body
extra_kw["headers"] = {"Content-Type": content_type}
2014-12-02 00:48:38 +01:00
2019-10-09 16:16:37 +02:00
extra_kw["headers"].update(headers)
2014-12-02 00:48:38 +01:00
extra_kw.update(urlopen_kw)
2013-08-15 15:33:47 +02:00
2014-12-02 00:48:38 +01:00
return self.urlopen(method, url, **extra_kw)