Skip to content

Commit 2c4ad7f

Browse files
committed
Simplify RequestsFetcher.fetch()
Make the internal function chunks() a separate private method of Reqests fetcher. Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
1 parent 02ea646 commit 2c4ad7f

File tree

1 file changed

+48
-46
lines changed

1 file changed

+48
-46
lines changed

tuf/ngclient/_internal/requests_fetcher.py

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# Globals
2121
logger = logging.getLogger(__name__)
2222

23-
# Classess
23+
# Classes
2424
class RequestsFetcher(FetcherInterface):
2525
"""A concrete implementation of FetcherInterface based on the Requests
2626
library.
@@ -86,58 +86,60 @@ def fetch(self, url, required_length):
8686
status = e.response.status_code
8787
raise exceptions.FetcherHTTPError(str(e), status)
8888

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)
11690

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."""
12596

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+
)
128129

129-
yield data
130+
# Finally, we signal that the download is complete.
131+
break
130132

131-
if bytes_received >= required_length:
132-
break
133+
yield data
133134

134-
except urllib3.exceptions.ReadTimeoutError as e:
135-
raise exceptions.SlowRetrievalError(str(e))
135+
if bytes_received >= required_length:
136+
break
136137

137-
finally:
138-
response.close()
138+
except urllib3.exceptions.ReadTimeoutError as e:
139+
raise exceptions.SlowRetrievalError(str(e))
139140

140-
return chunks()
141+
finally:
142+
response.close()
141143

142144
def _get_session(self, url):
143145
"""Returns a different customized requests.Session per schema+hostname

0 commit comments

Comments
 (0)