add support for happy eyeballs

This commit is contained in:
Ahmed TAHRI 2024-10-15 09:45:05 +02:00
parent 2ac67d59ff
commit 7537b40bfc
5 changed files with 41 additions and 0 deletions

View File

@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- Added support for HTTP/2, and HTTP/3 protocols. ([#523](https://github.com/httpie/cli/issues/523), [#692](https://github.com/httpie/cli/issues/692), [#1531](https://github.com/httpie/cli/pull/1531)) - Added support for HTTP/2, and HTTP/3 protocols. ([#523](https://github.com/httpie/cli/issues/523), [#692](https://github.com/httpie/cli/issues/692), [#1531](https://github.com/httpie/cli/pull/1531))
- Added support for early (informational) responses. ([#752](https://github.com/httpie/cli/issues/752)) ([#1531](https://github.com/httpie/cli/pull/1531)) - Added support for early (informational) responses. ([#752](https://github.com/httpie/cli/issues/752)) ([#1531](https://github.com/httpie/cli/pull/1531))
- Added support for IPv4/IPv6 enforcement with `-6` and `-4`. ([#94](https://github.com/httpie/cli/issues/94), [#1531](https://github.com/httpie/cli/pull/1531)) - Added support for IPv4/IPv6 enforcement with `-6` and `-4`. ([#94](https://github.com/httpie/cli/issues/94), [#1531](https://github.com/httpie/cli/pull/1531))
- Added support for Happy Eyeballs algorithm via `--heb` flag (disabled by default). [#1599](https://github.com/httpie/cli/issues/1599) [#1531](https://github.com/httpie/cli/pull/1531)
- Added support for alternative DNS resolvers via `--resolver`. DNS over HTTPS, DNS over TLS, DNS over QUIC, and DNS over UDP are accepted. ([#99](https://github.com/httpie/cli/issues/99), [#1531](https://github.com/httpie/cli/pull/1531)) - Added support for alternative DNS resolvers via `--resolver`. DNS over HTTPS, DNS over TLS, DNS over QUIC, and DNS over UDP are accepted. ([#99](https://github.com/httpie/cli/issues/99), [#1531](https://github.com/httpie/cli/pull/1531))
- Added support for binding to a specific network adapter with `--interface`. ([#1422](https://github.com/httpie/cli/issues/1422), [#1531](https://github.com/httpie/cli/pull/1531)) - Added support for binding to a specific network adapter with `--interface`. ([#1422](https://github.com/httpie/cli/issues/1422), [#1531](https://github.com/httpie/cli/pull/1531))
- Added support for specifying the local port with `--local-port`. ([#1456](https://github.com/httpie/cli/issues/1456), [#1531](https://github.com/httpie/cli/pull/1531)) - Added support for specifying the local port with `--local-port`. ([#1456](https://github.com/httpie/cli/issues/1456), [#1531](https://github.com/httpie/cli/pull/1531))

View File

@ -1970,6 +1970,17 @@ You can specify multiple entries, concatenated with a comma:
$ https --resolver "pie.dev:10.10.4.1,re.pie.dev:10.10.8.1" pie.dev/get $ https --resolver "pie.dev:10.10.4.1,re.pie.dev:10.10.8.1" pie.dev/get
``` ```
## Happy Eyeballs
By default, when HTTPie establish the connection it asks for the IP(v4 or v6) records of
the requested domain and then tries them sequentially preferring IPv6 by default. This
may induce longer connection delays and in some case hangs due to an unresponsive endpoint.
To concurrently try to connect to available IP(v4 or v6), set the following flag:
```bash
$ https --heb pie.dev/get
```
## Network interface ## Network interface
In order to bind emitted request from a specific network adapter you can use the `--interface` flag. In order to bind emitted request from a specific network adapter you can use the `--interface` flag.

View File

@ -864,6 +864,20 @@ network.add_argument(
""" """
) )
network.add_argument(
"--heb",
default=False,
dest="happy_eyeballs",
action="store_true",
short_help="Establish the connection using IETF Happy Eyeballs algorithm",
help="""
By default, when HTTPie establish the connection it asks for the IP(v4 or v6) records of
the requested domain and then tries them sequentially preferring IPv6 by default. This
may induce longer connection delays and in some case hangs due to an unresponsive endpoint.
To concurrently try to connect to available IP(v4 or v6), set this flag.
"""
)
network.add_argument( network.add_argument(
"--resolver", "--resolver",
default=[], default=[],

View File

@ -118,6 +118,7 @@ def collect_messages(
disable_ipv4=args.ipv6, disable_ipv4=args.ipv6,
source_address=source_address, source_address=source_address,
quic_cache=env.config.quic_file, quic_cache=env.config.quic_file,
happy_eyeballs=args.happy_eyeballs,
) )
if args.disable_http3 is False and args.force_http3 is True: if args.disable_http3 is False and args.force_http3 is True:
@ -237,6 +238,7 @@ def build_requests_session(
disable_ipv6: bool = False, disable_ipv6: bool = False,
source_address: typing.Tuple[str, int] = None, source_address: typing.Tuple[str, int] = None,
quic_cache: typing.Optional[Path] = None, quic_cache: typing.Optional[Path] = None,
happy_eyeballs: bool = False,
) -> niquests.Session: ) -> niquests.Session:
requests_session = niquests.Session() requests_session = niquests.Session()
@ -260,6 +262,8 @@ def build_requests_session(
source_address=source_address, source_address=source_address,
disable_http1=disable_http1, disable_http1=disable_http1,
disable_http2=disable_http2, disable_http2=disable_http2,
disable_http3=disable_http3,
happy_eyeballs=happy_eyeballs,
) )
https_adapter = HTTPieHTTPSAdapter( https_adapter = HTTPieHTTPSAdapter(
ciphers=ciphers, ciphers=ciphers,
@ -276,6 +280,7 @@ def build_requests_session(
disable_ipv6=disable_ipv6, disable_ipv6=disable_ipv6,
source_address=source_address, source_address=source_address,
quic_cache_layer=requests_session.quic_cache_layer, quic_cache_layer=requests_session.quic_cache_layer,
happy_eyeballs=happy_eyeballs,
) )
requests_session.mount('http://', http_adapter) requests_session.mount('http://', http_adapter)
requests_session.mount('https://', https_adapter) requests_session.mount('https://', https_adapter)

View File

@ -36,3 +36,13 @@ def test_ensure_interface_and_port_parameters(httpbin):
assert r.exit_status == 0 assert r.exit_status == 0
assert HTTP_OK in r assert HTTP_OK in r
def test_happy_eyeballs(remote_httpbin_secure):
r = http(
"--heb", # this will automatically and concurrently try IPv6 and IPv4 endpoints
remote_httpbin_secure + "/get",
)
assert r.exit_status == 0
assert HTTP_OK in r