aio-libs / aiohttp

Asynchronous HTTP client/server framework for asyncio and Python
https://docs.aiohttp.org
Other
14.97k stars 2k forks source link

Client`s proxy auth not work in specially condition #2596

Open strongbugman opened 6 years ago

strongbugman commented 6 years ago

Long story short

Only first proxy auth is fun when sending many http`s requests(with same proxy and same url location) through one client session

Expected behaviour

Request same url with same authed proxy many times in one client session, proxy auth will work for all requests

Actual behaviour

Request same url with same authed proxy many times in one client session, only first request is fun, others will return 407 HTTP status code which mean the proxy auth not work(No proxy auth in the requests HTTP headers).

Steps to reproduce

There is a example code:

async def main():
    s = aiohttp.ClientSession()
    proxy = 'http://name:passwd@one_proxy_with_auth' 
    async with s.request('GET', 'http://httpbin.org', proxy=proxy) as res:
        await res.read()
        print(res.status)  # print 200
    async with s.request('GET', 'http://httpbin.org', proxy=proxy) as res:
        await res.read()
        print(res.status)  # print 407, means proxy auth not work
    await s.close()

if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())

Your environment

aiohttp: 3.3.1 as client OS: MacOS 10.13.1 proxy: squid

strongbugman commented 6 years ago

Reopen with more detail.

I find that the second request do not have proxy auth header(for http request, https request is fun), so I can do like this:

s = aiohttp.ClientSession()
proxy = 'http://name:passwd@one_proxy_with_auth' 
async with s.request('GET', 'http://httpbin.org', proxy=proxy) as res:
    await res.read()
    print(res.status)  # print 200
async with s.request('GET', 'http://httpbin.org', proxy=proxy, headers={'PROXY_AUTHENTICATE': 'Basic authstr'}) as res:
    await res.read()
    print(res.status)  # print 200
await s.close()