Skip to content

Commit cdf7f73

Browse files
committed
Add class FetcherSettings
Move tuf settings related to network download to a separate class which can be used by a FetcherInterface implementation or replaced with its own settings. Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
1 parent 0019361 commit cdf7f73

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

tuf/fetcher.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import urllib3.exceptions
2525
import tuf.exceptions
26-
import tuf.settings
2726

2827
logger = logging.getLogger(__name__)
2928

@@ -62,7 +61,25 @@ def fetch(self, url, required_length):
6261

6362

6463

65-
class RequestsFetcher(FetcherInterface):
64+
class FetcherSettings():
65+
"""
66+
<Purpose>
67+
Data class. Defines fetcher-specific settings and their default values
68+
"""
69+
70+
# The maximum chunk of data, in bytes, we would download in every round.
71+
CHUNK_SIZE = 400000 #bytes
72+
# Set a timeout value in seconds (float) for non-blocking socket operations.
73+
SOCKET_TIMEOUT = 4 #seconds
74+
# The client's update procedure (contained within a while-loop) can potentially
75+
# hog the CPU. The following setting can be used to force the update sequence
76+
# to suspend execution for a specified amount of time. See
77+
# theupdateframework/tuf/issue#338.
78+
SLEEP_BEFORE_ROUND = None
79+
80+
81+
82+
class RequestsFetcher(FetcherInterface, FetcherSettings):
6683
"""
6784
<Purpose>
6885
A concrete implementation of FetcherInterface based on the Requests
@@ -97,7 +114,7 @@ def fetch(self, url, required_length):
97114
# - connect timeout (max delay before first byte is received)
98115
# - read (gap) timeout (max delay between bytes received)
99116
with session.get(url, stream=True,
100-
timeout=tuf.settings.SOCKET_TIMEOUT) as response:
117+
timeout=self.SOCKET_TIMEOUT) as response:
101118

102119
# Check response status.
103120
response.raise_for_status()
@@ -110,11 +127,11 @@ def fetch(self, url, required_length):
110127
# to download an extremely large file in one shot.
111128
# Before beginning the round, sleep (if set) for a short amount of time
112129
# so that the CPU is not hogged in the while loop.
113-
if tuf.settings.SLEEP_BEFORE_ROUND:
114-
time.sleep(tuf.settings.SLEEP_BEFORE_ROUND)
130+
if self.SLEEP_BEFORE_ROUND:
131+
time.sleep(self.SLEEP_BEFORE_ROUND)
115132

116133
read_amount = min(
117-
tuf.settings.CHUNK_SIZE, required_length - bytes_received)
134+
self.CHUNK_SIZE, required_length - bytes_received)
118135

119136
# NOTE: This may not handle some servers adding a Content-Encoding
120137
# header, which may cause urllib3 to misbehave:

tuf/settings.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@
7474
# download Targets metadata.
7575
DEFAULT_TARGETS_REQUIRED_LENGTH = 5000000 #bytes
7676

77-
# Set a timeout value in seconds (float) for non-blocking socket operations.
78-
SOCKET_TIMEOUT = 4 #seconds
79-
80-
# The maximum chunk of data, in bytes, we would download in every round.
81-
CHUNK_SIZE = 400000 #bytes
82-
8377
# The minimum average download speed (bytes/second) that must be met to
8478
# avoid being considered as a slow retrieval attack.
8579
MIN_AVERAGE_DOWNLOAD_SPEED = 50 #bytes/second
@@ -97,12 +91,6 @@
9791
# The hashing algorithms used to compute file hashes
9892
FILE_HASH_ALGORITHMS = ['sha256', 'sha512']
9993

100-
# The client's update procedure (contained within a while-loop) can potentially
101-
# hog the CPU. The following setting can be used to force the update sequence
102-
# to suspend execution for a specified amount of time. See
103-
# theupdateframework/tuf/issue#338.
104-
SLEEP_BEFORE_ROUND = None
105-
10694
# Maximum number of root metadata file rotations we should perform in order to
10795
# prevent a denial-of-service (DoS) attack.
10896
MAX_NUMBER_ROOT_ROTATIONS = 2**5

0 commit comments

Comments
 (0)