From 745f75026343a56db4fb916bcf7dce292acbfa46 Mon Sep 17 00:00:00 2001 From: Kim Christie Date: Tue, 16 Dec 2025 17:17:12 +0000 Subject: [PATCH 1/4] ValueError for invalid request URLs. When instantiating a request... * Raise ValueError for URLs with a non-HTTP scheme. * Raise ValueError for URLs missing a hostname. --- tests/test_request.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_request.py b/tests/test_request.py index a69e1d1..5b777c2 100644 --- a/tests/test_request.py +++ b/tests/test_request.py @@ -77,3 +77,13 @@ def test_request_empty_post(): "Content-Length": "0", } assert r.read() == b'' + + +def test_request_invalid_scheme(): + with pytest.raises(ValueError): + httpx.Request("GET", "ws://example.com") + + +def test_request_missing_host(): + with pytest.raises(ValueError): + r = httpx.Request("GET", "https:/example.com") From e59239d62cc9be3c3f8c38f2267d6b5372510d99 Mon Sep 17 00:00:00 2001 From: Kim Christie Date: Tue, 16 Dec 2025 17:28:41 +0000 Subject: [PATCH 2/4] Validate URL scheme and host in request Add validation for URL scheme and host presence. --- src/ahttpx/_request.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ahttpx/_request.py b/src/ahttpx/_request.py index 78b8228..a247b8b 100644 --- a/src/ahttpx/_request.py +++ b/src/ahttpx/_request.py @@ -29,6 +29,11 @@ def __init__( if "Host" not in self.headers: self.headers = self.headers.copy_set("Host", self.url.netloc) + if self.url.scheme not in ('http', 'https'): + raise ValueError(f'Invalid scheme for URL {str(self.url)!r}.') + if not self.url.netloc: + raise ValueError(f'Missing host for URL {str(self.url)!r}.') + if content is not None: if isinstance(content, bytes): self.stream = ByteStream(content) From adee3371ba565ff72ae72e95d0f4fd0215c03907 Mon Sep 17 00:00:00 2001 From: Kim Christie Date: Tue, 16 Dec 2025 17:29:40 +0000 Subject: [PATCH 3/4] Validate URL scheme and ensure host is present Add validation for URL scheme and host presence --- src/httpx/_request.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/httpx/_request.py b/src/httpx/_request.py index 1b739b1..a8b9a32 100644 --- a/src/httpx/_request.py +++ b/src/httpx/_request.py @@ -29,6 +29,11 @@ def __init__( if "Host" not in self.headers: self.headers = self.headers.copy_set("Host", self.url.netloc) + if self.url.scheme not in ('http', 'https'): + raise ValueError(f'Invalid scheme for URL {str(self.url)!r}.') + if not self.url.netloc: + raise ValueError(f'Missing host for URL {str(self.url)!r}.') + if content is not None: if isinstance(content, bytes): self.stream = ByteStream(content) From b3cb1d11e2dccad58f1af0f6426ef04eaf1ce2e2 Mon Sep 17 00:00:00 2001 From: Kim Christie Date: Tue, 16 Dec 2025 17:32:27 +0000 Subject: [PATCH 4/4] Add pytest import to test_request.py --- tests/test_request.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_request.py b/tests/test_request.py index 5b777c2..cab0a5e 100644 --- a/tests/test_request.py +++ b/tests/test_request.py @@ -1,4 +1,5 @@ import httpx +import pytest class ByteIterator: