Skip to content

Commit

Permalink
python/grass/utils: fix checking server response content type/dispost…
Browse files Browse the repository at this point in the history
…ion 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
  • Loading branch information
tmszi authored Nov 6, 2024
1 parent 98bccea commit 48e382c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion python/grass/utils/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""Download and extract various archives"""

import os
import re
import shutil
import tarfile
import tempfile
Expand All @@ -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)
Expand Down Expand Up @@ -170,7 +175,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"
Expand Down

0 comments on commit 48e382c

Please sign in to comment.