diff --git a/RELEASE.md b/RELEASE.md index 8604b96b..dd18c351 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -5,6 +5,7 @@ - Unknown keyword arguments now raise `TypeError` when calling async or blocking client constructors, HTTP or WebSocket request methods and functions, `Http1Options`, `Http2Options`, `TlsOptions`, or `Proxy` factories. These arguments were previously ignored. - Set client-only options such as `connect_timeout` and `proxies` on `Client`; passing them to a request now raises `TypeError`. Use `proxy` to select a proxy for one request. - Async request calls check keyword names before returning a coroutine. Values for supported request keywords are still converted on first `await` or entry with `async with`. +- System DNS is consistently used by default. Previously, the behavior differed between no kwargs and any unrelated kwarg being set. ## [0.11.0](https://github.com/0x676e67/wreq-python/compare/v0.10.2..v0.11.0) - 2026-04-13 diff --git a/docs/source/guide/advanced.md b/docs/source/guide/advanced.md index c1a815c2..62f2f750 100644 --- a/docs/source/guide/advanced.md +++ b/docs/source/guide/advanced.md @@ -129,15 +129,15 @@ enable logging only when you need it. ## DNS and protocol options `dns_options` selects the resolver and can override specific host addresses. -Overrides retain the URL's hostname for HTTP and TLS. This configuration uses -system DNS; omit it to use the default Hickory resolver: +Overrides retain the URL's hostname for HTTP and TLS. Set `system_dns=False` to +use the Hickory resolver instead of system DNS (such as `getaddrinfo` on Linux). ```python from wreq.blocking import Client from wreq.dns import DnsOptions -with Client(dns_options=DnsOptions(system_dns=True)) as client: +with Client(dns_options=DnsOptions(system_dns=False)) as client: with client.get("https://httpbin.io/get") as response: print(response.text()) ``` diff --git a/python/wreq/dns.py b/python/wreq/dns.py index 3690cc58..9843fc64 100644 --- a/python/wreq/dns.py +++ b/python/wreq/dns.py @@ -52,7 +52,7 @@ class DnsOptions: def __init__( self, - system_dns: bool = False, + system_dns: bool = True, lookup_ip_strategy: LookupIpStrategy = LookupIpStrategy.IPV4_AND_IPV6, ) -> None: """Create a new DnsOptions. diff --git a/src/client.rs b/src/client.rs index cc1e2c3e..f3f566c4 100644 --- a/src/client.rs +++ b/src/client.rs @@ -25,7 +25,7 @@ use self::{ use crate::{ cookie::Jar, coroutine::{self, Coroutine}, - dns::{DnsOptions, HickoryResolver, LookupIpStrategy}, + dns::{DnsOptions, HickoryResolver}, emulate::EmulationLike, error::Error, extractor::Extractor, @@ -488,10 +488,7 @@ impl Client { builder = builder.dns_resolver(HickoryResolver::new(opts.lookup_ip_strategy)?); } - } else { - builder = - builder.dns_resolver(HickoryResolver::new(LookupIpStrategy::default())?); - }; + } // Compression options. apply_option!(set_if_some, builder, config.gzip, gzip); diff --git a/src/dns.rs b/src/dns.rs index 704357c0..3cb2a94f 100644 --- a/src/dns.rs +++ b/src/dns.rs @@ -52,7 +52,7 @@ impl DnsOptions { /// `lookup_ip_strategy` only applies to hickory and has no effect when /// using the system DNS resolver. #[new] - #[pyo3(signature=(system_dns = false, lookup_ip_strategy = LookupIpStrategy::IPV4_AND_IPV6))] + #[pyo3(signature=(system_dns = true, lookup_ip_strategy = LookupIpStrategy::IPV4_AND_IPV6))] pub fn new(system_dns: bool, lookup_ip_strategy: LookupIpStrategy) -> Self { DnsOptions { system_dns, diff --git a/tests/dns_test.py b/tests/dns_test.py index b1461f4d..1f15f285 100644 --- a/tests/dns_test.py +++ b/tests/dns_test.py @@ -1,10 +1,11 @@ import asyncio +import platform import threading +from datetime import timedelta from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from ipaddress import IPv4Address import pytest - import wreq from wreq import blocking from wreq.dns import DnsOptions, LookupIpStrategy @@ -46,21 +47,26 @@ async def fetch(host, options): if blocking_api: def request(): - with blocking.Client(**kwargs) as client: - with client.get(url) as response: - return response.text() + with blocking.Client(**kwargs) as client, client.get(url) as response: + return response.text() body = await asyncio.to_thread(request) else: - async with wreq.Client(**kwargs) as client: - async with client.get(url) as response: - body = await response.text() + async with wreq.Client(**kwargs) as client, client.get(url) as response: + body = await response.text() assert body == "DNS resolved" # Exercise actual hostname resolution, including the default resolver. await fetch("localhost", None) await fetch("localhost", DnsOptions(system_dns=True)) - await fetch("localhost", DnsOptions(lookup_ip_strategy=LookupIpStrategy.IPV4_ONLY)) + await fetch( + "localhost", + DnsOptions(system_dns=False, lookup_ip_strategy=LookupIpStrategy.IPV4_ONLY), + ) + + options = DnsOptions(system_dns=True) + options.add_resolve("dns-test.invalid", [IPv4Address("127.0.0.1")]) + await fetch("dns-test.invalid", options) # Every strategy must initialize and preserve explicit address overrides. for strategy in ( @@ -70,7 +76,7 @@ def request(): LookupIpStrategy.IPV6_THEN_IPV4, LookupIpStrategy.IPV4_THEN_IPV6, ): - options = DnsOptions(lookup_ip_strategy=strategy) + options = DnsOptions(system_dns=False, lookup_ip_strategy=strategy) options.add_resolve("dns-test.invalid", [IPv4Address("127.0.0.1")]) await fetch("dns-test.invalid", options) @@ -78,9 +84,53 @@ def request(): @pytest.mark.asyncio async def test_dns_resolve_override(dns_http_server): # An override wins over the real address of a resolvable host. - dns_options = DnsOptions(lookup_ip_strategy=LookupIpStrategy.IPV4_ONLY) + dns_options = DnsOptions( + system_dns=False, lookup_ip_strategy=LookupIpStrategy.IPV4_ONLY + ) dns_options.add_resolve("www.google.com", [IPv4Address("127.0.0.1")]) async with wreq.Client(dns_options=dns_options, no_proxy=True) as client: url = f"http://www.google.com:{dns_http_server}/" async with await asyncio.wait_for(client.get(url), 5) as response: assert await response.text() == "DNS resolved" + + +async def invalid_name_resolve_error(client: wreq.Client | blocking.Client) -> str: + # A 64-byte label is not a valid DNS name, so every resolver fails without + # sending a query, each with its own error. + url = f"http://{'a' * 64}.invalid/" + + def request_blocking(blocking_client: blocking.Client) -> None: + with blocking_client, blocking_client.get(url, timeout=timedelta(seconds=5)): + pass + + with pytest.raises(wreq.exceptions.ConnectionError) as exc_info: + if isinstance(client, blocking.Client): + await asyncio.to_thread(request_blocking, client) + else: + async with client, await asyncio.wait_for(client.get(url), 5): + pass + return str(exc_info.value) + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "use_dns_options", [False, True], ids=["no_kwargs", "dns_options"] +) +@pytest.mark.parametrize( + "client_type", [wreq.Client, blocking.Client], ids=["async", "blocking"] +) +async def test_default_dns_resolver_is_getaddrinfo( + use_dns_options: bool, client_type: type[wreq.Client | blocking.Client] +): + # Control: hickory rejects the name while parsing it. If this stops holding, + # the assertions below no longer tell the resolvers apart. + hickory_client = client_type(dns_options=DnsOptions(system_dns=False)) + assert "LabelBytesTooLong" in await invalid_name_resolve_error(hickory_client) + + client = client_type(dns_options=DnsOptions()) if use_dns_options else client_type() + error = await invalid_name_resolve_error(client) + assert "LabelBytesTooLong" not in error + if platform.system() != "Windows": + # std's getaddrinfo wrapper prefixes every EAI_* failure with this; + # Windows reports a localized WSA error instead. + assert "failed to lookup address information" in error