Skip to content
Myr

mostly complete cookie support based on cookiejar

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by miguel · committed by Shivaram Lingamneni · parent fed6cfb · Browse files · 3 files+40 added−37 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+40 added−37 removed

  • other:+40 added−37 removed

3 files+40 added−37 removed

modified:requests/packages/oreos/cookiejar.py+9 added-4 removed
1"""2Wrapper for CookieJar allow dict-like access.3Ideally there would be wrappers for LWPCookieJar and MozillaCookieJar as well.4"""56import urlparse7import Cookie8from Cookie import Morsel9import cookielib
12 unmodified lines
22 def get_host(self):23 return urlparse.urlparse(self._r.full_url).netloc24 25 def get_origin_req_host(self):21 if self._r.history:22 r = self._r.history[0]26 if self._r.response.history:27 r = self._r.response.history[0]28 return urlparse.urlparse(r).netloc29 else:30 return self.get_host()31
14 unmodified lines
46 self._r.headers[name] = value47 48 def is_unverifiable(self):49 # unverifiable == redirected45 return bool(self.history)50 return bool(self._r.response.history)5152class MockResponse:53 """54 Wraps a *requests.Response* to mimic a *urllib.addinfourl*.
6 unmodified lines
61 62 def getheaders(self, name):63 self._r.msg.getheaders(name)6460class CookieJar(cookielib.LWPCookieJar, collections.MutableMapping):65class CookieJar(cookielib.CookieJar, collections.MutableMapping):66 def extract_cookies(self, response, request):67 if response.raw._original_response:68 req = MockRequest(request)69 res = MockResponse(response.raw._original_response)
124 unmodified lines
modified:requests/models.py+22 added-25 removed
13 unmodified lines
14from .structures import CaseInsensitiveDict15from .status_codes import codes1617from .auth import HTTPBasicAuth, HTTPProxyAuth18from .packages.oreos.cookiejar import CookieJar19from .packages.urllib3.response import HTTPResponse20from .packages.urllib3.exceptions import MaxRetryError, LocationParseError21from .packages.urllib3.exceptions import SSLError as _SSLError22from .packages.urllib3.exceptions import HTTPError as _HTTPError
103 unmodified lines
126 #: Authentication tuple or object to attach to :class:`Request <Request>`.127 self.auth = auth128129 #: CookieJar to attach to :class:`Request <Request>`.129 self.cookies = dict(cookies or [])130 if isinstance(cookies, CookieJar):131 self.cookies = cookies132 else:133 self.cookies = CookieJar()134 135 # Add passed cookies in.136 if cookies is not None:137 self.cookies.update(cookies)138139 #: True if Request has been sent.140 self.sent = False141
47 unmodified lines
189190 # Pass settings over.191 response.config = self.config192193 # Save original response for later.194 response.raw = resp195 response.url = self.full_url196197 if resp:198199 # Fallback to None if there's no status_code, for whatever reason.200 response.status_code = getattr(resp, 'status', None)189201 202 # Make headers case-insensitive.203 response.headers = CaseInsensitiveDict(getattr(resp, 'headers', None))204205 # Set encoding.206 response.encoding = get_encoding_from_headers(response.headers)207196 # Start off with our local cookies.197 cookies = self.cookies or dict()198208 # Add new cookies from the server.200 if 'set-cookie' in response.headers:201 cookie_header = response.headers['set-cookie']202 cookies = dict_from_string(cookie_header)209 self.cookies.extract_cookies(response, self)210211 # Save cookies in Response.205 response.cookies = cookies212 response.cookies = self.cookies213214 # No exceptions were harmed in the making of this request.215 response.error = getattr(resp, 'error', None)216
9 unmodified lines
226 history = []227228 r = build(resp)229223 self.cookies.update(r.cookies)224230 if r.status_code in REDIRECT_STATI and not self.redirect:231232 while (('location' in r.headers) and233 ((r.status_code is codes.see_other) or (self.allow_redirects))):
69 unmodified lines
303 )304305 request.send()306 r = request.response302 self.cookies.update(r.cookies)307308 r.history = history309310 self.response = r311 self.response.request = self308 self.response.cookies.update(self.cookies)312313 @staticmethod314 def _encode_params(data):315 """Encode parameters in a piece of data.
259 unmodified lines
575 conn.cert_file = self.cert576577 if not self.sent or anyway:578576 if self.cookies:579 # Skip if 'cookie' header is explicitly set.580 if 'cookie' not in self.headers:581578 # Skip if 'cookie' header is explicitly set.579 if 'cookie' not in self.headers:580581 # Simple cookie with our dict.582 c = SimpleCookie()583 for (k, v) in list(self.cookies.items()):584 c[k] = v585586 # Turn it into a header.587 cookie_header = c.output(header='', sep='; ').strip()588582 # Turn it into a header.583 cookie_header = self.cookies.get_header(self)584 585 if cookie_header:586 # Attach Cookie header to request.587 self.headers['Cookie'] = cookie_header588589 # Pre-request hook.
270 unmodified lines
modified:requests/sessions.py+9 added-8 removed
12 unmodified lines
13from .models import Request14from .hooks import dispatch_hook15from .utils import header_expand16from .packages.urllib3.poolmanager import PoolManager17from .packages.oreos.cookiejar import CookieJar181819def merge_kwargs(local_kwarg, default_kwarg):20 """Merges kwarg dictionaries.2122 If a local key in the dictionary is set to None, it will be removed.
45 unmodified lines
68 verify=True,69 cert=None):7071 self.headers = headers or {}72 self.cookies = cookies or {}72 self.auth = auth73 self.timeout = timeout74 self.proxies = proxies or {}75 self.hooks = hooks or {}
8 unmodified lines
8485 self.init_poolmanager()8687 # Set up a CookieJar to be used by default88 if isinstance(cookies, CookieJar):89 self.cookies = {}89 self.cookies = cookies90 else:91 self.cookies = CookieJar()92 93 # Add passed cookies in.94 if cookies is not None:95 self.cookies.update(cookies)9691 # Add passed cookies in.92 if cookies is not None:93 self.cookies.update(cookies)9497 def init_poolmanager(self):98 self.poolmanager = PoolManager(99 num_pools=self.config.get('pool_connections'),100 maxsize=self.config.get('pool_maxsize')
48 unmodified lines
149150 method = str(method).upper()151152 # Default empty dicts for dict params.151 cookies = {} if cookies is None else cookies153 data = {} if data is None else data154 files = {} if files is None else files155 headers = {} if headers is None else headers156 params = {} if params is None else params
151 unmodified lines