Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/pip/_internal/network/utils.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 from pip._vendor.requests.models import CONTENT_CHUNK_SIZE, Response | |
2 | |
3 from pip._internal.exceptions import NetworkConnectionError | |
4 from pip._internal.utils.typing import MYPY_CHECK_RUNNING | |
5 | |
6 if MYPY_CHECK_RUNNING: | |
7 from typing import Dict, Iterator | |
8 | |
9 # The following comments and HTTP headers were originally added by | |
10 # Donald Stufft in git commit 22c562429a61bb77172039e480873fb239dd8c03. | |
11 # | |
12 # We use Accept-Encoding: identity here because requests defaults to | |
13 # accepting compressed responses. This breaks in a variety of ways | |
14 # depending on how the server is configured. | |
15 # - Some servers will notice that the file isn't a compressible file | |
16 # and will leave the file alone and with an empty Content-Encoding | |
17 # - Some servers will notice that the file is already compressed and | |
18 # will leave the file alone, adding a Content-Encoding: gzip header | |
19 # - Some servers won't notice anything at all and will take a file | |
20 # that's already been compressed and compress it again, and set | |
21 # the Content-Encoding: gzip header | |
22 # By setting this to request only the identity encoding we're hoping | |
23 # to eliminate the third case. Hopefully there does not exist a server | |
24 # which when given a file will notice it is already compressed and that | |
25 # you're not asking for a compressed file and will then decompress it | |
26 # before sending because if that's the case I don't think it'll ever be | |
27 # possible to make this work. | |
28 HEADERS = {'Accept-Encoding': 'identity'} # type: Dict[str, str] | |
29 | |
30 | |
31 def raise_for_status(resp): | |
32 # type: (Response) -> None | |
33 http_error_msg = '' | |
34 if isinstance(resp.reason, bytes): | |
35 # We attempt to decode utf-8 first because some servers | |
36 # choose to localize their reason strings. If the string | |
37 # isn't utf-8, we fall back to iso-8859-1 for all other | |
38 # encodings. | |
39 try: | |
40 reason = resp.reason.decode('utf-8') | |
41 except UnicodeDecodeError: | |
42 reason = resp.reason.decode('iso-8859-1') | |
43 else: | |
44 reason = resp.reason | |
45 | |
46 if 400 <= resp.status_code < 500: | |
47 http_error_msg = '%s Client Error: %s for url: %s' % ( | |
48 resp.status_code, reason, resp.url) | |
49 | |
50 elif 500 <= resp.status_code < 600: | |
51 http_error_msg = '%s Server Error: %s for url: %s' % ( | |
52 resp.status_code, reason, resp.url) | |
53 | |
54 if http_error_msg: | |
55 raise NetworkConnectionError(http_error_msg, response=resp) | |
56 | |
57 | |
58 def response_chunks(response, chunk_size=CONTENT_CHUNK_SIZE): | |
59 # type: (Response, int) -> Iterator[bytes] | |
60 """Given a requests Response, provide the data chunks. | |
61 """ | |
62 try: | |
63 # Special case for urllib3. | |
64 for chunk in response.raw.stream( | |
65 chunk_size, | |
66 # We use decode_content=False here because we don't | |
67 # want urllib3 to mess with the raw bytes we get | |
68 # from the server. If we decompress inside of | |
69 # urllib3 then we cannot verify the checksum | |
70 # because the checksum will be of the compressed | |
71 # file. This breakage will only occur if the | |
72 # server adds a Content-Encoding header, which | |
73 # depends on how the server was configured: | |
74 # - Some servers will notice that the file isn't a | |
75 # compressible file and will leave the file alone | |
76 # and with an empty Content-Encoding | |
77 # - Some servers will notice that the file is | |
78 # already compressed and will leave the file | |
79 # alone and will add a Content-Encoding: gzip | |
80 # header | |
81 # - Some servers won't notice anything at all and | |
82 # will take a file that's already been compressed | |
83 # and compress it again and set the | |
84 # Content-Encoding: gzip header | |
85 # | |
86 # By setting this not to decode automatically we | |
87 # hope to eliminate problems with the second case. | |
88 decode_content=False, | |
89 ): | |
90 yield chunk | |
91 except AttributeError: | |
92 # Standard file-like object. | |
93 while True: | |
94 chunk = response.raw.read(chunk_size) | |
95 if not chunk: | |
96 break | |
97 yield chunk |