summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorBrad Kollmyer <bradk@vitalsoft.com>2026-06-21 15:31:42 -0700
committerBrad Kollmyer <bradk@vitalsoft.com>2026-06-21 15:42:29 -0700
commit516eccd41b75bbfd237440d919ef5292215ba30d (patch)
tree299c39981cf5aca33353b477edb454a9b95c9a0f /python
parent6c0fd99099a6251a12bc2287d5ba2ab115cd8015 (diff)
downloadvyos-1x-516eccd41b75bbfd237440d919ef5292215ba30d.tar.gz
vyos-1x-516eccd41b75bbfd237440d919ef5292215ba30d.zip
remote: T8829: fall back to GET when HEAD is not supported
HttpC.download() always probed remote URLs with HEAD before GET to discover redirects and Content-Length. Some APIs (e.g. AbuseIPDB) reject HEAD with 405 Method Not Allowed, causing firewall remote-group downloads to fail and leave empty cached list files. Treat HEAD 405/501 as unsupported and proceed with GET using the original URL. When HEAD does not provide Content-Length, read it from the GET response headers instead. Validate available storage after determining file size and before opening the destination file. Log sanitized download errors in vyos-domain-resolver when a remote- group list-file fetch fails. Add unit tests with mock servers that return 405 or 501 on HEAD and 200 on GET.
Diffstat (limited to 'python')
-rw-r--r--python/vyos/remote.py59
1 files changed, 36 insertions, 23 deletions
diff --git a/python/vyos/remote.py b/python/vyos/remote.py
index 3be87e179..724b46e98 100644
--- a/python/vyos/remote.py
+++ b/python/vyos/remote.py
@@ -413,33 +413,46 @@ class HttpC:
# Not only would it potentially mess up with the progress bar but
# `shutil.copyfileobj(request.raw, file)` does not handle automatic decoding.
s.headers.update({'Accept-Encoding': 'identity'})
+ final_urlstring = self.urlstring
+ size = None
with s.head(self.urlstring,
allow_redirects=True,
timeout=self.timeout) as r:
- # Abort early if the destination is inaccessible.
+ # Some servers (e.g. AbuseIPDB) reject HEAD with 405 but allow GET.
+ if r.status_code not in (405, 501):
+ # Abort early if the destination is inaccessible.
+ r.raise_for_status()
+ # If the request got redirected, keep the last URL we ended up with.
+ final_urlstring = r.url
+ if r.history and self.progressbar:
+ print_error('Redirecting to ' + final_urlstring)
+ # Check for the prospective file size.
+ try:
+ size = int(r.headers['Content-Length'])
+ # In case the server does not supply the header.
+ except KeyError:
+ size = None
+ with s.get(final_urlstring, stream=True, timeout=self.timeout) as r:
r.raise_for_status()
- # If the request got redirected, keep the last URL we ended up with.
- final_urlstring = r.url
- if r.history and self.progressbar:
- print_error('Redirecting to ' + final_urlstring)
- # Check for the prospective file size.
- try:
- size = int(r.headers['Content-Length'])
- # In case the server does not supply the header.
- except KeyError:
- size = None
- if self.check_space:
- check_storage(location, size)
- with s.get(final_urlstring, stream=True,
- timeout=self.timeout) as r, open(location, 'wb') as f:
- if self.progressbar and size:
- with Progressbar(CHUNK_SIZE / size) as p:
- for chunk in iter(lambda: begin(p.increment(), r.raw.read(CHUNK_SIZE)), b''):
- f.write(chunk)
- else:
- # We'll try to stream the download directly with `copyfileobj()` so that large
- # files (like entire VyOS images) don't occupy much memory.
- shutil.copyfileobj(r.raw, f)
+ if size is None:
+ try:
+ size = int(r.headers['Content-Length'])
+ except KeyError:
+ size = None
+ if self.check_space:
+ check_storage(location, size)
+ with open(location, 'wb') as f:
+ if self.progressbar and size:
+ with Progressbar(CHUNK_SIZE / size) as p:
+ for chunk in iter(
+ lambda: begin(p.increment(), r.raw.read(CHUNK_SIZE)),
+ b'',
+ ):
+ f.write(chunk)
+ else:
+ # We'll try to stream the download directly with `copyfileobj()` so that large
+ # files (like entire VyOS images) don't occupy much memory.
+ shutil.copyfileobj(r.raw, f)
def upload(self, location: str):
# Does not yet support progressbars.