|
20 | 20 | # Globals |
21 | 21 | logger = logging.getLogger(__name__) |
22 | 22 |
|
23 | | -# Classess |
| 23 | +# Classes |
24 | 24 | class RequestsFetcher(FetcherInterface): |
25 | 25 | """A concrete implementation of FetcherInterface based on the Requests |
26 | 26 | library. |
@@ -86,58 +86,60 @@ def fetch(self, url, required_length): |
86 | 86 | status = e.response.status_code |
87 | 87 | raise exceptions.FetcherHTTPError(str(e), status) |
88 | 88 |
|
89 | | - # Define a generator function to be returned by fetch. This way the |
90 | | - # caller of fetch can differentiate between connection and actual data |
91 | | - # download and measure download times accordingly. |
92 | | - def chunks(): |
93 | | - try: |
94 | | - bytes_received = 0 |
95 | | - while True: |
96 | | - # We download a fixed chunk of data in every round. This is |
97 | | - # so that we can defend against slow retrieval attacks. |
98 | | - # Furthermore, we do not wish to download an extremely |
99 | | - # large file in one shot. Before beginning the round, sleep |
100 | | - # (if set) for a short amount of time so that the CPU is not |
101 | | - # hogged in the while loop. |
102 | | - if settings.SLEEP_BEFORE_ROUND: |
103 | | - time.sleep(settings.SLEEP_BEFORE_ROUND) |
104 | | - |
105 | | - read_amount = min( |
106 | | - settings.CHUNK_SIZE, |
107 | | - required_length - bytes_received, |
108 | | - ) |
109 | | - |
110 | | - # NOTE: This may not handle some servers adding a |
111 | | - # Content-Encoding header, which may cause urllib3 to |
112 | | - # misbehave: |
113 | | - # https://github.com/pypa/pip/blob/404838abcca467648180b358598c597b74d568c9/src/pip/_internal/download.py#L547-L582 |
114 | | - data = response.raw.read(read_amount) |
115 | | - bytes_received += len(data) |
| 89 | + return self._chunks(response, required_length) |
116 | 90 |
|
117 | | - # We might have no more data to read. Check number of bytes |
118 | | - # downloaded. |
119 | | - if not data: |
120 | | - logger.debug( |
121 | | - "Downloaded %d out of %d bytes", |
122 | | - bytes_received, |
123 | | - required_length, |
124 | | - ) |
| 91 | + @staticmethod |
| 92 | + def _chunks(response, required_length): |
| 93 | + """A generator function to be returned by fetch. This way the |
| 94 | + caller of fetch can differentiate between connection and actual data |
| 95 | + download.""" |
125 | 96 |
|
126 | | - # Finally, we signal that the download is complete. |
127 | | - break |
| 97 | + try: |
| 98 | + bytes_received = 0 |
| 99 | + while True: |
| 100 | + # We download a fixed chunk of data in every round. This is |
| 101 | + # so that we can defend against slow retrieval attacks. |
| 102 | + # Furthermore, we do not wish to download an extremely |
| 103 | + # large file in one shot. Before beginning the round, sleep |
| 104 | + # (if set) for a short amount of time so that the CPU is not |
| 105 | + # hogged in the while loop. |
| 106 | + if settings.SLEEP_BEFORE_ROUND: |
| 107 | + time.sleep(settings.SLEEP_BEFORE_ROUND) |
| 108 | + |
| 109 | + read_amount = min( |
| 110 | + settings.CHUNK_SIZE, |
| 111 | + required_length - bytes_received, |
| 112 | + ) |
| 113 | + |
| 114 | + # NOTE: This may not handle some servers adding a |
| 115 | + # Content-Encoding header, which may cause urllib3 to |
| 116 | + # misbehave: |
| 117 | + # https://github.com/pypa/pip/blob/404838abcca467648180b358598c597b74d568c9/src/pip/_internal/download.py#L547-L582 |
| 118 | + data = response.raw.read(read_amount) |
| 119 | + bytes_received += len(data) |
| 120 | + |
| 121 | + # We might have no more data to read. Check number of bytes |
| 122 | + # downloaded. |
| 123 | + if not data: |
| 124 | + logger.debug( |
| 125 | + "Downloaded %d out of %d bytes", |
| 126 | + bytes_received, |
| 127 | + required_length, |
| 128 | + ) |
128 | 129 |
|
129 | | - yield data |
| 130 | + # Finally, we signal that the download is complete. |
| 131 | + break |
130 | 132 |
|
131 | | - if bytes_received >= required_length: |
132 | | - break |
| 133 | + yield data |
133 | 134 |
|
134 | | - except urllib3.exceptions.ReadTimeoutError as e: |
135 | | - raise exceptions.SlowRetrievalError(str(e)) |
| 135 | + if bytes_received >= required_length: |
| 136 | + break |
136 | 137 |
|
137 | | - finally: |
138 | | - response.close() |
| 138 | + except urllib3.exceptions.ReadTimeoutError as e: |
| 139 | + raise exceptions.SlowRetrievalError(str(e)) |
139 | 140 |
|
140 | | - return chunks() |
| 141 | + finally: |
| 142 | + response.close() |
141 | 143 |
|
142 | 144 | def _get_session(self, url): |
143 | 145 | """Returns a different customized requests.Session per schema+hostname |
|
0 commit comments