Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookies doesn't shared between requests #139

Closed
spumer opened this issue Aug 19, 2014 · 16 comments
Closed

Cookies doesn't shared between requests #139

spumer opened this issue Aug 19, 2014 · 16 comments
Labels

Comments

@spumer
Copy link
Contributor

spumer commented Aug 19, 2014

Cookies doesn't handled if server set cookie with 200 response code
In example http:https://aiohttp.readthedocs.org/en/latest/client.html#cookies the http:https://httpbin.org set cookie with redirect response and it works.

So, should i manually release response each time or this is a bug?

_shared_cookie_conn = aiohttp.connector.TCPConnector(share_cookies=True)

@asyncio.coroutine
def main():
   resp = yield from aiohttp.request('get', 'http:https://steamcommunity.com/', connector=_shared_cookie_conn)
   #yield from resp.release()  # uncomment this line to handle resp cookies and share with _shared_cookie_conn

if __name__ == '__main__':
   loop = asyncio.get_event_loop()
   loop.run_until_complete(main())
@spumer spumer changed the title Cookies don't shared between requests Cookies doesn't shared between requests Aug 19, 2014
@spumer
Copy link
Contributor Author

spumer commented Aug 21, 2014

As i can see in sources, connection closed (and cookies not handled by connector) instead releasing.
May be handle cookies in any case?

If I have something misunderstood or mistaken — please, give right example. Docs seems outdated.

@popravich
Copy link
Member

Hi, @spumer,
Basically you have to either read response or release it because connection which is handled by
TCPConnector is still belongs to that response (because you might need that response))
As soon as you release (I mean yield from resp.read(), resp.json(), resp.text() or resp.release()) connection it will be available (with cookies) for subsequent requests.

_shared_cookie_conn = aiohttp.connector.TCPConnector(share_cookies=True)

@asyncio.coroutine
def main():
   resp = yield from aiohttp.request('get', 'http:https://steamcommunity.com/', connector=_shared_cookie_conn)
   assert dict(resp.cookies) != {}
   assert dict(_shared_cookie_conn.cookies) == {}
   yield from handle_response_or_release(resp)

   # resp2 = yield from aiohttp.request('get', 'http:https://steamcommunitycom/', connector=_shared_cookie_conn)
   # assert dict(_shared_cookie_conn.cookies) != {}
   # ...


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

@popravich
Copy link
Member

Hopefully, that makes it clear

@spumer
Copy link
Contributor Author

spumer commented Aug 21, 2014

@popravich Yea! Thanks a lot! 👍
Can you add this explanation to documentation? It would be excellent clarify :)

@spumer spumer closed this as completed Aug 21, 2014
@popravich
Copy link
Member

@spumer, updated the docs.

@asvetlov
Copy link
Member

I think making guard for check that user called .release() or .read() makes sense.
Guard can be implemented via weakref callback (we still should support 3.3).

Solution adds subtle incompatibility: now user can read response from .content without calling .read()/.release() but I think the use case is very rare ad the change worth to be applied.

@fafhrd91
Copy link
Member

I disagree, problem with .read() it reads whole response into memory. Using .read() will kill my application immediately.

@kxepal
Copy link
Member

kxepal commented Aug 26, 2014

Using .read() will kill my application immediately.

+1. More over, read() may not actually read all the response and will block the client for a very long time.

@asvetlov
Copy link
Member

We can have some special method for marking response object 'processed'.
I understand .read() problems but for casual user good to have strict error in case of improper usage. Or at least warning.

@kxepal
Copy link
Member

kxepal commented Aug 26, 2014

May be change the way how cookies get shared? Any reasons why connector, which actually should manage connection and his state, knows about and manages the headers which are a little bit more high level than it operates?

@asvetlov
Copy link
Member

Add separate CookieManager and pass it along aiohttp.request with connector param?

@popravich
Copy link
Member

ok, the problem here is with cookies.
we can simply update shared cookies as soon as headers read.
correct me if I'm wrong.

@kxepal
Copy link
Member

kxepal commented Aug 26, 2014

Add separate CookieManager and pass it along aiohttp.request with connector param?

Something like that. Actually, that's what I currently doing, but via wrapper around aiohttp.request to set cookies/authorization headers and get them back to manager after.

@kxepal
Copy link
Member

kxepal commented Aug 26, 2014

we can simply update shared cookies as soon as headers read.

Sounds right. And that's becomes possible after ClientResponse.start() call when headers are get read and parsed.

@popravich
Copy link
Member

Guys, any thoughts on pull-request? Can I merge it?

popravich added a commit that referenced this issue Aug 29, 2014
share response cookies as soon as received (relates to #139)
@lock
Copy link

lock bot commented Oct 29, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants