Skip to content

Commit

Permalink
Use urlopen's context parameter instead of cafile
Browse files Browse the repository at this point in the history
  • Loading branch information
glandium committed Dec 26, 2024
1 parent 5a03d0f commit ea3803a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/taskgraph/run-task/fetch-content
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import os
import pathlib
import random
import re
import ssl
import stat
import subprocess
import sys
Expand Down Expand Up @@ -190,9 +191,11 @@ def stream_download(url, sha256=None, size=None, headers=None):
req_headers[key.strip()] = val.strip()

req = urllib.request.Request(url, None, req_headers)
with urllib.request.urlopen(
req, timeout=60, cafile=certifi.where()
) if certifi else urllib.request.urlopen(req, timeout=60) as fh:
kwargs = {}
if certifi:
ssl_context = ssl.create_default_context(cafile=certifi.where())
kwargs["context"] = context = ssl_context
with urllib.request.urlopen(req, timeout=60, **kwargs) as fh:
if not url.endswith(".gz") and fh.info().get("Content-Encoding") == "gzip":
fh = gzip.GzipFile(fileobj=fh)
else:
Expand Down
2 changes: 1 addition & 1 deletion test/test_scripts_fetch_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def fetch_content_mod():
def test_stream_download(
monkeypatch, fetch_content_mod, url, sha256, size, headers, raises
):
def mock_urlopen(req, timeout=None, *, cafile=None):
def mock_urlopen(req, timeout=None, *, context=None):
assert req._full_url == url
assert timeout is not None
if headers:
Expand Down

0 comments on commit ea3803a

Please sign in to comment.