feat: Add IPv6 zone ID detection and parsing support in HTTPAdapter
Summary
This PR possibly solves https://github.com/psf/requests/pull/6927 and https://github.com/psf/requests/issues/6735 by adding support for IPv6 zone identifiers (also known as scope IDs) in URLs and enabling proper handling of link-local IPv6 addresses with zone identifiers like http://[fe80::1%eth0]/.
Background
IPv6 zone identifiers are used to disambiguate link-local addresses (addresses in the fe80::/10 range) that may exist on multiple network interfaces. The zone ID is appended to the address with a % delimiter, such as fe80::1%eth0. Previously, requests did not properly handle these URLs, which could lead to connection issues or parsing errors.
Changes Made
-
New helper function: Added
_has_ipv6_zone_id()to detect URLs containing IPv6 zone identifiers -
Enhanced URL parsing: Updated
_urllib3_request_context()to useurllib3'sparse_urlfor URLs with zone IDs, ensuring correct parsing while maintaining backward compatibility for standard URLs -
Comprehensive test coverage: Added extensive unit and integration tests in
test_adapters.pycovering:- Detection of various IPv6 zone ID formats
- Correct parsing of URLs with zone identifiers
- Connection pool key generation for different zone IDs
- Edge cases and error scenarios
Files Changed
-
src/requests/adapters.py- Core implementation (+54 -4 lines) -
src/requests/models.py- Core implementation (+9 lines) -
tests/test_adapters.py- Test suite (+292 lines)
Testing
All new functionality is covered by comprehensive tests that verify:
- IPv6 zone ID detection logic
- Correct URL parsing behavior
- Connection pool isolation based on zone IDs
- Backward compatibility with existing URL handling
Backward Compatibility
This change is fully backward compatible. Standard URLs without IPv6 zone identifiers continue to use the existing parsing logic, while only URLs with detected zone IDs utilize the enhanced urllib3 parsing pathway.