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
Provenance
- Commit
c4ab96b- Tree
5dc6eba- Author
miguel <miguel@immunityinc.com>- Committer
Shivaram Lingamneni <slingamn@cs.stanford.edu>- Parent
fed6cfb
3 files changed+40 added−37 removed
modified:requests/packages/oreos/cookiejar.py+9 added-4 removed
1+"""2+Wrapper for CookieJar allow dict-like access.3+Ideally there would be wrappers for LWPCookieJar and MozillaCookieJar as well.4+"""5+6 import urlparse7 import Cookie8 from Cookie import Morsel9 import cookielib12 unmodified lines22 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 lines46 self._r.headers[name] = value47 48 def is_unverifiable(self):49 # unverifiable == redirected45- return bool(self.history)50+ return bool(self._r.response.history)51 52 class MockResponse:53 """54 Wraps a *requests.Response* to mimic a *urllib.addinfourl*.6 unmodified lines61 62 def getheaders(self, name):63 self._r.msg.getheaders(name)64 60-class CookieJar(cookielib.LWPCookieJar, collections.MutableMapping):65+class 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 linesmodified:requests/models.py+22 added-25 removed
13 unmodified lines14 from .structures import CaseInsensitiveDict15 from .status_codes import codes16 17 from .auth import HTTPBasicAuth, HTTPProxyAuth18+from .packages.oreos.cookiejar import CookieJar19 from .packages.urllib3.response import HTTPResponse20 from .packages.urllib3.exceptions import MaxRetryError, LocationParseError21 from .packages.urllib3.exceptions import SSLError as _SSLError22 from .packages.urllib3.exceptions import HTTPError as _HTTPError103 unmodified lines126 #: Authentication tuple or object to attach to :class:`Request <Request>`.127 self.auth = auth128 129 #: 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)138 139 #: True if Request has been sent.140 self.sent = False141 47 unmodified lines189 190 # Pass settings over.191 response.config = self.config192 193+ # Save original response for later.194+ response.raw = resp195+ response.url = self.full_url196+197 if resp:198 199 # Fallback to None if there's no status_code, for whatever reason.200 response.status_code = getattr(resp, 'status', None)189-201+ 202 # Make headers case-insensitive.203 response.headers = CaseInsensitiveDict(getattr(resp, 'headers', None))204 205 # Set encoding.206 response.encoding = get_encoding_from_headers(response.headers)207 196- # Start off with our local cookies.197- cookies = self.cookies or dict()198-208 # 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)210 211 # Save cookies in Response.205- response.cookies = cookies212+ response.cookies = self.cookies213 214 # No exceptions were harmed in the making of this request.215 response.error = getattr(resp, 'error', None)216 9 unmodified lines226 history = []227 228 r = build(resp)229 223- self.cookies.update(r.cookies)224-230 if r.status_code in REDIRECT_STATI and not self.redirect:231 232 while (('location' in r.headers) and233 ((r.status_code is codes.see_other) or (self.allow_redirects))):69 unmodified lines303 )304 305 request.send()306 r = request.response302- self.cookies.update(r.cookies)307 308 r.history = history309 310 self.response = r311 self.response.request = self308- self.response.cookies.update(self.cookies)312 313 @staticmethod314 def _encode_params(data):315 """Encode parameters in a piece of data.259 unmodified lines575 conn.cert_file = self.cert576 577 if not self.sent or anyway:578 576- if self.cookies:579+ # Skip if 'cookie' header is explicitly set.580+ if 'cookie' not in self.headers:581 578- # Skip if 'cookie' header is explicitly set.579- if 'cookie' not in self.headers:580-581- # Simple cookie with our dict.582- c = SimpleCookie()583- for (k, v) in list(self.cookies.items()):584- c[k] = v585-586- # Turn it into a header.587- cookie_header = c.output(header='', sep='; ').strip()588-582+ # 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_header588 589 # Pre-request hook.270 unmodified linesmodified:requests/sessions.py+9 added-8 removed
12 unmodified lines13 from .models import Request14 from .hooks import dispatch_hook15 from .utils import header_expand16 from .packages.urllib3.poolmanager import PoolManager17+from .packages.oreos.cookiejar import CookieJar18 18-19 def merge_kwargs(local_kwarg, default_kwarg):20 """Merges kwarg dictionaries.21 22 If a local key in the dictionary is set to None, it will be removed.45 unmodified lines68 verify=True,69 cert=None):70 71 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 lines84 85 self.init_poolmanager()86 87 # 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)96 91- # Add passed cookies in.92- if cookies is not None:93- self.cookies.update(cookies)94-97 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 lines149 150 method = str(method).upper()151 152 # 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 params151 unmodified lines