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