From a51aad724ccda6829d2c4410c1d8c5aa52837043 Mon Sep 17 00:00:00 2001 From: Tomas Zigo <50632337+tmszi@users.noreply.github.com> Date: Wed, 6 Nov 2024 06:33:20 +0100 Subject: [PATCH] python/grass/utils: fix checking server response content type/dispostion header (#4658) To allow to download ZIP file. Server response headers which indicating ZIP file: content-type: application/octet-stream content-disposition: attachment; filename=natural_earth_dataset.zip Fix download Natural Earth Dataset in WGS84 from the server URL https://zenodo.org/records/13370131/files/natural_earth_dataset.zip --- python/grass/utils/download.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/python/grass/utils/download.py b/python/grass/utils/download.py index 68fb4f5eeb0..2565286d514 100644 --- a/python/grass/utils/download.py +++ b/python/grass/utils/download.py @@ -13,6 +13,7 @@ """Download and extract various archives""" import os +import re import shutil import tarfile import tempfile @@ -23,6 +24,10 @@ from urllib.request import urlretrieve +reponse_content_type_header_pattern = re.compile(r"application/(zip|octet-stream)") +reponse_content_disposition_header_pattern = re.compile(r"attachment; filename=.*.zip$") + + def debug(*args, **kwargs): """Print a debug message (to be used in this module only) @@ -524,7 +529,13 @@ def download_and_extract(source, reporthook=None): ) except URLError: raise DownloadError(url_error_message.format(url=source)) - if headers.get("content-type", "") != "application/zip": + + if not re.search( + reponse_content_type_header_pattern, headers.get("content-type", "") + ) and not re.search( + reponse_content_disposition_header_pattern, + headers.get("content-disposition", ""), + ): raise DownloadError( _( "Download of <{url}> failed or file <{name}> is not a ZIP file"