`http_proxy` bug with `starlette.responses.RedirectResponse`
When using requests with the http_proxy environment variable set, and explicitly passing proxies={'http': None, 'https': None} to bypass the proxy, the first request correctly avoids using the proxy. However, if the request returns a redirect (e.g. 3xx), the redirected request incorrectly picks up the proxy settings from the environment, causing the request to fail.
When passing proxies={'http': None, 'https': None} to a request, both the original and any redirected requests should bypass the proxy and not use the http_proxy environment variable.
Only the original request respects the proxies={'http': None, 'https': None} setting. The redirected request ignores it and uses the proxy from the environment variable, leading to connection errors if the target is not proxy-accessible.
Reproduction Steps
import os
import requests
# Set environment variable proxy (simulating a typical corporate environment)
os.environ['http_proxy'] = 'http://some.invalid.proxy:8080'
# Target URL that redirects to another domain
url = "http://httpbin.org/redirect-to?url=http://example.com"
# Explicitly disable proxy
resp = requests.get(url, proxies={'http': None, 'https': None})
print(resp.status_code)
Suggested Fix
new_proxies = resolve_proxies(prepared_request, proxies, self.trust_env)
should be updated to:
new_proxies = resolve_proxies(prepared_request, proxies, self.trust_env if proxies is None else False)
This ensures that when proxies is explicitly passed in by the user (even as None), environment proxies are not used for redirects.
For the time being, I solved the issue by manually setting the trust_env attribute of the Session object to False in the modified request function.
Refer: https://github.com/LazyAGI/LazyLLM/pull/548/files#diff-b02a502a018fbdf49a0af37c6f8c2e971bbd850f4cad1ca6cca54da63cf1ca1cR26