From 6c2f8d1d399715e3f51ffd40ac7fb8c2e0f4209f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20Negru=C8=9Biu?= Date: Sat, 1 Jun 2024 12:52:22 +0300 Subject: [PATCH] GH-19: Replaced _acquire_curl-ca-bundle.bat with _get_curl-ca-bundle.py --- src/nscurl/Makefile.windows | 2 +- src/nscurl/NScurl.vcxproj | 8 +++---- src/nscurl/_acquire_curl-ca-bundle.bat | 7 ------ src/nscurl/_get_curl-ca-bundle.py | 31 ++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 12 deletions(-) delete mode 100644 src/nscurl/_acquire_curl-ca-bundle.bat create mode 100644 src/nscurl/_get_curl-ca-bundle.py diff --git a/src/nscurl/Makefile.windows b/src/nscurl/Makefile.windows index 971343b..87540e8 100644 --- a/src/nscurl/Makefile.windows +++ b/src/nscurl/Makefile.windows @@ -137,7 +137,7 @@ nsis-sdk: curl-ca-bundle: REM ----- curl-ca-bundle.crt -------------------------------------------------- - call _acquire_curl-ca-bundle.bat + py -3 _get_curl-ca-bundle.py update-file-version: REM ----- Resource.rc --------------------------------------------------------- diff --git a/src/nscurl/NScurl.vcxproj b/src/nscurl/NScurl.vcxproj index a3ada85..3c989d4 100644 --- a/src/nscurl/NScurl.vcxproj +++ b/src/nscurl/NScurl.vcxproj @@ -88,7 +88,7 @@ version.lib;libcurl-d.lib;libcrypto.lib;libssl.lib;nghttp2.lib;zlibd.lib;zstd.lib;brotlicommon.lib;brotlidec.lib;ws2_32.lib;Wldap32.lib;Crypt32.lib;%(AdditionalDependencies) - call py -3 _get_nsis_sdk.py && call _acquire_curl-ca-bundle.bat && call py -3 _update_resource_version.py && call "$(SolutionDir)_install_vcpkg.bat" $(Platform) msbuild static + call py -3 _get_nsis_sdk.py && py -3 _get_curl-ca-bundle.py && call py -3 _update_resource_version.py && call "$(SolutionDir)_install_vcpkg.bat" $(Platform) msbuild static @@ -105,7 +105,7 @@ version.lib;libcurl-d.lib;libcrypto.lib;libssl.lib;nghttp2.lib;zlibd.lib;zstd.lib;brotlicommon.lib;brotlidec.lib;ws2_32.lib;Wldap32.lib;Crypt32.lib;%(AdditionalDependencies) - call py -3 _get_nsis_sdk.py && call _acquire_curl-ca-bundle.bat && call py -3 _update_resource_version.py && call "$(SolutionDir)_install_vcpkg.bat" $(Platform) msbuild static + call py -3 _get_nsis_sdk.py && py -3 _get_curl-ca-bundle.py && call py -3 _update_resource_version.py && call "$(SolutionDir)_install_vcpkg.bat" $(Platform) msbuild static @@ -125,7 +125,7 @@ version.lib;libcurl.lib;libcrypto.lib;libssl.lib;nghttp2.lib;zlib.lib;zstd.lib;brotlicommon.lib;brotlidec.lib;ws2_32.lib;Wldap32.lib;Crypt32.lib;%(AdditionalDependencies) - call py -3 _get_nsis_sdk.py && call _acquire_curl-ca-bundle.bat && call py -3 _update_resource_version.py && call "$(SolutionDir)_install_vcpkg.bat" $(Platform) msbuild static + call py -3 _get_nsis_sdk.py && py -3 _get_curl-ca-bundle.py && call py -3 _update_resource_version.py && call "$(SolutionDir)_install_vcpkg.bat" $(Platform) msbuild static @@ -144,7 +144,7 @@ version.lib;libcurl.lib;libcrypto.lib;libssl.lib;nghttp2.lib;zlib.lib;zstd.lib;brotlicommon.lib;brotlidec.lib;ws2_32.lib;Wldap32.lib;Crypt32.lib;%(AdditionalDependencies) - call py -3 _get_nsis_sdk.py && call _acquire_curl-ca-bundle.bat && call py -3 _update_resource_version.py && call "$(SolutionDir)_install_vcpkg.bat" $(Platform) msbuild static + call py -3 _get_nsis_sdk.py && py -3 _get_curl-ca-bundle.py && call py -3 _update_resource_version.py && call "$(SolutionDir)_install_vcpkg.bat" $(Platform) msbuild static diff --git a/src/nscurl/_acquire_curl-ca-bundle.bat b/src/nscurl/_acquire_curl-ca-bundle.bat deleted file mode 100644 index 31f0fae..0000000 --- a/src/nscurl/_acquire_curl-ca-bundle.bat +++ /dev/null @@ -1,7 +0,0 @@ -@echo off -setlocal enabledelayedexpansion - -set outdir=%~dp0 - -echo Downloading https://curl.se/ca/cacert.pem ... -curl --no-progress-meter -L -f -o "%outdir%/curl-ca-bundle.crt" -z "%outdir%/curl-ca-bundle.crt" https://curl.se/ca/cacert.pem || exit /b !errorlevel! diff --git a/src/nscurl/_get_curl-ca-bundle.py b/src/nscurl/_get_curl-ca-bundle.py new file mode 100644 index 0000000..93d7cd2 --- /dev/null +++ b/src/nscurl/_get_curl-ca-bundle.py @@ -0,0 +1,31 @@ +# Download curl-ca-bundle.crt from Curl website + +from pathlib import Path +from urllib import request +import ssl +from pip._vendor import certifi # use pip certifi to fix "Let's Encrypt" issues (urllib.error.URLError: ) +import datetime + +url = 'https://curl.se/ca/cacert.pem' +file = Path(__file__).parent.joinpath('curl-ca-bundle.crt') + +print(f"Download {url} ...") + +headers = {} +if file.exists(): + dt = datetime.datetime.fromtimestamp(file.stat().st_mtime, tz=datetime.timezone.utc) + headers['If-Modified-Since'] = dt.strftime('%a, %d %b %Y %H:%M:%S GMT') + print(f" If-Modified-Since: {headers['If-Modified-Since']}") + +sslctx = ssl.create_default_context(cafile=certifi.where()) +try: + with request.urlopen(request.Request(url, headers=headers), context=sslctx) as http: + file.parent.mkdir(parents=True, exist_ok=True) + with open(file, 'wb') as outfile: + outfile.write(http.read()) + print(f" {http.status} {http.reason}, {http.getheader('Content-Length')} bytes") +except request.HTTPError as ex: + if ex.code == 304: + print(f" {ex.code} {ex.reason}, {file.stat().st_size} bytes, the file is already up-to-date") + else: + raise