## [Unreleased]
-This release introduces an `httpx.SSLContext()` class and `ssl_context` parameter.
+The 0.28 release includes a limited set of backwards incompatible changes.
-* Added `httpx.SSLContext` class and `ssl_context` parameter. (#3022, #3335)
-* The `verify` and `cert` arguments have been deprecated and will now raise warnings. (#3022, #3335)
+**Backwards incompatible changes**:
+
+SSL configuration has been significantly simplified.
+
+* The `verify` argument no longer accepts string arguments.
+* The `cert` argument has now been removed.
+* The `SSL_CERT_FILE` and `SSL_CERT_DIR` environment variables are no longer automatically used.
+
+For users of the standard `verify=True` or `verify=False` cases this should require no changes.
+
+For information on configuring more complex SSL cases, please see the [SSL documentation](docs/advanced/ssl.md).
+
+**The following changes are also included**:
+
+* The undocumented `URL.raw` property has now been deprecated, and will raise warnings.
* The deprecated `proxies` argument has now been removed.
* The deprecated `app` argument has now been removed.
-* The `URL.raw` property has now been removed.
* Ensure JSON request bodies are compact. (#3363)
* Review URL percent escape sets, based on WHATWG spec. (#3371, #3373)
* Ensure `certifi` and `httpcore` are only imported if required. (#3377)
httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:997)
```
-Verification is configured through [the SSL Context API](https://docs.python.org/3/library/ssl.html#ssl-contexts).
+You can disable SSL verification completely and allow insecure requests...
```pycon
->>> context = httpx.SSLContext()
->>> context
-<SSLContext(verify=True)>
->>> httpx.get("https://www.example.com", ssl_context=context)
-httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:997)
-```
-
-You can use this to disable verification completely and allow insecure requests...
-
-```pycon
->>> context = httpx.SSLContext(verify=False)
->>> context
-<SSLContext(verify=False)>
->>> httpx.get("https://expired.badssl.com/", ssl_context=context)
+>>> httpx.get("https://expired.badssl.com/", verify=False)
<Response [200 OK]>
```
### Configuring client instances
-If you're using a `Client()` instance you should pass any SSL context when instantiating the client.
-
-```pycon
->>> context = httpx.SSLContext()
->>> client = httpx.Client(ssl_context=context)
-```
-
-The `client.get(...)` method and other request methods on a `Client` instance *do not* support changing the SSL settings on a per-request basis.
-
-If you need different SSL settings in different cases you should use more than one client instance, with different settings on each. Each client will then be using an isolated connection pool with a specific fixed SSL configuration on all connections within that pool.
-
-### Configuring certificate stores
-
-By default, HTTPX uses the CA bundle provided by [Certifi](https://pypi.org/project/certifi/).
-
-You can load additional certificate verification using the [`.load_verify_locations()`](https://docs.python.org/3/library/ssl.html#ssl.SSLContext.load_verify_locations) API:
-
-```pycon
->>> context = httpx.SSLContext()
->>> context.load_verify_locations(cafile="path/to/certs.pem")
->>> client = httpx.Client(ssl_context=context)
->>> client.get("https://www.example.com")
-<Response [200 OK]>
-```
-
-Or by providing an certificate directory:
-
-```pycon
->>> context = httpx.SSLContext()
->>> context.load_verify_locations(capath="path/to/certs")
->>> client = httpx.Client(ssl_context=context)
->>> client.get("https://www.example.com")
-<Response [200 OK]>
-```
+If you're using a `Client()` instance you should pass any `verify=<...>` configuration when instantiating the client.
-### Client side certificates
+By default the [certifi CA bundle](https://certifiio.readthedocs.io/en/latest/) is used for SSL verification.
-You can also specify a local cert to use as a client-side certificate, using the [`.load_cert_chain()`](https://docs.python.org/3/library/ssl.html#ssl.SSLContext.load_cert_chain) API:
+For more complex configurations you can pass an [SSL Context](https://docs.python.org/3/library/ssl.html) instance...
-```pycon
->>> context = httpx.SSLContext()
->>> context.load_cert_chain(certfile="path/to/client.pem")
->>> httpx.get("https://example.org", ssl_context=ssl_context)
-<Response [200 OK]>
-```
-
-Or including a keyfile...
-
-```pycon
->>> context = httpx.SSLContext()
->>> context.load_cert_chain(
- certfile="path/to/client.pem",
- keyfile="path/to/client.key"
- )
->>> httpx.get("https://example.org", ssl_context=context)
-<Response [200 OK]>
-```
-
-Or including a keyfile and password...
+```python
+import certifi
+import httpx
+import ssl
-```pycon
->>> context = httpx.SSLContext(cert=cert)
->>> context = httpx.SSLContext()
->>> context.load_cert_chain(
- certfile="path/to/client.pem",
- keyfile="path/to/client.key"
- password="password"
- )
->>> httpx.get("https://example.org", ssl_context=context)
-<Response [200 OK]>
+# This SSL context is equivelent to the default `verify=True`.
+ctx = ssl.create_default_context(cafile=certifi.where())
+client = httpx.Client(verify=ctx)
```
-### Using alternate SSL contexts
-
-You can also use an alternate `ssl.SSLContext` instances.
-
-For example, [using the `truststore` package](https://truststore.readthedocs.io/)...
+Using [the `truststore` package](https://truststore.readthedocs.io/) to support system certificate stores...
```python
import ssl
import truststore
import httpx
-ssl_context = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
-client = httpx.Client(ssl_context=ssl_context)
+# Use system certificate stores.
+ctx = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
+client = httpx.Client(verify=ctx)
```
-Or working [directly with Python's standard library](https://docs.python.org/3/library/ssl.html)...
+Loding an alternative certificate verification store using [the standard SSL context API](https://docs.python.org/3/library/ssl.html)...
```python
-import ssl
import httpx
+import ssl
-ssl_context = ssl.create_default_context()
-client = httpx.Client(ssl_context=ssl_context)
+# Use an explicitly configured certificate store.
+ctx = ssl.create_default_context(cafile="path/to/certs.pem") # Either cafile or capath.
+client = httpx.Client(verify=ctx)
```
-### Working with `SSL_CERT_FILE` and `SSL_CERT_DIR`
+### Client side certificates
-Unlike `requests`, the `httpx` package does not automatically pull in [the environment variables `SSL_CERT_FILE` or `SSL_CERT_DIR`](https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_default_verify_paths.html). If you want to use these they need to be enabled explicitly.
+Client side certificates allow a remote server to verify the client. They tend to be used within private organizations to authenticate requests to remote servers.
-For example...
+You can specify client-side certificates, using the [`.load_cert_chain()`](https://docs.python.org/3/library/ssl.html#ssl.SSLContext.load_cert_chain) API...
```python
-context = httpx.SSLContext()
-
-# Use `SSL_CERT_FILE` or `SSL_CERT_DIR` if configured.
-if os.environ.get("SSL_CERT_FILE") or os.environ.get("SSL_CERT_DIR"):
- context.load_verify_locations(
- cafile=os.environ.get("SSL_CERT_FILE"),
- capath=os.environ.get("SSL_CERT_DIR"),
- )
+ctx = ssl.create_default_context()
+ctx.load_cert_chain(certfile="path/to/client.pem") # Optionally also keyfile or password.
+client = httpx.Client(verify=ctx)
```
-## `SSLKEYLOGFILE`
-
-Valid values: a filename
-
-If this environment variable is set, TLS keys will be appended to the specified file, creating it if it doesn't exist, whenever key material is generated or received. The keylog file is designed for debugging purposes only.
+### Working with `SSL_CERT_FILE` and `SSL_CERT_DIR`
-Support for `SSLKEYLOGFILE` requires Python 3.8 and OpenSSL 1.1.1 or newer.
+Unlike `requests`, the `httpx` package does not automatically pull in [the environment variables `SSL_CERT_FILE` or `SSL_CERT_DIR`](https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_default_verify_paths.html). If you want to use these they need to be enabled explicitly.
-Example:
+For example...
```python
-# test_script.py
-import httpx
-
-with httpx.Client() as client:
- r = client.get("https://google.com")
-```
-
-```console
-SSLKEYLOGFILE=test.log python test_script.py
-cat test.log
-# TLS secrets log file, generated by OpenSSL / Python
-SERVER_HANDSHAKE_TRAFFIC_SECRET XXXX
-EXPORTER_SECRET XXXX
-SERVER_TRAFFIC_SECRET_0 XXXX
-CLIENT_HANDSHAKE_TRAFFIC_SECRET XXXX
-CLIENT_TRAFFIC_SECRET_0 XXXX
-SERVER_HANDSHAKE_TRAFFIC_SECRET XXXX
-EXPORTER_SECRET XXXX
-SERVER_TRAFFIC_SECRET_0 XXXX
-CLIENT_HANDSHAKE_TRAFFIC_SECRET XXXX
-CLIENT_TRAFFIC_SECRET_0 XXXX
+# Use `SSL_CERT_FILE` or `SSL_CERT_DIR` if configured.
+# Otherwise default to certifi.
+ctx = ssl.create_default_context(
+ cafile=os.environ.get("SSL_CERT_FILE", certifi.where()),
+ capath=os.environ.get("SSL_CERT_DIR"),
+)
+client = httpx.Client(verify=ctx)
```
### Making HTTPS requests to a local server
When making requests to local servers, such as a development server running on `localhost`, you will typically be using unencrypted HTTP connections.
-If you do need to make HTTPS connections to a local server, for example to test an HTTPS-only service, you will need to create and use your own certificates. Here's one way to do it:
+If you do need to make HTTPS connections to a local server, for example to test an HTTPS-only service, you will need to create and use your own certificates. Here's one way to do it...
1. Use [trustme](https://github.com/python-trio/trustme) to generate a pair of server key/cert files, and a client cert file.
2. Pass the server key/cert files when starting your local server. (This depends on the particular web server you're using. For example, [Uvicorn](https://www.uvicorn.org) provides the `--ssl-keyfile` and `--ssl-certfile` options.)
-3. Tell HTTPX to use the certificates stored in `client.pem`:
+3. Configure `httpx` to use the certificates stored in `client.pem`.
-```pycon
->>> import httpx
->>> context = httpx.SSLContext()
->>> context.load_verify_locations(cafile="/tmp/client.pem")
->>> r = httpx.get("https://localhost:8000", ssl_context=context)
->>> r
-Response <200 OK>
+```python
+ctx = ssl.create_default_context(cafile="client.pem")
+client = httpx.Client(verify=ctx)
```
the [SSL certificates section](https://www.python-httpx.org/advanced/ssl/),
this is where our previously generated `client.pem` comes in:
-```
-import httpx
-
-with httpx.Client(proxy="http://127.0.0.1:8080/", verify="/path/to/client.pem") as client:
- response = client.get("https://example.org")
- print(response.status_code) # should print 200
+```python
+ctx = ssl.create_default_context(cafile="/path/to/client.pem")
+client = httpx.Client(proxy="http://127.0.0.1:8080/", verify=ctx)
```
Note, however, that HTTPS requests will only succeed to the host specified
"RequestNotRead",
"Response",
"ResponseNotRead",
- "SSLContext",
"stream",
"StreamClosed",
"StreamConsumed",
from __future__ import annotations
-import ssl
import typing
from contextlib import contextmanager
)
from ._urls import URL
+if typing.TYPE_CHECKING:
+ import ssl # pragma: no cover
+
+
__all__ = [
"delete",
"get",
proxy: ProxyTypes | None = None,
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
follow_redirects: bool = False,
- ssl_context: ssl.SSLContext | None = None,
+ verify: ssl.SSLContext | bool = True,
trust_env: bool = True,
- # Deprecated in favor of `ssl_context`...
- verify: typing.Any = None,
- cert: typing.Any = None,
) -> Response:
"""
Sends an HTTP request.
* **timeout** - *(optional)* The timeout configuration to use when sending
the request.
* **follow_redirects** - *(optional)* Enables or disables HTTP redirects.
- * **ssl_context** - *(optional)* An SSL certificate used by the requested host
- to authenticate the client.
+ * **verify** - *(optional)* Either `True` to use an SSL context with the
+ default CA bundle, `False` to disable verification, or an instance of
+ `ssl.SSLContext` to use a custom context.
* **trust_env** - *(optional)* Enables or disables usage of environment
variables for configuration.
with Client(
cookies=cookies,
proxy=proxy,
- ssl_context=ssl_context,
+ verify=verify,
timeout=timeout,
trust_env=trust_env,
- verify=verify,
- cert=cert,
) as client:
return client.request(
method=method,
proxy: ProxyTypes | None = None,
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
follow_redirects: bool = False,
- ssl_context: ssl.SSLContext | None = None,
+ verify: ssl.SSLContext | bool = True,
trust_env: bool = True,
- # Deprecated in favor of `ssl_context`...
- verify: typing.Any = None,
- cert: typing.Any = None,
) -> typing.Iterator[Response]:
"""
Alternative to `httpx.request()` that streams the response body
with Client(
cookies=cookies,
proxy=proxy,
- ssl_context=ssl_context,
+ verify=verify,
timeout=timeout,
trust_env=trust_env,
- verify=verify,
- cert=cert,
) as client:
with client.stream(
method=method,
auth: AuthTypes | None = None,
proxy: ProxyTypes | None = None,
follow_redirects: bool = False,
- ssl_context: ssl.SSLContext | None = None,
+ verify: ssl.SSLContext | bool = True,
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
trust_env: bool = True,
- # Deprecated in favor of `ssl_context`...
- verify: typing.Any = None,
- cert: typing.Any = None,
) -> Response:
"""
Sends a `GET` request.
auth=auth,
proxy=proxy,
follow_redirects=follow_redirects,
- ssl_context=ssl_context,
+ verify=verify,
timeout=timeout,
trust_env=trust_env,
- verify=verify,
- cert=cert,
)
auth: AuthTypes | None = None,
proxy: ProxyTypes | None = None,
follow_redirects: bool = False,
- ssl_context: ssl.SSLContext | None = None,
+ verify: ssl.SSLContext | bool = True,
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
trust_env: bool = True,
- # Deprecated in favor of `ssl_context`...
- verify: typing.Any = None,
- cert: typing.Any = None,
) -> Response:
"""
Sends an `OPTIONS` request.
auth=auth,
proxy=proxy,
follow_redirects=follow_redirects,
- ssl_context=ssl_context,
+ verify=verify,
timeout=timeout,
trust_env=trust_env,
- verify=verify,
- cert=cert,
)
auth: AuthTypes | None = None,
proxy: ProxyTypes | None = None,
follow_redirects: bool = False,
- ssl_context: ssl.SSLContext | None = None,
+ verify: ssl.SSLContext | bool = True,
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
trust_env: bool = True,
- # Deprecated in favor of `ssl_context`...
- verify: typing.Any = None,
- cert: typing.Any = None,
) -> Response:
"""
Sends a `HEAD` request.
auth=auth,
proxy=proxy,
follow_redirects=follow_redirects,
- ssl_context=ssl_context,
+ verify=verify,
timeout=timeout,
trust_env=trust_env,
- verify=verify,
- cert=cert,
)
auth: AuthTypes | None = None,
proxy: ProxyTypes | None = None,
follow_redirects: bool = False,
- ssl_context: ssl.SSLContext | None = None,
+ verify: ssl.SSLContext | bool = True,
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
trust_env: bool = True,
- # Deprecated in favor of `ssl_context`...
- verify: typing.Any = None,
- cert: typing.Any = None,
) -> Response:
"""
Sends a `POST` request.
auth=auth,
proxy=proxy,
follow_redirects=follow_redirects,
- ssl_context=ssl_context,
+ verify=verify,
timeout=timeout,
trust_env=trust_env,
- verify=verify,
- cert=cert,
)
auth: AuthTypes | None = None,
proxy: ProxyTypes | None = None,
follow_redirects: bool = False,
- ssl_context: ssl.SSLContext | None = None,
+ verify: ssl.SSLContext | bool = True,
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
trust_env: bool = True,
- # Deprecated in favor of `ssl_context`...
- verify: typing.Any = None,
- cert: typing.Any = None,
) -> Response:
"""
Sends a `PUT` request.
auth=auth,
proxy=proxy,
follow_redirects=follow_redirects,
- ssl_context=ssl_context,
+ verify=verify,
timeout=timeout,
trust_env=trust_env,
- verify=verify,
- cert=cert,
)
auth: AuthTypes | None = None,
proxy: ProxyTypes | None = None,
follow_redirects: bool = False,
- ssl_context: ssl.SSLContext | None = None,
+ verify: ssl.SSLContext | bool = True,
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
trust_env: bool = True,
- # Deprecated in favor of `ssl_context`...
- verify: typing.Any = None,
- cert: typing.Any = None,
) -> Response:
"""
Sends a `PATCH` request.
auth=auth,
proxy=proxy,
follow_redirects=follow_redirects,
- ssl_context=ssl_context,
+ verify=verify,
timeout=timeout,
trust_env=trust_env,
- verify=verify,
- cert=cert,
)
proxy: ProxyTypes | None = None,
follow_redirects: bool = False,
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
- ssl_context: ssl.SSLContext | None = None,
+ verify: ssl.SSLContext | bool = True,
trust_env: bool = True,
- # Deprecated in favor of `ssl_context`...
- verify: typing.Any = None,
- cert: typing.Any = None,
) -> Response:
"""
Sends a `DELETE` request.
auth=auth,
proxy=proxy,
follow_redirects=follow_redirects,
- ssl_context=ssl_context,
+ verify=verify,
timeout=timeout,
trust_env=trust_env,
- verify=verify,
- cert=cert,
)
import datetime
import enum
import logging
-import ssl
import time
import typing
import warnings
same_origin,
)
+if typing.TYPE_CHECKING:
+ import ssl # pragma: no cover
+
__all__ = ["USE_CLIENT_DEFAULT", "AsyncClient", "Client"]
# The type annotation for @classmethod and context managers here follows PEP 484
sending requests.
* **cookies** - *(optional)* Dictionary of Cookie items to include when
sending requests.
- * **ssl_context** - *(optional)* An SSL certificate used by the requested host
- to authenticate the client.
+ * **verify** - *(optional)* Either `True` to use an SSL context with the
+ default CA bundle, `False` to disable verification, or an instance of
+ `ssl.SSLContext` to use a custom context.
* **http2** - *(optional)* A boolean indicating if HTTP/2 support should be
enabled. Defaults to `False`.
* **proxy** - *(optional)* A proxy URL where all the traffic should be routed.
params: QueryParamTypes | None = None,
headers: HeaderTypes | None = None,
cookies: CookieTypes | None = None,
- ssl_context: ssl.SSLContext | None = None,
+ verify: ssl.SSLContext | bool = True,
http1: bool = True,
http2: bool = False,
proxy: ProxyTypes | None = None,
transport: BaseTransport | None = None,
trust_env: bool = True,
default_encoding: str | typing.Callable[[bytes], str] = "utf-8",
- # Deprecated in favor of `ssl_context`...
- verify: typing.Any = None,
- cert: typing.Any = None,
) -> None:
super().__init__(
auth=auth,
proxy_map = self._get_proxy_map(proxy, allow_env_proxies)
self._transport = self._init_transport(
- ssl_context=ssl_context,
+ verify=verify,
http1=http1,
http2=http2,
limits=limits,
transport=transport,
- trust_env=trust_env,
- # Deprecated in favor of ssl_context...
- verify=verify,
- cert=cert,
)
self._mounts: dict[URLPattern, BaseTransport | None] = {
URLPattern(key): None
if proxy is None
else self._init_proxy_transport(
proxy,
- ssl_context=ssl_context,
+ verify=verify,
http1=http1,
http2=http2,
limits=limits,
- # Deprecated in favor of ssl_context...
- verify=verify,
- cert=cert,
)
for key, proxy in proxy_map.items()
}
def _init_transport(
self,
- ssl_context: ssl.SSLContext | None = None,
+ verify: ssl.SSLContext | bool = True,
http1: bool = True,
http2: bool = False,
limits: Limits = DEFAULT_LIMITS,
transport: BaseTransport | None = None,
- trust_env: bool = True,
- # Deprecated in favor of `ssl_context`...
- verify: typing.Any = None,
- cert: typing.Any = None,
) -> BaseTransport:
if transport is not None:
return transport
return HTTPTransport(
- ssl_context=ssl_context,
+ verify=verify,
http1=http1,
http2=http2,
limits=limits,
- verify=verify,
- cert=cert,
)
def _init_proxy_transport(
self,
proxy: Proxy,
- ssl_context: ssl.SSLContext | None = None,
+ verify: ssl.SSLContext | bool = True,
http1: bool = True,
http2: bool = False,
limits: Limits = DEFAULT_LIMITS,
- trust_env: bool = True,
- # Deprecated in favor of `ssl_context`...
- verify: typing.Any = None,
- cert: typing.Any = None,
) -> BaseTransport:
return HTTPTransport(
- ssl_context=ssl_context,
+ verify=verify,
http1=http1,
http2=http2,
limits=limits,
proxy=proxy,
- verify=verify,
- cert=cert,
)
def _transport_for_url(self, url: URL) -> BaseTransport:
sending requests.
* **cookies** - *(optional)* Dictionary of Cookie items to include when
sending requests.
- * **ssl_context** - *(optional)* An SSL certificate used by the requested host
- to authenticate the client.
+ * **verify** - *(optional)* Either `True` to use an SSL context with the
+ default CA bundle, `False` to disable verification, or an instance of
+ `ssl.SSLContext` to use a custom context.
* **http2** - *(optional)* A boolean indicating if HTTP/2 support should be
enabled. Defaults to `False`.
* **proxy** - *(optional)* A proxy URL where all the traffic should be routed.
params: QueryParamTypes | None = None,
headers: HeaderTypes | None = None,
cookies: CookieTypes | None = None,
- ssl_context: ssl.SSLContext | None = None,
+ verify: ssl.SSLContext | bool = True,
http1: bool = True,
http2: bool = False,
proxy: ProxyTypes | None = None,
transport: AsyncBaseTransport | None = None,
trust_env: bool = True,
default_encoding: str | typing.Callable[[bytes], str] = "utf-8",
- # Deprecated in favor of `ssl_context`...
- verify: typing.Any = None,
- cert: typing.Any = None,
) -> None:
super().__init__(
auth=auth,
proxy_map = self._get_proxy_map(proxy, allow_env_proxies)
self._transport = self._init_transport(
- ssl_context=ssl_context,
+ verify=verify,
http1=http1,
http2=http2,
limits=limits,
transport=transport,
- # Deprecated in favor of ssl_context
- verify=verify,
- cert=cert,
)
self._mounts: dict[URLPattern, AsyncBaseTransport | None] = {
if proxy is None
else self._init_proxy_transport(
proxy,
- ssl_context=ssl_context,
+ verify=verify,
http1=http1,
http2=http2,
limits=limits,
- # Deprecated in favor of `ssl_context`...
- verify=verify,
- cert=cert,
)
for key, proxy in proxy_map.items()
}
def _init_transport(
self,
- ssl_context: ssl.SSLContext | None = None,
+ verify: ssl.SSLContext | bool = True,
http1: bool = True,
http2: bool = False,
limits: Limits = DEFAULT_LIMITS,
transport: AsyncBaseTransport | None = None,
- # Deprecated in favor of `ssl_context`...
- verify: typing.Any = None,
- cert: typing.Any = None,
) -> AsyncBaseTransport:
if transport is not None:
return transport
return AsyncHTTPTransport(
- ssl_context=ssl_context,
+ verify=verify,
http1=http1,
http2=http2,
limits=limits,
- verify=verify,
- cert=cert,
)
def _init_proxy_transport(
self,
proxy: Proxy,
- ssl_context: ssl.SSLContext | None = None,
+ verify: ssl.SSLContext | bool = True,
http1: bool = True,
http2: bool = False,
limits: Limits = DEFAULT_LIMITS,
- # Deprecated in favor of `ssl_context`...
- verify: typing.Any = None,
- cert: typing.Any = None,
) -> AsyncBaseTransport:
return AsyncHTTPTransport(
- ssl_context=ssl_context,
+ verify=verify,
http1=http1,
http2=http2,
limits=limits,
proxy=proxy,
- verify=verify,
- cert=cert,
)
def _transport_for_url(self, url: URL) -> AsyncBaseTransport:
from __future__ import annotations
-import os
import ssl
-import sys
import typing
-import warnings
from ._models import Headers
from ._types import HeaderTypes, TimeoutTypes
from ._urls import URL
-__all__ = ["Limits", "Proxy", "SSLContext", "Timeout", "create_ssl_context"]
+__all__ = ["Limits", "Proxy", "Timeout", "create_ssl_context"]
class UnsetType:
UNSET = UnsetType()
-def create_ssl_context(
- verify: typing.Any = None,
- cert: typing.Any = None,
- trust_env: bool = True,
- http2: bool = False,
-) -> ssl.SSLContext: # pragma: nocover
- # The `create_ssl_context` helper function is now deprecated
- # in favour of `httpx.SSLContext()`.
- if isinstance(verify, bool):
- ssl_context: ssl.SSLContext = SSLContext(verify=verify)
- warnings.warn(
- "The verify=<bool> parameter is deprecated since 0.28.0. "
- "Use `ssl_context=httpx.SSLContext(verify=<bool>)`."
- )
- elif isinstance(verify, str):
- warnings.warn(
- "The verify=<str> parameter is deprecated since 0.28.0. "
- "Use `ssl_context=httpx.SSLContext()` and `.load_verify_locations()`."
- )
- ssl_context = SSLContext()
- if os.path.isfile(verify):
- ssl_context.load_verify_locations(cafile=verify)
- elif os.path.isdir(verify):
- ssl_context.load_verify_locations(capath=verify)
- elif isinstance(verify, ssl.SSLContext):
- warnings.warn(
- "The verify=<ssl context> parameter is deprecated since 0.28.0. "
- "Use `ssl_context = httpx.SSLContext()`."
- )
- ssl_context = verify
- else:
- warnings.warn(
- "`create_ssl_context()` is deprecated since 0.28.0."
- "Use `ssl_context = httpx.SSLContext()`."
- )
- ssl_context = SSLContext()
-
- if cert is not None:
- warnings.warn(
- "The `cert=<...>` parameter is deprecated since 0.28.0. "
- "Use `ssl_context = httpx.SSLContext()` and `.load_cert_chain()`."
- )
- if isinstance(cert, str):
- ssl_context.load_cert_chain(cert)
- else:
- ssl_context.load_cert_chain(*cert)
+def create_ssl_context(verify: ssl.SSLContext | bool = True) -> ssl.SSLContext:
+ import ssl
- return ssl_context
+ import certifi
+ if verify is True:
+ return ssl.create_default_context(cafile=certifi.where())
+ elif verify is False:
+ ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
+ ssl_context.check_hostname = False
+ ssl_context.verify_mode = ssl.CERT_NONE
+ return ssl_context
-class SSLContext(ssl.SSLContext):
- def __init__(
- self,
- verify: bool = True,
- ) -> None:
- import certifi
-
- # ssl.SSLContext sets OP_NO_SSLv2, OP_NO_SSLv3, OP_NO_COMPRESSION,
- # OP_CIPHER_SERVER_PREFERENCE, OP_SINGLE_DH_USE and OP_SINGLE_ECDH_USE
- # by default. (from `ssl.create_default_context`)
- super().__init__()
- self._verify = verify
-
- # Our SSL setup here is similar to the stdlib `ssl.create_default_context()`
- # implementation, except with `certifi` used for certificate verification.
- if not verify:
- self.check_hostname = False
- self.verify_mode = ssl.CERT_NONE
- return
-
- self.verify_mode = ssl.CERT_REQUIRED
- self.check_hostname = True
-
- # Use stricter verify flags where possible.
- if hasattr(ssl, "VERIFY_X509_PARTIAL_CHAIN"): # pragma: nocover
- self.verify_flags |= ssl.VERIFY_X509_PARTIAL_CHAIN
- if hasattr(ssl, "VERIFY_X509_STRICT"): # pragma: nocover
- self.verify_flags |= ssl.VERIFY_X509_STRICT
-
- # Default to `certifi` for certificiate verification.
- self.load_verify_locations(cafile=certifi.where())
-
- # OpenSSL keylog file support.
- if hasattr(self, "keylog_filename"):
- keylogfile = os.environ.get("SSLKEYLOGFILE")
- if keylogfile and not sys.flags.ignore_environment:
- self.keylog_filename = keylogfile
-
- def __repr__(self) -> str:
- class_name = self.__class__.__name__
- return f"<{class_name}(verify={self._verify!r})>"
-
- def __new__(
- cls,
- protocol: ssl._SSLMethod = ssl.PROTOCOL_TLS_CLIENT,
- *args: typing.Any,
- **kwargs: typing.Any,
- ) -> "SSLContext":
- return super().__new__(cls, protocol, *args, **kwargs)
+ return verify
class Timeout:
import rich.table
from ._client import Client
-from ._config import SSLContext
from ._exceptions import RequestError
from ._models import Response
from ._status_codes import codes
if not method:
method = "POST" if content or data or files or json else "GET"
- ssl_context = SSLContext(verify=verify)
try:
- with Client(
- proxy=proxy, timeout=timeout, http2=http2, ssl_context=ssl_context
- ) as client:
+ with Client(proxy=proxy, timeout=timeout, http2=http2, verify=verify) as client:
with client.stream(
method,
url,
import httpx # pragma: no cover
-from .._config import DEFAULT_LIMITS, Limits, Proxy, SSLContext, create_ssl_context
+from .._config import DEFAULT_LIMITS, Limits, Proxy, create_ssl_context
from .._exceptions import (
ConnectError,
ConnectTimeout,
class HTTPTransport(BaseTransport):
def __init__(
self,
- ssl_context: ssl.SSLContext | None = None,
+ verify: ssl.SSLContext | bool = True,
http1: bool = True,
http2: bool = False,
limits: Limits = DEFAULT_LIMITS,
local_address: str | None = None,
retries: int = 0,
socket_options: typing.Iterable[SOCKET_OPTION] | None = None,
- # Deprecated...
- verify: typing.Any = None,
- cert: typing.Any = None,
) -> None:
import httpcore
proxy = Proxy(url=proxy) if isinstance(proxy, (str, URL)) else proxy
- if verify is not None or cert is not None: # pragma: nocover
- # Deprecated...
- ssl_context = create_ssl_context(verify, cert)
- else:
- ssl_context = ssl_context or SSLContext()
+ ssl_context = create_ssl_context(verify=verify)
if proxy is None:
self._pool = httpcore.ConnectionPool(
class AsyncHTTPTransport(AsyncBaseTransport):
def __init__(
self,
- ssl_context: ssl.SSLContext | None = None,
+ verify: ssl.SSLContext | bool = True,
http1: bool = True,
http2: bool = False,
limits: Limits = DEFAULT_LIMITS,
local_address: str | None = None,
retries: int = 0,
socket_options: typing.Iterable[SOCKET_OPTION] | None = None,
- # Deprecated...
- verify: typing.Any = None,
- cert: typing.Any = None,
) -> None:
import httpcore
proxy = Proxy(url=proxy) if isinstance(proxy, (str, URL)) else proxy
- if verify is not None or cert is not None: # pragma: nocover
- # Deprecated...
- ssl_context = create_ssl_context(verify, cert)
- else:
- ssl_context = ssl_context or SSLContext()
+ ssl_context = create_ssl_context(verify=verify)
if proxy is None:
self._pool = httpcore.AsyncConnectionPool(
return f"{self.__class__.__name__}({url!r})"
+ @property
+ def raw(self) -> tuple[bytes, bytes, int, bytes]: # pragma: nocover
+ import collections
+ import warnings
+
+ warnings.warn("URL.raw is deprecated.")
+ RawURL = collections.namedtuple(
+ "RawURL", ["raw_scheme", "raw_host", "port", "raw_path"]
+ )
+ return RawURL(
+ raw_scheme=self.raw_scheme,
+ raw_host=self.raw_host,
+ port=self.port,
+ raw_path=self.raw_path,
+ )
+
class QueryParams(typing.Mapping[str, str]):
"""
def test_load_ssl_config():
- context = httpx.SSLContext()
+ context = httpx.create_ssl_context()
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
assert context.check_hostname is True
def test_load_ssl_config_verify_non_existing_file():
with pytest.raises(IOError):
- context = httpx.SSLContext()
+ context = httpx.create_ssl_context()
context.load_verify_locations(cafile="/path/to/nowhere")
def test_load_ssl_with_keylog(monkeypatch: typing.Any) -> None:
monkeypatch.setenv("SSLKEYLOGFILE", "test")
- context = httpx.SSLContext()
+ context = httpx.create_ssl_context()
assert context.keylog_filename == "test"
def test_load_ssl_config_verify_existing_file():
- context = httpx.SSLContext()
+ context = httpx.create_ssl_context()
context.load_verify_locations(capath=certifi.where())
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
assert context.check_hostname is True
def test_load_ssl_config_verify_directory():
- context = httpx.SSLContext()
+ context = httpx.create_ssl_context()
context.load_verify_locations(capath=Path(certifi.where()).parent)
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
assert context.check_hostname is True
def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):
- context = httpx.SSLContext()
+ context = httpx.create_ssl_context()
context.load_cert_chain(cert_pem_file, cert_private_key_file)
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
assert context.check_hostname is True
def test_load_ssl_config_cert_and_encrypted_key(
cert_pem_file, cert_encrypted_private_key_file, password
):
- context = httpx.SSLContext()
+ context = httpx.create_ssl_context()
context.load_cert_chain(cert_pem_file, cert_encrypted_private_key_file, password)
assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED
assert context.check_hostname is True
cert_pem_file, cert_encrypted_private_key_file
):
with pytest.raises(ssl.SSLError):
- context = httpx.SSLContext()
+ context = httpx.create_ssl_context()
context.load_cert_chain(
cert_pem_file, cert_encrypted_private_key_file, "password1"
)
def test_load_ssl_config_cert_without_key_raises(cert_pem_file):
with pytest.raises(ssl.SSLError):
- context = httpx.SSLContext()
+ context = httpx.create_ssl_context()
context.load_cert_chain(cert_pem_file)
def test_load_ssl_config_no_verify():
- context = httpx.SSLContext(verify=False)
+ context = httpx.create_ssl_context(verify=False)
assert context.verify_mode == ssl.VerifyMode.CERT_NONE
assert context.check_hostname is False
def test_SSLContext_with_get_request(server, cert_pem_file):
- context = httpx.SSLContext()
+ context = httpx.create_ssl_context()
context.load_verify_locations(cert_pem_file)
- response = httpx.get(server.url, ssl_context=context)
+ response = httpx.get(server.url, verify=context)
assert response.status_code == 200
-def test_SSLContext_repr():
- ssl_context = httpx.SSLContext()
-
- assert repr(ssl_context) == "<SSLContext(verify=True)>"
-
-
def test_limits_repr():
limits = httpx.Limits(max_connections=100)
expected = (
def test_invalid_proxy_scheme():
with pytest.raises(ValueError):
httpx.Proxy("invalid://example.com")
-
-
-def test_certifi_lazy_loading():
- global httpx, certifi
- import sys
-
- del sys.modules["httpx"]
- del sys.modules["certifi"]
- del httpx
- del certifi
- import httpx
-
- assert "certifi" not in sys.modules
- _context = httpx.SSLContext()
- assert "certifi" in sys.modules