Skip to content

Commit 573a021

Browse files
committed
Move HTTP version options validation to the CLI layer
1 parent 2ea86b2 commit 573a021

File tree

3 files changed

+38
-33
lines changed

3 files changed

+38
-33
lines changed

httpie/cli/argparser.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ def parse_args(
169169
)
170170
# Arguments processing and environment setup.
171171
self._apply_no_options(no_options)
172+
self._process_http_versions()
172173
self._process_request_type()
173174
self._process_download_options()
174175
self._setup_standard_streams()
@@ -213,6 +214,38 @@ def parse_args(
213214

214215
return self.args
215216

217+
def _process_http_versions(self):
218+
available, forced, disabled = (
219+
{1, 2, 3},
220+
{
221+
self.args.force_http1 and 1,
222+
self.args.force_http2 and 2,
223+
self.args.force_http3 and 3,
224+
} - {False},
225+
{
226+
self.args.disable_http1 and 1,
227+
self.args.disable_http2 and 2,
228+
self.args.disable_http3 and 3,
229+
} - {False},
230+
)
231+
if forced and disabled:
232+
self.error(
233+
'You cannot both force a http protocol version and disable some other. e.g. '
234+
'--http2 already force HTTP/2, do not use --disable-http1 at the same time.'
235+
)
236+
if len(forced) > 1:
237+
self.error(
238+
'You may only force one of --http1, --http2 or --http3. Use --disable-http1, '
239+
'--disable-http2 or --disable-http3 instead if you prefer the excluding logic.'
240+
)
241+
if disabled == available:
242+
self.error('At least one HTTP protocol version must be enabled.')
243+
244+
if forced:
245+
self.args.disable_http1 = forced != {1}
246+
self.args.disable_http2 = forced != {2}
247+
self.args.disable_http3 = forced != {3}
248+
216249
def _process_request_type(self):
217250
request_type = self.args.request_type
218251
self.args.json = request_type is RequestType.JSON

httpie/client.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -73,41 +73,11 @@ def collect_messages(
7373
# We want to make sure every ".localhost" host resolve to loopback
7474
if parsed_url.host and parsed_url.host.endswith(".localhost"):
7575
ensure_resolver = f"in-memory://default/?hosts={parsed_url.host}:127.0.0.1&hosts={parsed_url.host}:[::1]"
76-
7776
if resolver and isinstance(resolver, list):
7877
resolver.append(ensure_resolver)
7978
else:
8079
resolver = [ensure_resolver, "system://"]
8180

82-
force_opt_count = [args.force_http1, args.force_http2, args.force_http3].count(True)
83-
disable_opt_count = [args.disable_http1, args.disable_http2, args.disable_http3].count(True)
84-
85-
if force_opt_count > 1:
86-
raise ValueError(
87-
'You may only force one of --http1, --http2 or --http3. Use --disable-http1, '
88-
'--disable-http2 or --disable-http3 instead if you prefer the excluding logic.'
89-
)
90-
elif force_opt_count == 1 and disable_opt_count:
91-
raise ValueError(
92-
'You cannot both force a http protocol version and disable some other. e.g. '
93-
'--http2 already force HTTP/2, do not use --disable-http1 at the same time.'
94-
)
95-
96-
if args.force_http1:
97-
args.disable_http1 = False
98-
args.disable_http2 = True
99-
args.disable_http3 = True
100-
101-
if args.force_http2:
102-
args.disable_http1 = True
103-
args.disable_http2 = False
104-
args.disable_http3 = True
105-
106-
if args.force_http3:
107-
args.disable_http1 = True
108-
args.disable_http2 = True
109-
args.disable_http3 = False
110-
11181
requests_session = build_requests_session(
11282
ssl_version=args.ssl_version,
11383
ciphers=args.ciphers,

tests/test_h2n3.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_disable_all_error_https(remote_httpbin_secure):
6868
tolerate_error_exit_status=True,
6969
)
7070

71-
assert 'You disabled every supported protocols.' in r.stderr
71+
assert 'At least one HTTP protocol version must be enabled.' in r.stderr
7272

7373

7474
def test_disable_all_error_http(remote_httpbin):
@@ -94,8 +94,10 @@ def test_disable_all_error_http(remote_httpbin):
9494
def with_quic_cache_persistent(tmp_path):
9595
env = PersistentMockEnvironment()
9696
env.config['quic_file'] = tmp_path / 'quic.json'
97-
yield env
98-
env.cleanup(force=True)
97+
try:
98+
yield env
99+
finally:
100+
env.cleanup(force=True)
99101

100102

101103
@pytest.mark.skipif(qh3 is None, reason="test require HTTP/3 support")

0 commit comments

Comments
 (0)