Skip to content

Commit 35fb22c

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 0b77eb9 commit 35fb22c

File tree

1 file changed

+47
-46
lines changed

1 file changed

+47
-46
lines changed

tuf/ngclient/_internal/requests_fetcher.py

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

24-
# Classess
24+
# Classes
2525
class RequestsFetcher(FetcherInterface):
2626
"""A concrete implementation of FetcherInterface based on the Requests
2727
library.
@@ -90,58 +90,59 @@ def fetch(self, url, required_length):
9090
status = e.response.status_code
9191
raise exceptions.FetcherHTTPError(str(e), status)
9292

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

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

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

133-
yield data
133+
# Finally, we signal that the download is complete.
134+
break
134135

135-
if bytes_received >= required_length:
136-
break
136+
yield data
137137

138-
except urllib3.exceptions.ReadTimeoutError as e:
139-
raise exceptions.SlowRetrievalError(str(e))
138+
if bytes_received >= required_length:
139+
break
140140

141-
finally:
142-
response.close()
141+
except urllib3.exceptions.ReadTimeoutError as e:
142+
raise exceptions.SlowRetrievalError(str(e))
143143

144-
return chunks()
144+
finally:
145+
response.close()
145146

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

0 commit comments

Comments
 (0)