Skip to content

ASGI requests are routed through proxy #1039

Closed

Description

@marco-neumann-by

Checklist

  • The bug is reproducible against the latest release and/or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

Using the AsyncClient with an ASGI app seems to go through the proxy. Either this is is bug or I don't understand under which conditions this would make sense.

To reproduce

import asyncio
import os
import httpx


async def hello_world(scope, receive, send):
    status = 200
    output = b"Hello, World!"
    headers = [(b"content-type", "text/plain"), (b"content-length", str(len(output)))]

    await send({"type": "http.response.start", "status": status, "headers": headers})
    await send({"type": "http.response.body", "body": output})


async def test_asgi_with_env():
    os.environ["http_proxy"] = "http://localhost:1111"

    client = httpx.AsyncClient(app=hello_world)
    response = await client.get("http://www.example.org/")
    assert response.status_code == 200
    assert response.text == "Hello, World!"


asyncio.run(test_asgi_with_env())

Expected behavior

Test should pass, no proxy interaction should be made.

Note that passing trust_env=False to AsyncClient disables the proxy and the test passes. However, this interaction seems weird.

Actual behavior

httpcore._exceptions.ConnectError: Multiple exceptions: [Errno 61] Connect call failed ('::1', 1111, 0, 0), [Errno 61] Connect call failed ('127.0.0.1', 1111

Environment

  • OS: macOS
  • Python version: 3.8.3
  • HTTPX version: 0.13.2 and on master (cfd07cc290cc610fceb8d9f025c6bebc1d4b3275)
  • Async environment: asyncio, but also tested with trio where the issue also occurs.
  • HTTP proxy: see description
  • Custom certificates: no

Additional context

fastapi/fastapi#1619

Activity

  1. added
    bugSomething isn't working
    proxiesIssues related to HTTP and SOCKS proxies
    user-experienceEnsuring that users have a good experience using the library
    and removed
    bugSomething isn't working
    on Jun 30, 2020
  2. florimondmanca commented on Jun 30, 2020

    @florimondmanca
    Contributor

    Might be worth looking if there’s any past discussion about whether we should make trust_env default to False... I think we’ve had strange UX like these in the past due to HTTPX using env cars when it didn’t really make sense.

    Alternatively, on a smaller scope, we can look at the auto proxies setup code, see if it makes sense to bypass it if an app was passed... (Noting that it’s fixes the behavior in this particular case only, eg custom dispatchers where mount-by-scheme doesn’t really make sense could also experience this bug in the future. But you know, YAGNI...)

  3. lovelydinosaur commented on Jul 1, 2020

    @lovelydinosaur
    Member

    on a smaller scope, we can look at the auto proxies setup code, see if it makes sense to bypass it if an app was passed...

    Yup absolutely makes sense to ignore the proxies setup if app=... is passed. 👍

  4. florimondmanca commented on Jul 2, 2020

    @florimondmanca
    Contributor

    Cool, looks like the resolution path is all rounded there, up for grabs…

  5. added and removed
    user-experienceEnsuring that users have a good experience using the library
    on Jul 2, 2020
  6. added this to the v0.14 milestone on Jul 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomersproxiesIssues related to HTTP and SOCKS proxies

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions