From: kimbo Date: Mon, 30 Dec 2019 23:22:51 +0000 (-0700) Subject: send_https() and test for it X-Git-Tag: v2.0.0rc1~342^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F405%2Fhead;p=thirdparty%2Fdnspython.git send_https() and test for it --- diff --git a/dns/query.py b/dns/query.py index 8f4b1c25..c36248e3 100644 --- a/dns/query.py +++ b/dns/query.py @@ -38,6 +38,7 @@ 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 @@ -210,6 +211,19 @@ def _destination_and_source(af, where, port, source, source_port): source = (source, source_port, 0, 0) return (af, destination, source) +def send_https(session, what, lifetime=None): + """ + :param session: a :class:`requests.sessions.Session` + :param what: a :class:`requests.models.Request` or + :class:`requests.models.PreparedRequest`. + If it's a :class:`requests.models.Request`, it will be converted + into a :class:`requests.models.PreparedRequest`. + :param lifetime: timeout (in seconds) + :return: a :class:`requests.models.Response` object. + """ + if isinstance(what, requests.models.Request): + what = what.prepare() + return session.send(what, timeout=lifetime) def https(q, where, session, timeout=None, port=443, path='/dns-query', post=True, bootstrap_address=None, verify=True, source=None, source_port=0, diff --git a/tests/test_doh.py b/tests/test_doh.py index 481ca0c0..3819b1a4 100644 --- a/tests/test_doh.py +++ b/tests/test_doh.py @@ -14,6 +14,7 @@ # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +import base64 import unittest import random @@ -68,5 +69,15 @@ class DNSOverHTTPSTestCase(unittest.TestCase): r = dns.query.https(q, valid_tls_url, self.session, bootstrap_address=ip) self.assertTrue(q.is_response(r)) + def test_send_https(self): + q = dns.message.make_query('example.com.', dns.rdatatype.A) + wire = q.to_wire() + query_string = '?dns={}'.format(base64.urlsafe_b64encode(wire).decode('utf-8').strip("=")) + request = requests.models.Request('GET', 'https://cloudflare-dns.com/dns-query{}'.format(query_string)) + r = request.prepare() + response = dns.query.send_https(self.session, r) + dns_resp = dns.message.from_wire(response.content) + self.assertTrue(q.is_response(dns_resp)) + if __name__ == '__main__': unittest.main()