diff --git a/README.rst b/README.rst index 4b9163f..45cb5c2 100644 --- a/README.rst +++ b/README.rst @@ -217,6 +217,29 @@ Whether links with broken hash anchors should be marked as valid. Disable this if you want that links to anchors which are not contained in the link target's HTML source are marked as invalid. +LINKCHECK_PROXIES +~~~~~~~~~~~~~~~~~ + +Default: `{}` + +Allows you to make your `check_external` requests via a proxy. Expects a dictionary, e.g.: + +``` +LINKCHECK_PROXIES = { + "http": "http://...", + "https": "https://...", +} +``` + + +LINKCHECK_TRUST_PROXY_SSL +~~~~~~~~~~~~~~~~~~~~~~~~~ + +Default: `False` + +If you are making your requests via a proxy, you can use this setting to turn off SSL verification for the proxy. + + django-filebrowser integration ------------------------------ diff --git a/linkcheck/linkcheck_settings.py b/linkcheck/linkcheck_settings.py index 566be34..617aef4 100644 --- a/linkcheck/linkcheck_settings.py +++ b/linkcheck/linkcheck_settings.py @@ -59,3 +59,5 @@ SITE_DOMAINS = getattr(settings, 'LINKCHECK_SITE_DOMAINS', []) DISABLE_LISTENERS = getattr(settings, 'LINKCHECK_DISABLE_LISTENERS', False) TOLERATE_BROKEN_ANCHOR = getattr(settings, 'LINKCHECK_TOLERATE_BROKEN_ANCHOR', True) +PROXIES = getattr(settings, 'LINKCHECK_PROXIES', {}) +TRUST_PROXY_SSL = getattr(settings, 'LINKCHECK_TRUST_PROXY_SSL', False) diff --git a/linkcheck/models.py b/linkcheck/models.py index 536fb50..d7c189f 100644 --- a/linkcheck/models.py +++ b/linkcheck/models.py @@ -31,8 +31,10 @@ LINKCHECK_CONNECTION_ATTEMPT_TIMEOUT, MAX_URL_LENGTH, MEDIA_PREFIX, + PROXIES, SITE_DOMAINS, TOLERATE_BROKEN_ANCHOR, + TRUST_PROXY_SSL, ) logger = logging.getLogger(__name__) @@ -386,6 +388,10 @@ def check_external(self, external_recheck_interval=EXTERNAL_RECHECK_INTERVAL): "timeout": LINKCHECK_CONNECTION_ATTEMPT_TIMEOUT, "verify": True, } + if PROXIES: + request_params["verify"] = not TRUST_PROXY_SSL + request_params["proxies"] = PROXIES + try: try: # At first try a HEAD request diff --git a/linkcheck/tests/test_linkcheck.py b/linkcheck/tests/test_linkcheck.py index 8a3f1e1..323669f 100644 --- a/linkcheck/tests/test_linkcheck.py +++ b/linkcheck/tests/test_linkcheck.py @@ -672,6 +672,25 @@ def test_external_check_blocked_user_agent_blocked_head(self): self.assertEqual(uv.redirect_to, '') self.assertEqual(uv.type, 'external') + @patch( + 'linkcheck.models.PROXIES', + {'http': 'http://proxy.example.com:8080'}, + ) + @requests_mock.Mocker() + def test_external_proxy_request(self, mocker): + mocker.register_uri('HEAD', 'http://test.com', reason='OK'), + uv = Url(url='http://test.com') + self.assertEqual(mocker.called, False) + uv.check_url() + self.assertEqual(mocker.called, True) + self.assertEqual(uv.status, True) + self.assertEqual(uv.message, '200 OK') + self.assertEqual(uv.type, 'external') + last_request = mocker.last_request + self.assertEqual(last_request.hostname, 'test.com') + self.assertEqual(last_request.scheme, 'http') + self.assertEqual(last_request.proxies, {'http': 'http://proxy.example.com:8080'}) + def test_external_check_timedout(self): uv = Url(url=f"{self.live_server_url}/timeout/") uv.check_url()