diff options
author | Shubham Saini <shubham6405@gmail.com> | 2018-12-11 10:01:23 +0000 |
---|---|---|
committer | Shubham Saini <shubham6405@gmail.com> | 2018-12-11 10:01:23 +0000 |
commit | 68df54d6629ec019142eb149dd037774f2d11e7c (patch) | |
tree | 345bc22d46b4e01a4ba8303b94278952a4ed2b9e /venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/urllib3/util/request.py |
First commit
Diffstat (limited to 'venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/urllib3/util/request.py')
-rw-r--r-- | venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/urllib3/util/request.py | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/urllib3/util/request.py b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/urllib3/util/request.py new file mode 100644 index 0000000..22882b8 --- /dev/null +++ b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/urllib3/util/request.py | |||
@@ -0,0 +1,118 @@ | |||
1 | from __future__ import absolute_import | ||
2 | from base64 import b64encode | ||
3 | |||
4 | from ..packages.six import b, integer_types | ||
5 | from ..exceptions import UnrewindableBodyError | ||
6 | |||
7 | ACCEPT_ENCODING = 'gzip,deflate' | ||
8 | _FAILEDTELL = object() | ||
9 | |||
10 | |||
11 | def make_headers(keep_alive=None, accept_encoding=None, user_agent=None, | ||
12 | basic_auth=None, proxy_basic_auth=None, disable_cache=None): | ||
13 | """ | ||
14 | Shortcuts for generating request headers. | ||
15 | |||
16 | :param keep_alive: | ||
17 | If ``True``, adds 'connection: keep-alive' header. | ||
18 | |||
19 | :param accept_encoding: | ||
20 | Can be a boolean, list, or string. | ||
21 | ``True`` translates to 'gzip,deflate'. | ||
22 | List will get joined by comma. | ||
23 | String will be used as provided. | ||
24 | |||
25 | :param user_agent: | ||
26 | String representing the user-agent you want, such as | ||
27 | "python-urllib3/0.6" | ||
28 | |||
29 | :param basic_auth: | ||
30 | Colon-separated username:password string for 'authorization: basic ...' | ||
31 | auth header. | ||
32 | |||
33 | :param proxy_basic_auth: | ||
34 | Colon-separated username:password string for 'proxy-authorization: basic ...' | ||
35 | auth header. | ||
36 | |||
37 | :param disable_cache: | ||
38 | If ``True``, adds 'cache-control: no-cache' header. | ||
39 | |||
40 | Example:: | ||
41 | |||
42 | >>> make_headers(keep_alive=True, user_agent="Batman/1.0") | ||
43 | {'connection': 'keep-alive', 'user-agent': 'Batman/1.0'} | ||
44 | >>> make_headers(accept_encoding=True) | ||
45 | {'accept-encoding': 'gzip,deflate'} | ||
46 | """ | ||
47 | headers = {} | ||
48 | if accept_encoding: | ||
49 | if isinstance(accept_encoding, str): | ||
50 | pass | ||
51 | elif isinstance(accept_encoding, list): | ||
52 | accept_encoding = ','.join(accept_encoding) | ||
53 | else: | ||
54 | accept_encoding = ACCEPT_ENCODING | ||
55 | headers['accept-encoding'] = accept_encoding | ||
56 | |||
57 | if user_agent: | ||
58 | headers['user-agent'] = user_agent | ||
59 | |||
60 | if keep_alive: | ||
61 | headers['connection'] = 'keep-alive' | ||
62 | |||
63 | if basic_auth: | ||
64 | headers['authorization'] = 'Basic ' + \ | ||
65 | b64encode(b(basic_auth)).decode('utf-8') | ||
66 | |||
67 | if proxy_basic_auth: | ||
68 | headers['proxy-authorization'] = 'Basic ' + \ | ||
69 | b64encode(b(proxy_basic_auth)).decode('utf-8') | ||
70 | |||
71 | if disable_cache: | ||
72 | headers['cache-control'] = 'no-cache' | ||
73 | |||
74 | return headers | ||
75 | |||
76 | |||
77 | def set_file_position(body, pos): | ||
78 | """ | ||
79 | If a position is provided, move file to that point. | ||
80 | Otherwise, we'll attempt to record a position for future use. | ||
81 | """ | ||
82 | if pos is not None: | ||
83 | rewind_body(body, pos) | ||
84 | elif getattr(body, 'tell', None) is not None: | ||
85 | try: | ||
86 | pos = body.tell() | ||
87 | except (IOError, OSError): | ||
88 | # This differentiates from None, allowing us to catch | ||
89 | # a failed `tell()` later when trying to rewind the body. | ||
90 | pos = _FAILEDTELL | ||
91 | |||
92 | return pos | ||
93 | |||
94 | |||
95 | def rewind_body(body, body_pos): | ||
96 | """ | ||
97 | Attempt to rewind body to a certain position. | ||
98 | Primarily used for request redirects and retries. | ||
99 | |||
100 | :param body: | ||
101 | File-like object that supports seek. | ||
102 | |||
103 | :param int pos: | ||
104 | Position to seek to in file. | ||
105 | """ | ||
106 | body_seek = getattr(body, 'seek', None) | ||
107 | if body_seek is not None and isinstance(body_pos, integer_types): | ||
108 | try: | ||
109 | body_seek(body_pos) | ||
110 | except (IOError, OSError): | ||
111 | raise UnrewindableBodyError("An error occurred when rewinding request " | ||
112 | "body for redirect/retry.") | ||
113 | elif body_pos is _FAILEDTELL: | ||
114 | raise UnrewindableBodyError("Unable to record file position for rewinding " | ||
115 | "request body during a redirect/retry.") | ||
116 | else: | ||
117 | raise ValueError("body_pos must be of type integer, " | ||
118 | "instead it was %s." % type(body_pos)) | ||