Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ Proxies can be configured to have different behavior such as forwarding or tunne
```python
proxy = httpx.HTTPProxy(
proxy_url="https://127.0.0.1",
proxy_mode=httpx.HTTPProxyMode.TUNNEL_ONLY
proxy_mode="TUNNEL_ONLY" # May be "TUNNEL_ONLY" or "FORWARD_ONLY". Defaults to "DEFAULT".
)
async with httpx.Client(proxies=proxy) as client:
# This request will be tunneled instead of forwarded.
Expand Down
61 changes: 8 additions & 53 deletions httpx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,11 @@
from .api import delete, get, head, options, patch, post, put, request, stream
from .auth import BasicAuth, DigestAuth
from .client import Client
from .concurrency.asyncio import AsyncioBackend
from .concurrency.base import BasePoolSemaphore, BaseSocketStream, ConcurrencyBackend
from .config import (
USER_AGENT,
CertTypes,
PoolLimits,
SSLConfig,
Timeout,
TimeoutConfig,
TimeoutTypes,
VerifyTypes,
)
from .dispatch.base import Dispatcher
from .dispatch.connection import HTTPConnection
from .dispatch.connection_pool import ConnectionPool
from .config import TimeoutConfig # For 0.8 backwards compat.
from .config import PoolLimits, Timeout
from .dispatch.proxy_http import HTTPProxy, HTTPProxyMode
from .exceptions import (
ConnectionClosed,
ConnectTimeout,
CookieConflict,
DecodingError,
Expand All @@ -38,23 +26,7 @@
TooManyRedirects,
WriteTimeout,
)
from .models import (
URL,
AuthTypes,
Cookies,
CookieTypes,
Headers,
HeaderTypes,
Origin,
QueryParams,
QueryParamTypes,
Request,
RequestData,
RequestFiles,
Response,
ResponseContent,
URLTypes,
)
from .models import URL, Cookies, Headers, Origin, QueryParams, Request, Response
from .status_codes import StatusCode, codes

__all__ = [
Expand All @@ -71,24 +43,18 @@
"put",
"request",
"stream",
"codes",
"BasicAuth",
"Client",
"DigestAuth",
"AsyncioBackend",
"USER_AGENT",
"CertTypes",
"PoolLimits",
"SSLConfig",
"Timeout",
"TimeoutConfig",
"VerifyTypes",
"HTTPConnection",
"BasePoolSemaphore",
"ConnectionPool",
"TimeoutConfig", # For 0.8 backwards compat.
"HTTPProxy",
"HTTPProxyMode",
"HTTPProxyMode", # For 0.8 backwards compat.
"ConnectTimeout",
"CookieConflict",
"ConnectionClosed",
"DecodingError",
"HTTPError",
"InvalidURL",
Expand All @@ -106,25 +72,14 @@
"WriteTimeout",
"BaseSocketStream",
"ConcurrencyBackend",
"Dispatcher",
"URL",
"URLTypes",
"StatusCode",
"codes",
"TimeoutTypes",
"AuthTypes",
"Cookies",
"CookieTypes",
"Headers",
"HeaderTypes",
"Origin",
"QueryParams",
"QueryParamTypes",
"Request",
"RequestData",
"TimeoutException",
"Response",
"ResponseContent",
"RequestFiles",
"DigestAuth",
]
23 changes: 20 additions & 3 deletions httpx/dispatch/proxy_http.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import enum
import typing
import warnings
from base64 import b64encode

import h11
Expand All @@ -25,11 +26,18 @@


class HTTPProxyMode(enum.Enum):
# This enum is pending deprecation in order to reduce API surface area,
# but is currently still around for 0.8 backwards compat.
DEFAULT = "DEFAULT"
FORWARD_ONLY = "FORWARD_ONLY"
TUNNEL_ONLY = "TUNNEL_ONLY"


DEFAULT_MODE = "DEFAULT"
FORWARD_ONLY = "FORWARD_ONLY"
TUNNEL_ONLY = "TUNNEL_ONLY"
Comment thread
florimondmanca marked this conversation as resolved.


class HTTPProxy(ConnectionPool):
"""A proxy that sends requests to the recipient server
on behalf of the connecting client.
Expand All @@ -40,7 +48,7 @@ def __init__(
proxy_url: URLTypes,
*,
proxy_headers: HeaderTypes = None,
proxy_mode: HTTPProxyMode = HTTPProxyMode.DEFAULT,
proxy_mode: str = "DEFAULT",
verify: VerifyTypes = True,
cert: CertTypes = None,
trust_env: bool = None,
Expand All @@ -49,6 +57,15 @@ def __init__(
backend: typing.Union[str, ConcurrencyBackend] = "auto",
):

if isinstance(proxy_mode, HTTPProxyMode):
warnings.warn(
"The 'HTTPProxyMode' enum is pending deprecation. "
"Use a plain string instead. proxy_mode='FORWARD_ONLY', or "
"proxy_mode='TUNNEL_ONLY'."
)
proxy_mode = proxy_mode.value
assert proxy_mode in ("DEFAULT", "FORWARD_ONLY", "TUNNEL_ONLY")

super(HTTPProxy, self).__init__(
verify=verify,
cert=cert,
Expand Down Expand Up @@ -216,8 +233,8 @@ def should_forward_origin(self, origin: Origin) -> bool:
tunnel all 'HTTPS' requests.
"""
return (
self.proxy_mode == HTTPProxyMode.DEFAULT and not origin.is_ssl
) or self.proxy_mode == HTTPProxyMode.FORWARD_ONLY
self.proxy_mode == DEFAULT_MODE and not origin.is_ssl
) or self.proxy_mode == FORWARD_ONLY

async def send(
self,
Expand Down
15 changes: 3 additions & 12 deletions tests/client/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@

import pytest

from httpx import (
URL,
CertTypes,
Client,
DigestAuth,
Dispatcher,
ProtocolError,
Request,
Response,
TimeoutTypes,
VerifyTypes,
)
from httpx import URL, Client, DigestAuth, ProtocolError, Request, Response
from httpx.config import CertTypes, TimeoutTypes, VerifyTypes
from httpx.dispatch.base import Dispatcher


class MockDispatch(Dispatcher):
Expand Down
13 changes: 3 additions & 10 deletions tests/client/test_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@

import pytest

from httpx import (
CertTypes,
Client,
Cookies,
Dispatcher,
Request,
Response,
TimeoutTypes,
VerifyTypes,
)
from httpx import Client, Cookies, Request, Response
from httpx.config import CertTypes, TimeoutTypes, VerifyTypes
from httpx.dispatch.base import Dispatcher


class MockDispatch(Dispatcher):
Expand Down
16 changes: 4 additions & 12 deletions tests/client/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@

import pytest

from httpx import (
CertTypes,
Client,
Dispatcher,
Request,
Response,
TimeoutTypes,
VerifyTypes,
__version__,
models,
)
from httpx import Client, Headers, Request, Response, __version__
from httpx.config import CertTypes, TimeoutTypes, VerifyTypes
from httpx.dispatch.base import Dispatcher


class MockDispatch(Dispatcher):
Expand Down Expand Up @@ -132,7 +124,7 @@ async def test_header_update():


def test_header_does_not_exist():
headers = models.Headers({"foo": "bar"})
headers = Headers({"foo": "bar"})
with pytest.raises(KeyError):
del headers["baz"]

Expand Down
2 changes: 0 additions & 2 deletions tests/client/test_proxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def test_proxies_has_same_properties_as_dispatch():
pool = client.dispatch
proxy = client.proxies["all"]

assert isinstance(pool, httpx.ConnectionPool)
assert isinstance(proxy, httpx.HTTPProxy)

for prop in [
Expand Down Expand Up @@ -101,7 +100,6 @@ def test_dispatcher_for_request(url, proxies, expected):
dispatcher = client.dispatcher_for_url(httpx.URL(url))

if expected is None:
assert isinstance(dispatcher, httpx.ConnectionPool)
assert dispatcher is client.dispatch
else:
assert isinstance(dispatcher, httpx.HTTPProxy)
Expand Down
14 changes: 3 additions & 11 deletions tests/client/test_queryparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@

import pytest

from httpx import (
CertTypes,
Client,
Dispatcher,
QueryParams,
Request,
Response,
TimeoutTypes,
VerifyTypes,
)
from httpx.models import URL
from httpx import URL, Client, QueryParams, Request, Response
from httpx.config import CertTypes, TimeoutTypes, VerifyTypes
from httpx.dispatch.base import Dispatcher


class MockDispatch(Dispatcher):
Expand Down
6 changes: 2 additions & 4 deletions tests/client/test_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@

from httpx import (
URL,
CertTypes,
Client,
Dispatcher,
NotRedirectResponse,
RedirectBodyUnavailable,
RedirectLoop,
Request,
Response,
TimeoutTypes,
TooManyRedirects,
VerifyTypes,
codes,
)
from httpx.config import CertTypes, TimeoutTypes, VerifyTypes
from httpx.dispatch.base import Dispatcher


class MockDispatch(Dispatcher):
Expand Down
2 changes: 1 addition & 1 deletion tests/concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import trio

from httpx import AsyncioBackend
from httpx.concurrency.asyncio import AsyncioBackend
from httpx.concurrency.trio import TrioBackend


Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from uvicorn.config import Config
from uvicorn.main import Server

from httpx import URL, AsyncioBackend
from httpx import URL
from httpx.concurrency.asyncio import AsyncioBackend
from httpx.concurrency.trio import TrioBackend

ENVIRONMENT_VARIABLES = {
Expand Down
Loading