]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
make DOH an extra feature (to avoid requests as hard dependency)
authorDaniel Lenski <dlenski@amazon.com>
Fri, 1 May 2020 00:31:28 +0000 (17:31 -0700)
committerDaniel Lenski <dlenski@amazon.com>
Fri, 1 May 2020 00:35:27 +0000 (17:35 -0700)
dns/query.py
doc/exceptions.rst
setup.py
tests/test_doh.py

index 725eff19627a101b429baff76018073f33327dd6..174a7fb7eb08845bbd48616e7d1118ad46fc3f81 100644 (file)
@@ -38,9 +38,14 @@ import dns.rcode
 import dns.rdataclass
 import dns.rdatatype
 
-import requests
-from requests_toolbelt.adapters.source import SourceAddressAdapter
-from requests_toolbelt.adapters.host_header_ssl import HostHeaderSSLAdapter
+try:
+    import requests
+    from requests_toolbelt.adapters.source import SourceAddressAdapter
+    from requests_toolbelt.adapters.host_header_ssl import HostHeaderSSLAdapter
+    have_doh = True
+except ImportError:
+    have_doh = False
+
 
 try:
     import ssl
@@ -76,6 +81,11 @@ class TransferError(dns.exception.DNSException):
         self.rcode = rcode
 
 
+class NoDOH(dns.exception.DNSException):
+    """DNS over HTTPS (DOH) was requested but the requests module is not
+    available."""
+
+
 def _compute_expiration(timeout):
     if timeout is None:
         return None
@@ -226,6 +236,8 @@ def send_https(session, what, lifetime=None):
     :param lifetime: timeout (in seconds)
     :return: a :class:`requests.models.Response` object.
     """
+    if not have_doh:
+        raise NoDOH
     if isinstance(what, requests.models.Request):
         what = what.prepare()
     return session.send(what, timeout=lifetime)
@@ -280,6 +292,9 @@ def https(q, where, timeout=None, port=443, af=None, source=None, source_port=0,
     Returns a ``dns.message.Message``.
     """
 
+    if not have_doh:
+        raise NoDOH
+
     wire = q.to_wire()
     (af, destination, source) = _destination_and_source(af, where, port,
                                                         source, source_port,
index b7054822d002452602b59810030d207aab167a4c..b227e15f58b1db7d46d98ebf04a927060fcfbe18 100644 (file)
@@ -50,6 +50,7 @@ dns.query Exceptions
 --------------------
 
 .. autoexception:: dns.query.BadResponse
+.. autoexception:: dns.query.NoDOH
 .. autoexception:: dns.query.UnexpectedSource
 .. autoexception:: dns.query.TransferError
 
index 16fe7560077dd3c438385bc78330ceb213a82686..6e0c945a5d69c1ef8c791447618b9902ae7cc39f 100755 (executable)
--- a/setup.py
+++ b/setup.py
@@ -71,9 +71,9 @@ direct manipulation of DNS zones, messages, names, and records.""",
     'python_requires': '>=3.4',
     'test_suite': 'tests',
     'provides': ['dns'],
-    'install_requires': ['requests', 'requests-toolbelt'],
     'tests_require': ['typing ; python_version<"3.5"'],
     'extras_require': {
+        'DOH': ['requests', 'requests-toolbelt'],
         'IDNA': ['idna>=2.1'],
         'DNSSEC': ['cryptography>=2.6'],
         },
index 41d8331cc8258246a09d8d0ee289f512a04ef2fb..2e16aa2318f814e4ccf561ac231f1af6522fb5f4 100644 (file)
@@ -30,6 +30,8 @@ KNOWN_ANYCAST_DOH_RESOLVER_URLS = ['https://cloudflare-dns.com/dns-query',
                                    'https://dns.google/dns-query',
                                    'https://dns11.quad9.net/dns-query']
 
+@unittest.skipUnless(dns.query.have_doh,
+                     "Python requests cannot be imported; no DNS over HTTPS (DOH)")
 class DNSOverHTTPSTestCase(unittest.TestCase):
     def setUp(self):
         self.session = requests.sessions.Session()