OLD | NEW |
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 | 2 |
3 """ | 3 """ |
4 Compatibility code to be able to use `cookielib.CookieJar` with requests. | 4 Compatibility code to be able to use `cookielib.CookieJar` with requests. |
5 | 5 |
6 requests.utils imports from here, so be careful with imports. | 6 requests.utils imports from here, so be careful with imports. |
7 """ | 7 """ |
8 | 8 |
| 9 import time |
9 import collections | 10 import collections |
10 from .compat import cookielib, urlparse, Morsel | 11 from .compat import cookielib, urlparse, Morsel |
11 | 12 |
12 try: | 13 try: |
13 import threading | 14 import threading |
14 # grr, pyflakes: this fixes "redefinition of unused 'threading'" | 15 # grr, pyflakes: this fixes "redefinition of unused 'threading'" |
15 threading | 16 threading |
16 except ImportError: | 17 except ImportError: |
17 import dummy_threading as threading | 18 import dummy_threading as threading |
18 | 19 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 return self._new_headers | 67 return self._new_headers |
67 | 68 |
68 @property | 69 @property |
69 def unverifiable(self): | 70 def unverifiable(self): |
70 return self.is_unverifiable() | 71 return self.is_unverifiable() |
71 | 72 |
72 @property | 73 @property |
73 def origin_req_host(self): | 74 def origin_req_host(self): |
74 return self.get_origin_req_host() | 75 return self.get_origin_req_host() |
75 | 76 |
| 77 @property |
| 78 def host(self): |
| 79 return self.get_host() |
| 80 |
76 | 81 |
77 class MockResponse(object): | 82 class MockResponse(object): |
78 """Wraps a `httplib.HTTPMessage` to mimic a `urllib.addinfourl`. | 83 """Wraps a `httplib.HTTPMessage` to mimic a `urllib.addinfourl`. |
79 | 84 |
80 ...what? Basically, expose the parsed HTTP headers from the server response | 85 ...what? Basically, expose the parsed HTTP headers from the server response |
81 the way `cookielib` expects to see them. | 86 the way `cookielib` expects to see them. |
82 """ | 87 """ |
83 | 88 |
84 def __init__(self, headers): | 89 def __init__(self, headers): |
85 """Make a MockResponse for `cookielib` to read. | 90 """Make a MockResponse for `cookielib` to read. |
86 | 91 |
87 :param headers: a httplib.HTTPMessage or analogous carrying the headers | 92 :param headers: a httplib.HTTPMessage or analogous carrying the headers |
88 """ | 93 """ |
89 self._headers = headers | 94 self._headers = headers |
90 | 95 |
91 def info(self): | 96 def info(self): |
92 return self._headers | 97 return self._headers |
93 | 98 |
94 def getheaders(self, name): | 99 def getheaders(self, name): |
95 self._headers.getheaders(name) | 100 self._headers.getheaders(name) |
96 | 101 |
97 | 102 |
98 def extract_cookies_to_jar(jar, request, response): | 103 def extract_cookies_to_jar(jar, request, response): |
99 """Extract the cookies from the response into a CookieJar. | 104 """Extract the cookies from the response into a CookieJar. |
100 | 105 |
101 :param jar: cookielib.CookieJar (not necessarily a RequestsCookieJar) | 106 :param jar: cookielib.CookieJar (not necessarily a RequestsCookieJar) |
102 :param request: our own requests.Request object | 107 :param request: our own requests.Request object |
103 :param response: urllib3.HTTPResponse object | 108 :param response: urllib3.HTTPResponse object |
104 """ | 109 """ |
| 110 if not (hasattr(response, '_original_response') and |
| 111 response._original_response): |
| 112 return |
105 # the _original_response field is the wrapped httplib.HTTPResponse object, | 113 # the _original_response field is the wrapped httplib.HTTPResponse object, |
106 req = MockRequest(request) | 114 req = MockRequest(request) |
107 # pull out the HTTPMessage with the headers and put it in the mock: | 115 # pull out the HTTPMessage with the headers and put it in the mock: |
108 res = MockResponse(response._original_response.msg) | 116 res = MockResponse(response._original_response.msg) |
109 jar.extract_cookies(res, req) | 117 jar.extract_cookies(res, req) |
110 | 118 |
111 | 119 |
112 def get_cookie_header(jar, request): | 120 def get_cookie_header(jar, request): |
113 """Produce an appropriate Cookie header string to be sent with `request`, or
None.""" | 121 """Produce an appropriate Cookie header string to be sent with `request`, or
None.""" |
114 r = MockRequest(request) | 122 r = MockRequest(request) |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 """Dict-like __setitem__ for compatibility with client code. Throws exce
ption | 259 """Dict-like __setitem__ for compatibility with client code. Throws exce
ption |
252 if there is already a cookie of that name in the jar. In that case, use
the more | 260 if there is already a cookie of that name in the jar. In that case, use
the more |
253 explicit set() method instead.""" | 261 explicit set() method instead.""" |
254 | 262 |
255 self.set(name, value) | 263 self.set(name, value) |
256 | 264 |
257 def __delitem__(self, name): | 265 def __delitem__(self, name): |
258 """Deletes a cookie given a name. Wraps cookielib.CookieJar's remove_coo
kie_by_name().""" | 266 """Deletes a cookie given a name. Wraps cookielib.CookieJar's remove_coo
kie_by_name().""" |
259 remove_cookie_by_name(self, name) | 267 remove_cookie_by_name(self, name) |
260 | 268 |
| 269 def set_cookie(self, cookie, *args, **kwargs): |
| 270 if cookie.value.startswith('"') and cookie.value.endswith('"'): |
| 271 cookie.value = cookie.value.replace('\\"', '') |
| 272 return super(RequestsCookieJar, self).set_cookie(cookie, *args, **kwargs
) |
| 273 |
261 def update(self, other): | 274 def update(self, other): |
262 """Updates this jar with cookies from another CookieJar or dict-like""" | 275 """Updates this jar with cookies from another CookieJar or dict-like""" |
263 if isinstance(other, cookielib.CookieJar): | 276 if isinstance(other, cookielib.CookieJar): |
264 for cookie in other: | 277 for cookie in other: |
265 self.set_cookie(cookie) | 278 self.set_cookie(cookie) |
266 else: | 279 else: |
267 super(RequestsCookieJar, self).update(other) | 280 super(RequestsCookieJar, self).update(other) |
268 | 281 |
269 def _find(self, name, domain=None, path=None): | 282 def _find(self, name, domain=None, path=None): |
270 """Requests uses this method internally to get cookie values. Takes as a
rgs name | 283 """Requests uses this method internally to get cookie values. Takes as a
rgs name |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 result['port_specified'] = bool(result['port']) | 360 result['port_specified'] = bool(result['port']) |
348 result['domain_specified'] = bool(result['domain']) | 361 result['domain_specified'] = bool(result['domain']) |
349 result['domain_initial_dot'] = result['domain'].startswith('.') | 362 result['domain_initial_dot'] = result['domain'].startswith('.') |
350 result['path_specified'] = bool(result['path']) | 363 result['path_specified'] = bool(result['path']) |
351 | 364 |
352 return cookielib.Cookie(**result) | 365 return cookielib.Cookie(**result) |
353 | 366 |
354 | 367 |
355 def morsel_to_cookie(morsel): | 368 def morsel_to_cookie(morsel): |
356 """Convert a Morsel object into a Cookie containing the one k/v pair.""" | 369 """Convert a Morsel object into a Cookie containing the one k/v pair.""" |
| 370 expires = None |
| 371 if morsel["max-age"]: |
| 372 expires = time.time() + morsel["max-age"] |
| 373 elif morsel['expires']: |
| 374 expires = morsel['expires'] |
| 375 if type(expires) == type(""): |
| 376 time_template = "%a, %d-%b-%Y %H:%M:%S GMT" |
| 377 expires = time.mktime(time.strptime(expires, time_template)) |
357 c = create_cookie( | 378 c = create_cookie( |
358 name=morsel.key, | 379 name=morsel.key, |
359 value=morsel.value, | 380 value=morsel.value, |
360 version=morsel['version'] or 0, | 381 version=morsel['version'] or 0, |
361 port=None, | 382 port=None, |
362 port_specified=False, | |
363 domain=morsel['domain'], | 383 domain=morsel['domain'], |
364 domain_specified=bool(morsel['domain']), | |
365 domain_initial_dot=morsel['domain'].startswith('.'), | |
366 path=morsel['path'], | 384 path=morsel['path'], |
367 path_specified=bool(morsel['path']), | |
368 secure=bool(morsel['secure']), | 385 secure=bool(morsel['secure']), |
369 expires=morsel['max-age'] or morsel['expires'], | 386 expires=expires, |
370 discard=False, | 387 discard=False, |
371 comment=morsel['comment'], | 388 comment=morsel['comment'], |
372 comment_url=bool(morsel['comment']), | 389 comment_url=bool(morsel['comment']), |
373 rest={'HttpOnly': morsel['httponly']}, | 390 rest={'HttpOnly': morsel['httponly']}, |
374 rfc2109=False,) | 391 rfc2109=False,) |
375 return c | 392 return c |
376 | 393 |
377 | 394 |
378 def cookiejar_from_dict(cookie_dict, cookiejar=None): | 395 def cookiejar_from_dict(cookie_dict, cookiejar=None): |
379 """Returns a CookieJar from a key/value dictionary. | 396 """Returns a CookieJar from a key/value dictionary. |
380 | 397 |
381 :param cookie_dict: Dict of key/values to insert into CookieJar. | 398 :param cookie_dict: Dict of key/values to insert into CookieJar. |
382 """ | 399 """ |
383 if cookiejar is None: | 400 if cookiejar is None: |
384 cookiejar = RequestsCookieJar() | 401 cookiejar = RequestsCookieJar() |
385 | 402 |
386 if cookie_dict is not None: | 403 if cookie_dict is not None: |
387 for name in cookie_dict: | 404 for name in cookie_dict: |
388 cookiejar.set_cookie(create_cookie(name, cookie_dict[name])) | 405 cookiejar.set_cookie(create_cookie(name, cookie_dict[name])) |
389 return cookiejar | 406 return cookiejar |
OLD | NEW |