Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/urllib3/util/request.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:32:28 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:d30785e31577 | 1:56ad4e20f292 |
---|---|
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 try: | |
9 import brotli as _unused_module_brotli # noqa: F401 | |
10 except ImportError: | |
11 pass | |
12 else: | |
13 ACCEPT_ENCODING += ",br" | |
14 | |
15 _FAILEDTELL = object() | |
16 | |
17 | |
18 def make_headers( | |
19 keep_alive=None, | |
20 accept_encoding=None, | |
21 user_agent=None, | |
22 basic_auth=None, | |
23 proxy_basic_auth=None, | |
24 disable_cache=None, | |
25 ): | |
26 """ | |
27 Shortcuts for generating request headers. | |
28 | |
29 :param keep_alive: | |
30 If ``True``, adds 'connection: keep-alive' header. | |
31 | |
32 :param accept_encoding: | |
33 Can be a boolean, list, or string. | |
34 ``True`` translates to 'gzip,deflate'. | |
35 List will get joined by comma. | |
36 String will be used as provided. | |
37 | |
38 :param user_agent: | |
39 String representing the user-agent you want, such as | |
40 "python-urllib3/0.6" | |
41 | |
42 :param basic_auth: | |
43 Colon-separated username:password string for 'authorization: basic ...' | |
44 auth header. | |
45 | |
46 :param proxy_basic_auth: | |
47 Colon-separated username:password string for 'proxy-authorization: basic ...' | |
48 auth header. | |
49 | |
50 :param disable_cache: | |
51 If ``True``, adds 'cache-control: no-cache' header. | |
52 | |
53 Example:: | |
54 | |
55 >>> make_headers(keep_alive=True, user_agent="Batman/1.0") | |
56 {'connection': 'keep-alive', 'user-agent': 'Batman/1.0'} | |
57 >>> make_headers(accept_encoding=True) | |
58 {'accept-encoding': 'gzip,deflate'} | |
59 """ | |
60 headers = {} | |
61 if accept_encoding: | |
62 if isinstance(accept_encoding, str): | |
63 pass | |
64 elif isinstance(accept_encoding, list): | |
65 accept_encoding = ",".join(accept_encoding) | |
66 else: | |
67 accept_encoding = ACCEPT_ENCODING | |
68 headers["accept-encoding"] = accept_encoding | |
69 | |
70 if user_agent: | |
71 headers["user-agent"] = user_agent | |
72 | |
73 if keep_alive: | |
74 headers["connection"] = "keep-alive" | |
75 | |
76 if basic_auth: | |
77 headers["authorization"] = "Basic " + b64encode(b(basic_auth)).decode("utf-8") | |
78 | |
79 if proxy_basic_auth: | |
80 headers["proxy-authorization"] = "Basic " + b64encode( | |
81 b(proxy_basic_auth) | |
82 ).decode("utf-8") | |
83 | |
84 if disable_cache: | |
85 headers["cache-control"] = "no-cache" | |
86 | |
87 return headers | |
88 | |
89 | |
90 def set_file_position(body, pos): | |
91 """ | |
92 If a position is provided, move file to that point. | |
93 Otherwise, we'll attempt to record a position for future use. | |
94 """ | |
95 if pos is not None: | |
96 rewind_body(body, pos) | |
97 elif getattr(body, "tell", None) is not None: | |
98 try: | |
99 pos = body.tell() | |
100 except (IOError, OSError): | |
101 # This differentiates from None, allowing us to catch | |
102 # a failed `tell()` later when trying to rewind the body. | |
103 pos = _FAILEDTELL | |
104 | |
105 return pos | |
106 | |
107 | |
108 def rewind_body(body, body_pos): | |
109 """ | |
110 Attempt to rewind body to a certain position. | |
111 Primarily used for request redirects and retries. | |
112 | |
113 :param body: | |
114 File-like object that supports seek. | |
115 | |
116 :param int pos: | |
117 Position to seek to in file. | |
118 """ | |
119 body_seek = getattr(body, "seek", None) | |
120 if body_seek is not None and isinstance(body_pos, integer_types): | |
121 try: | |
122 body_seek(body_pos) | |
123 except (IOError, OSError): | |
124 raise UnrewindableBodyError( | |
125 "An error occurred when rewinding request body for redirect/retry." | |
126 ) | |
127 elif body_pos is _FAILEDTELL: | |
128 raise UnrewindableBodyError( | |
129 "Unable to record file position for rewinding " | |
130 "request body during a redirect/retry." | |
131 ) | |
132 else: | |
133 raise ValueError( | |
134 "body_pos must be of type integer, instead it was %s." % type(body_pos) | |
135 ) |