Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jkbrzt committed Oct 26, 2024
1 parent 65e4422 commit b58d001
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
7 changes: 4 additions & 3 deletions httpie/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,15 @@ def collect_messages(
happy_eyeballs=args.happy_eyeballs,
)

if args.disable_http3 is False and args.force_http3 is True:
if not args.disable_http3 and args.force_http3:
requests_session.quic_cache_layer[(parsed_url.host, parsed_url.port or 443)] = (parsed_url.host, parsed_url.port or 443)
# well, this one is tricky. If we allow HTTP/3, and remote host was marked as QUIC capable
# but is not anymore, we may face an indefinite hang if timeout isn't set. This could surprise some user.
elif (
args.disable_http3 is False
not args.disable_http3
and not args.force_http3
and requests_session.quic_cache_layer.get((parsed_url.host, parsed_url.port or 443)) is not None
and args.force_http3 is False

):
# we only set the connect timeout, the rest is still indefinite.
if send_kwargs["timeout"] is None:
Expand Down
43 changes: 21 additions & 22 deletions httpie/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
from typing import Any, Optional, Iterable

from httpie.cookies import HTTPieCookiePolicy
from http import cookiejar # noqa
from http import cookiejar # noqa

import niquests
from niquests._compat import HAS_LEGACY_URLLIB3


# to understand why this is required
# see https://niquests.readthedocs.io/en/latest/community/faq.html#what-is-urllib3-future
# short story, urllib3 (import/top-level import) may be the legacy one https://github.com/urllib3/urllib3
Expand All @@ -31,6 +32,25 @@
resolve_ssl_version,
)

# Importlib_metadata was a provisional module, so the APIs changed quite a few times
# between 3.8-3.10. It was also not included in the standard library until 3.8, so
# we install the backport for <3.8.
if sys.version_info >= (3, 8):
import importlib.metadata as importlib_metadata
else:
import importlib_metadata

is_windows = 'win32' in str(sys.platform).lower()
is_frozen = getattr(sys, 'frozen', False)

MIN_SUPPORTED_PY_VERSION = (3, 7)
MAX_SUPPORTED_PY_VERSION = (3, 11)

# Request does not carry the original policy attached to the
# cookie jar, so until it is resolved we change the global cookie
# policy. <https://github.com/psf/requests/issues/5449>
cookiejar.DefaultCookiePolicy = HTTPieCookiePolicy


def has_ipv6_support(new_value: Optional[bool] = None) -> bool:
if new_value is not None:
Expand All @@ -52,17 +72,6 @@ def enforce_niquests():
sys.modules["requests.exceptions"] = niquests.exceptions
sys.modules["requests.packages.urllib3"] = urllib3

# Request does not carry the original policy attached to the
# cookie jar, so until it is resolved we change the global cookie
# policy. <https://github.com/psf/requests/issues/5449>
cookiejar.DefaultCookiePolicy = HTTPieCookiePolicy


is_windows = 'win32' in str(sys.platform).lower()
is_frozen = getattr(sys, 'frozen', False)

MIN_SUPPORTED_PY_VERSION = (3, 7)
MAX_SUPPORTED_PY_VERSION = (3, 11)

try:
from functools import cached_property
Expand Down Expand Up @@ -114,16 +123,6 @@ def __get__(self, instance, cls=None):
return res


# importlib_metadata was a provisional module, so the APIs changed quite a few times
# between 3.8-3.10. It was also not included in the standard library until 3.8, so
# we install the backport for <3.8.

if sys.version_info >= (3, 8):
import importlib.metadata as importlib_metadata
else:
import importlib_metadata


def find_entry_points(entry_points: Any, group: str) -> Iterable[importlib_metadata.EntryPoint]:
if hasattr(entry_points, "select"): # Python 3.10+ / importlib_metadata >= 3.9.0
return entry_points.select(group=group)
Expand Down
18 changes: 9 additions & 9 deletions tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ def test_non_existent_interface_arg(httpbin):
('-0-8000', (0, 8000)),
('0-0', (0, 0)),
# Port ranges — invalid
(f'2-1', 'not a valid port range'),
(f'2-', 'not a number'),
(f'2-A', 'not a number'),
(f'A-A', 'not a number'),
(f'A-2', 'not a number'),
(f'-10-1', OUTSIDE_VALID_PORT_RANGE_ERROR),
(f'1--1', OUTSIDE_VALID_PORT_RANGE_ERROR),
(f'-10--1', OUTSIDE_VALID_PORT_RANGE_ERROR),
(f'1-{MAX_PORT + 1}', OUTSIDE_VALID_PORT_RANGE_ERROR),
('2-1', 'not a valid port range'),
('2-', 'not a number'),
('2-A', 'not a number'),
('A-A', 'not a number'),
('A-2', 'not a number'),
('-10-1', OUTSIDE_VALID_PORT_RANGE_ERROR),
('1--1', OUTSIDE_VALID_PORT_RANGE_ERROR),
('-10--1', OUTSIDE_VALID_PORT_RANGE_ERROR),
('1-{MAX_PORT + 1}', OUTSIDE_VALID_PORT_RANGE_ERROR),
])
def test_parse_local_port_arg(local_port_arg, expected_output):
expected_error = expected_output if isinstance(expected_output, str) else None
Expand Down

0 comments on commit b58d001

Please sign in to comment.