-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompression.diff
More file actions
483 lines (458 loc) · 19.8 KB
/
Copy pathcompression.diff
File metadata and controls
483 lines (458 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
--- input/input.py
+++ output/output.py
@@ -61,45 +61,14 @@
def merge_setting(request_setting, session_setting, dict_class=OrderedDict):
"""Determines appropriate setting for a given request, taking into account
the explicit setting on that request, and the setting in the session. If a
- setting is a dictionary, they will be merged together using `dict_class`
- """
-
- if session_setting is None:
- return request_setting
-
- if request_setting is None:
- return session_setting
-
- # Bypass if not a dictionary (e.g. verify)
- if not (
- isinstance(session_setting, Mapping) and isinstance(request_setting, Mapping)
- ):
- return request_setting
-
- merged_setting = dict_class(to_key_val_list(session_setting))
- merged_setting.update(to_key_val_list(request_setting))
-
- # Remove keys that are set to None. Extract keys first to avoid altering
- # the dictionary during iteration.
- none_keys = [k for (k, v) in merged_setting.items() if v is None]
- for key in none_keys:
- del merged_setting[key]
-
+... # 24 line(s) collapsed ⟦tj:077cde73280053b1704e089a0e9ed7be⟧
return merged_setting
def merge_hooks(request_hooks, session_hooks, dict_class=OrderedDict):
"""Properly merges both requests and session hooks.
- This is necessary because when request_hooks == {'response': []}, the
- merge breaks Session hooks entirely.
- """
- if session_hooks is None or session_hooks.get("response") == []:
- return request_hooks
-
- if request_hooks is None or request_hooks.get("response") == []:
- return session_hooks
-
+... # 9 line(s) collapsed ⟦tj:75f64ea8dfcfa010949254bf9caf5d94⟧
return merge_setting(request_hooks, session_hooks, dict_class)
@@ -107,53 +76,13 @@
def get_redirect_target(self, resp):
"""Receives a Response. Returns a redirect URI or ``None``"""
# Due to the nature of how requests processes redirects this method will
- # be called at least once upon the original response and at least twice
- # on each subsequent redirect response (if any).
- # If a custom mixin is used to handle this logic, it may be advantageous
- # to cache the redirect location onto the response object as a private
- # attribute.
- if resp.is_redirect:
- location = resp.headers["location"]
- # Currently the underlying http module on py3 decode headers
- # in latin1, but empirical evidence suggests that latin1 is very
- # rarely used with non-ASCII characters in HTTP headers.
- # It is more likely to get UTF8 header rather than latin1.
- # This causes incorrect handling of UTF8 encoded location headers.
- # To solve this, we re-encode the location in latin1.
- location = location.encode("latin1")
- return to_native_string(location, "utf8")
+... # 15 line(s) collapsed ⟦tj:171e3999e1bb483ef1c5e024c0740ae3⟧
return None
def should_strip_auth(self, old_url, new_url):
"""Decide whether Authorization header should be removed when redirecting"""
old_parsed = urlparse(old_url)
- new_parsed = urlparse(new_url)
- if old_parsed.hostname != new_parsed.hostname:
- return True
- # Special case: allow http -> https redirect when using the standard
- # ports. This isn't specified by RFC 7235, but is kept to avoid
- # breaking backwards compatibility with older versions of requests
- # that allowed any redirects on the same host.
- if (
- old_parsed.scheme == "http"
- and old_parsed.port in (80, None)
- and new_parsed.scheme == "https"
- and new_parsed.port in (443, None)
- ):
- return False
-
- # Handle default port usage corresponding to scheme.
- changed_port = old_parsed.port != new_parsed.port
- changed_scheme = old_parsed.scheme != new_parsed.scheme
- default_port = (DEFAULT_PORTS.get(old_parsed.scheme, None), None)
- if (
- not changed_scheme
- and old_parsed.port in default_port
- and new_parsed.port in default_port
- ):
- return False
-
- # Standard case: root URI must match
+... # 27 line(s) collapsed ⟦tj:3cb2ed79205a5da81fb84ed9613afccc⟧
return changed_port or changed_scheme
def resolve_redirects(
@@ -282,74 +211,19 @@
def rebuild_auth(self, prepared_request, response):
"""When being redirected we may want to strip authentication from the
request to avoid leaking credentials. This method intelligently removes
- and reapplies authentication where possible to avoid credential loss.
- """
- headers = prepared_request.headers
- url = prepared_request.url
-
- if "Authorization" in headers and self.should_strip_auth(
- response.request.url, url
- ):
- # If we get redirected to a new host, we should strip out any
- # authentication headers.
- del headers["Authorization"]
-
- # .netrc might have more auth for us on our new host.
- new_auth = get_netrc_auth(url) if self.trust_env else None
- if new_auth is not None:
+... # 15 line(s) collapsed ⟦tj:b26c57811ccb4aba6547cd59a7bf79ac⟧
prepared_request.prepare_auth(new_auth)
def rebuild_proxies(self, prepared_request, proxies):
"""This method re-evaluates the proxy configuration by considering the
environment variables. If we are redirected to a URL covered by
- NO_PROXY, we strip the proxy configuration. Otherwise, we set missing
- proxy keys for this URL (in case they were stripped by a previous
- redirect).
-
- This method also replaces the Proxy-Authorization header where
- necessary.
-
- :rtype: dict
- """
- headers = prepared_request.headers
- scheme = urlparse(prepared_request.url).scheme
- new_proxies = resolve_proxies(prepared_request, proxies, self.trust_env)
-
- if "Proxy-Authorization" in headers:
- del headers["Proxy-Authorization"]
-
- try:
- username, password = get_auth_from_url(new_proxies[scheme])
- except KeyError:
- username, password = None, None
-
- # urllib3 handles proxy authorization for us in the standard adapter.
- # Avoid appending this to TLS tunneled requests where it may be leaked.
- if not scheme.startswith("https") and username and password:
- headers["Proxy-Authorization"] = _basic_auth_str(username, password)
-
+... # 26 line(s) collapsed ⟦tj:9dd265d13fb9b594e16e7328053e4a46⟧
return new_proxies
def rebuild_method(self, prepared_request, response):
"""When being redirected we may want to change the method of the request
based on certain specs or browser behavior.
- """
- method = prepared_request.method
-
- # https://tools.ietf.org/html/rfc7231#section-6.4.4
- if response.status_code == codes.see_other and method != "HEAD":
- method = "GET"
-
- # Do what the browsers do, despite standards...
- # First, turn 302s into GETs.
- if response.status_code == codes.found and method != "HEAD":
- method = "GET"
-
- # Second, if a POST is responded to with a 301, turn it into a GET.
- # This bizarre behaviour is explained in Issue 1704.
- if response.status_code == codes.moved and method == "POST":
- method = "GET"
-
+... # 17 line(s) collapsed ⟦tj:43394ef7b9ad29f024a843b3ce8efc49⟧
prepared_request.method = method
@@ -457,44 +331,7 @@
def prepare_request(self, request):
"""Constructs a :class:`PreparedRequest <PreparedRequest>` for
transmission and returns it. The :class:`PreparedRequest` has settings
- merged from the :class:`Request <Request>` instance and those of the
- :class:`Session`.
-
- :param request: :class:`Request` instance to prepare with this
- session's settings.
- :rtype: requests.PreparedRequest
- """
- cookies = request.cookies or {}
-
- # Bootstrap CookieJar.
- if not isinstance(cookies, cookielib.CookieJar):
- cookies = cookiejar_from_dict(cookies)
-
- # Merge with session cookies
- merged_cookies = merge_cookies(
- merge_cookies(RequestsCookieJar(), self.cookies), cookies
- )
-
- # Set environment's basic authentication if not explicitly set.
- auth = request.auth
- if self.trust_env and not auth and not self.auth:
- auth = get_netrc_auth(request.url)
-
- p = PreparedRequest()
- p.prepare(
- method=request.method.upper(),
- url=request.url,
- files=request.files,
- data=request.data,
- json=request.json,
- headers=merge_setting(
- request.headers, self.headers, dict_class=CaseInsensitiveDict
- ),
- params=merge_setting(request.params, self.params),
- auth=merge_setting(auth, self.auth),
- cookies=merged_cookies,
- hooks=merge_hooks(request.hooks, self.hooks),
- )
+... # 38 line(s) collapsed ⟦tj:42b08a709fd99701fa8b1005a8498241⟧
return p
def request(
@@ -518,158 +355,30 @@
):
"""Constructs a :class:`Request <Request>`, prepares it and sends it.
Returns :class:`Response <Response>` object.
-
- :param method: method for the new :class:`Request` object.
- :param url: URL for the new :class:`Request` object.
- :param params: (optional) Dictionary or bytes to be sent in the query
- string for the :class:`Request`.
- :param data: (optional) Dictionary, list of tuples, bytes, or file-like
- object to send in the body of the :class:`Request`.
- :param json: (optional) json to send in the body of the
- :class:`Request`.
- :param headers: (optional) Dictionary of HTTP Headers to send with the
- :class:`Request`.
- :param cookies: (optional) Dict or CookieJar object to send with the
- :class:`Request`.
- :param files: (optional) Dictionary of ``'filename': file-like-objects``
- for multipart encoding upload.
- :param auth: (optional) Auth tuple or callable to enable
- Basic/Digest/Custom HTTP Auth.
- :param timeout: (optional) How long to wait for the server to send
- data before giving up, as a float, or a :ref:`(connect timeout,
- read timeout) <timeouts>` tuple.
- :type timeout: float or tuple
- :param allow_redirects: (optional) Set to True by default.
- :type allow_redirects: bool
- :param proxies: (optional) Dictionary mapping protocol or protocol and
- hostname to the URL of the proxy.
- :param hooks: (optional) Dictionary mapping hook name to one event or
- list of events, event must be callable.
- :param stream: (optional) whether to immediately download the response
- content. Defaults to ``False``.
- :param verify: (optional) Either a boolean, in which case it controls whether we verify
- the server's TLS certificate, or a string, in which case it must be a path
- to a CA bundle to use. Defaults to ``True``. When set to
- ``False``, requests will accept any TLS certificate presented by
- the server, and will ignore hostname mismatches and/or expired
- certificates, which will make your application vulnerable to
- man-in-the-middle (MitM) attacks. Setting verify to ``False``
- may be useful during local development or testing.
- :param cert: (optional) if String, path to ssl client cert file (.pem).
- If Tuple, ('cert', 'key') pair.
- :rtype: requests.Response
- """
- # Create the Request.
- req = Request(
- method=method.upper(),
- url=url,
- headers=headers,
- files=files,
- data=data or {},
- json=json,
- params=params or {},
- auth=auth,
- cookies=cookies,
- hooks=hooks,
- )
- prep = self.prepare_request(req)
-
- proxies = proxies or {}
-
- settings = self.merge_environment_settings(
- prep.url, proxies, stream, verify, cert
- )
-
- # Send the request.
- send_kwargs = {
- "timeout": timeout,
- "allow_redirects": allow_redirects,
- }
- send_kwargs.update(settings)
- resp = self.send(prep, **send_kwargs)
-
+... # 70 line(s) collapsed ⟦tj:ac4d895638fd05b557e26c6af9447447⟧
return resp
def get(self, url, **kwargs):
- r"""Sends a GET request. Returns :class:`Response` object.
+ ... # 9 line(s) collapsed ⟦tj:35a284a1d859efd30ab89ccfabe4fde5⟧
- :param url: URL for the new :class:`Request` object.
- :param \*\*kwargs: Optional arguments that ``request`` takes.
- :rtype: requests.Response
- """
-
- kwargs.setdefault("allow_redirects", True)
- return self.request("GET", url, **kwargs)
-
def options(self, url, **kwargs):
- r"""Sends a OPTIONS request. Returns :class:`Response` object.
+ ... # 9 line(s) collapsed ⟦tj:c6d5a653ac49a7e43374f8a465cc292e⟧
- :param url: URL for the new :class:`Request` object.
- :param \*\*kwargs: Optional arguments that ``request`` takes.
- :rtype: requests.Response
- """
-
- kwargs.setdefault("allow_redirects", True)
- return self.request("OPTIONS", url, **kwargs)
-
def head(self, url, **kwargs):
- r"""Sends a HEAD request. Returns :class:`Response` object.
+ ... # 9 line(s) collapsed ⟦tj:00b1fcd456f202ad04bd384586f0db5e⟧
- :param url: URL for the new :class:`Request` object.
- :param \*\*kwargs: Optional arguments that ``request`` takes.
- :rtype: requests.Response
- """
-
- kwargs.setdefault("allow_redirects", False)
- return self.request("HEAD", url, **kwargs)
-
def post(self, url, data=None, json=None, **kwargs):
- r"""Sends a POST request. Returns :class:`Response` object.
+ ... # 11 line(s) collapsed ⟦tj:eb6f65113720a2a096e9bf3f6860b3e5⟧
- :param url: URL for the new :class:`Request` object.
- :param data: (optional) Dictionary, list of tuples, bytes, or file-like
- object to send in the body of the :class:`Request`.
- :param json: (optional) json to send in the body of the :class:`Request`.
- :param \*\*kwargs: Optional arguments that ``request`` takes.
- :rtype: requests.Response
- """
-
- return self.request("POST", url, data=data, json=json, **kwargs)
-
def put(self, url, data=None, **kwargs):
- r"""Sends a PUT request. Returns :class:`Response` object.
+ ... # 10 line(s) collapsed ⟦tj:0f3058e56e5d9c92d80c6383b2aa7537⟧
- :param url: URL for the new :class:`Request` object.
- :param data: (optional) Dictionary, list of tuples, bytes, or file-like
- object to send in the body of the :class:`Request`.
- :param \*\*kwargs: Optional arguments that ``request`` takes.
- :rtype: requests.Response
- """
-
- return self.request("PUT", url, data=data, **kwargs)
-
def patch(self, url, data=None, **kwargs):
- r"""Sends a PATCH request. Returns :class:`Response` object.
+ ... # 10 line(s) collapsed ⟦tj:ac1a622e707b4b5044615919578a8542⟧
- :param url: URL for the new :class:`Request` object.
- :param data: (optional) Dictionary, list of tuples, bytes, or file-like
- object to send in the body of the :class:`Request`.
- :param \*\*kwargs: Optional arguments that ``request`` takes.
- :rtype: requests.Response
- """
-
- return self.request("PATCH", url, data=data, **kwargs)
-
def delete(self, url, **kwargs):
- r"""Sends a DELETE request. Returns :class:`Response` object.
+ ... # 8 line(s) collapsed ⟦tj:78d77344449dcc0e101e9e35b84cf497⟧
- :param url: URL for the new :class:`Request` object.
- :param \*\*kwargs: Optional arguments that ``request`` takes.
- :rtype: requests.Response
- """
-
- return self.request("DELETE", url, **kwargs)
-
def send(self, request, **kwargs):
"""Send a given PreparedRequest.
@@ -750,63 +459,20 @@
def merge_environment_settings(self, url, proxies, stream, verify, cert):
"""
Check the environment and merge it with some settings.
-
- :rtype: dict
- """
- # Gather clues from the surrounding environment.
- if self.trust_env:
- # Set environment's proxies.
- no_proxy = proxies.get("no_proxy") if proxies is not None else None
- env_proxies = get_environ_proxies(url, no_proxy=no_proxy)
- for k, v in env_proxies.items():
- proxies.setdefault(k, v)
-
- # Look for requests environment configuration
- # and be compatible with cURL.
- if verify is True or verify is None:
- verify = (
- os.environ.get("REQUESTS_CA_BUNDLE")
- or os.environ.get("CURL_CA_BUNDLE")
- or verify
- )
-
- # Merge all the kwargs.
- proxies = merge_setting(proxies, self.proxies)
- stream = merge_setting(stream, self.stream)
- verify = merge_setting(verify, self.verify)
- cert = merge_setting(cert, self.cert)
-
+... # 26 line(s) collapsed ⟦tj:7fc3edefc61e95e96f948aca2278a36e⟧
return {"proxies": proxies, "stream": stream, "verify": verify, "cert": cert}
def get_adapter(self, url):
- """
- Returns the appropriate connection adapter for the given URL.
+ ... # 11 line(s) collapsed ⟦tj:57d464a7af4bc58426e9989d3a9b308c⟧
- :rtype: requests.adapters.BaseAdapter
- """
- for prefix, adapter in self.adapters.items():
- if url.lower().startswith(prefix.lower()):
- return adapter
-
- # Nothing matches :-/
- raise InvalidSchema(f"No connection adapters were found for {url!r}")
-
def close(self):
"""Closes all adapters and as such the session"""
for v in self.adapters.values():
v.close()
def mount(self, prefix, adapter):
- """Registers a connection adapter to a prefix.
+ ... # 9 line(s) collapsed ⟦tj:16f26fd167d46459b3406509e7039be4⟧
- Adapters are sorted in descending order by prefix length.
- """
- self.adapters[prefix] = adapter
- keys_to_move = [k for k in self.adapters if len(k) < len(prefix)]
-
- for key in keys_to_move:
- self.adapters[key] = self.adapters.pop(key)
-
def __getstate__(self):
state = {attr: getattr(self, attr, None) for attr in self.__attrs__}
return state
@@ -819,13 +485,8 @@
def session():
"""
Returns a :class:`Session` for context-management.
-
- .. deprecated:: 1.0.0
-
- This method has been deprecated since version 1.0.0 and is only kept for
- backwards compatibility. New code should use :class:`~requests.sessions.Session`
- to create a session. This may be removed at a future date.
-
- :rtype: Session
- """
+... # 9 line(s) collapsed ⟦tj:cc0a4f0e52c49614c51ca0227d63022d⟧
return Session()
+[omitted blocks are individually retrievable: call tinyjuice_retrieve with the token inside an omission marker to expand just that block]
+
+[PARTIAL view — full original (30495 bytes): call tinyjuice_retrieve with token "ca44c8f145864a5b4e7c7d3b1caa2594"]
\ No newline at end of file