Skip to content

Commit f1cb16a

Browse files
try to fix http_download
1 parent 739a71f commit f1cb16a

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

lib/inputstreamhelper/utils.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,10 @@ def download_path(url):
7272

7373
return os.path.join(temp_path(), filename)
7474

75-
76-
7775
def _http_request(url, headers=None, time_out=10):
78-
"""Perform an HTTP request and return the response and content."""
79-
headers = headers or {}
80-
76+
"""Perform an HTTP request and return the response object and content."""
77+
if headers is None:
78+
headers = {}
8179
log(0, 'Request URL: {url}', url=url)
8280
request = Request(url, headers=headers)
8381

@@ -86,14 +84,14 @@ def _http_request(url, headers=None, time_out=10):
8684
log(0, 'Response code: {code}', code=response.getcode())
8785
if 400 <= response.getcode() < 600:
8886
raise HTTPError(url, response.getcode(), f'HTTP {response.getcode()} Error for url: {url}', response.headers, None)
89-
# Read the content inside the `with` block
9087
content = response.read()
88+
# Return both response object and content
9189
return response, content
9290
except (HTTPError, URLError) as err:
9391
log(2, 'Download failed with error {}'.format(err))
9492
if yesno_dialog(localize(30004), '{line1}\n{line2}'.format(line1=localize(30063), line2=localize(30065))): # Internet down, try again?
9593
return _http_request(url, headers, time_out)
96-
return None
94+
return None, None
9795

9896
def http_get(url):
9997
"""Perform an HTTP GET request and return content."""
@@ -131,7 +129,7 @@ def http_download(url, message=None, checksum=None, hash_alg='sha1', dl_size=Non
131129
log(4, 'Invalid hash algorithm specified: {}'.format(hash_alg))
132130
checksum = None
133131

134-
req = _http_request(url)
132+
req, _ = _http_request(url)
135133
if req is None:
136134
return None
137135

@@ -166,7 +164,7 @@ def http_download(url, message=None, checksum=None, hash_alg='sha1', dl_size=Non
166164
return False
167165

168166
headers = {'Range': 'bytes={}-{}'.format(size, total_length)}
169-
req = _http_request(url, headers=headers)
167+
req, _ = _http_request(url, headers=headers)
170168
if req is None:
171169
return None
172170
continue

0 commit comments

Comments
 (0)