From 17e351f82515bee3ec2e41a83d81ec5752889b41 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Mon, 10 Feb 2025 15:23:09 +0800 Subject: [PATCH] support SSL_CERT_FILE env for pycurl http backend --- .github/workflows/tests.yaml | 11 +---------- nvchecker/httpclient/tornado_httpclient.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 2de8f40..b3a9bb1 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -69,7 +69,7 @@ jobs: path: ~/.mitmproxy key: ${{ env.cache-name }} restore-keys: | - ${{ env.cache-name }} + ${{ env.cache-name }}- - name: Install mitmproxy run: | sudo apt update @@ -82,12 +82,3 @@ jobs: # uses: lhotari/action-upterm@v1 - name: Run pytest run: scripts/run_cached_tests - - - name: save mitmproxy cache - if: always() - uses: actions/cache/save@v4 - env: - cache-name: cache-mitm - with: - path: ~/.mitmproxy - key: ${{ env.cache-name }} diff --git a/nvchecker/httpclient/tornado_httpclient.py b/nvchecker/httpclient/tornado_httpclient.py index ce813ff..760c7f4 100644 --- a/nvchecker/httpclient/tornado_httpclient.py +++ b/nvchecker/httpclient/tornado_httpclient.py @@ -4,6 +4,7 @@ import json as _json from urllib.parse import urlencode from typing import Optional, Dict, Any +import os from tornado.httpclient import AsyncHTTPClient, HTTPRequest @@ -17,8 +18,9 @@ from .base import BaseSession, TemporaryError, Response, HTTPError __all__ = ['session'] HTTP2_AVAILABLE = None if pycurl else False +SSL_CERT_FILE = os.environ.get('SSL_CERT_FILE') -def try_use_http2(curl): +def setup_curl(curl): global HTTP2_AVAILABLE if HTTP2_AVAILABLE is None: try: @@ -29,6 +31,11 @@ def try_use_http2(curl): elif HTTP2_AVAILABLE: curl.setopt(pycurl.HTTP_VERSION, 4) + if SSL_CERT_FILE: + curl.setopt_string(pycurl.CAINFO, SSL_CERT_FILE) + # some servers insist sending us compressed content + curl.setopt_string(pycurl.ACCEPT_ENCODING, "") + class TornadoSession(BaseSession): def setup( self, @@ -68,7 +75,7 @@ class TornadoSession(BaseSession): kwargs['body'] = body elif json: kwargs['body'] = _json.dumps(json) - kwargs['prepare_curl_callback'] = try_use_http2 + kwargs['prepare_curl_callback'] = setup_curl if proxy: host, port = proxy.rsplit(':', 1)