Skip to content

Commit

Permalink
fix psf#1287: Make sure expired cookies get removed from session.cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
gazpachoking committed Apr 5, 2013
1 parent dccfc5b commit d22ac00
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions requests/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from datetime import datetime

from .compat import cookielib
from .cookies import cookiejar_from_dict
from .cookies import cookiejar_from_dict, extract_cookies_to_jar
from .models import Request, PreparedRequest
from .hooks import default_hooks, dispatch_hook
from .utils import from_key_val_list, default_headers
Expand Down Expand Up @@ -91,10 +91,6 @@ def resolve_redirects(self, resp, req, stream=False, timeout=None,
prepared_request.method = req.method
prepared_request.url = req.url

cookiejar = cookiejar_from_dict({})
cookiejar.update(self.cookies)
cookiejar.update(resp.cookies)

# ((resp.status_code is codes.see_other))
while (('location' in resp.headers and resp.status_code in REDIRECT_STATI)):

Expand Down Expand Up @@ -147,7 +143,7 @@ def resolve_redirects(self, resp, req, stream=False, timeout=None,
except KeyError:
pass

prepared_request.prepare_cookies(cookiejar)
prepared_request.prepare_cookies(self.cookies)

resp = self.send(
prepared_request,
Expand All @@ -159,12 +155,12 @@ def resolve_redirects(self, resp, req, stream=False, timeout=None,
allow_redirects=False,
)

cookiejar.update(resp.cookies)
extract_cookies_to_jar(self.cookies, prepared_request, resp.raw)

i += 1
yield resp

resp.cookies.update(cookiejar)
resp.cookies = self.cookies.copy()


class Session(SessionRedirectMixin):
Expand Down Expand Up @@ -349,9 +345,6 @@ def request(self, method, url,
}
resp = self.send(prep, **send_kwargs)

# Persist cookies.
self.cookies.update(resp.cookies)

return resp

def get(self, url, **kwargs):
Expand Down Expand Up @@ -460,6 +453,9 @@ def send(self, request, **kwargs):
# Response manipulation hooks
r = dispatch_hook('response', hooks, r, **kwargs)

# Persist cookies
extract_cookies_to_jar(self.cookies, request, r.raw)

# Redirect resolving generator.
gen = self.resolve_redirects(r, request, stream=stream,
timeout=timeout, verify=verify, cert=cert,
Expand Down

0 comments on commit d22ac00

Please sign in to comment.