OLD | NEW |
---|---|
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
Vadim Sh.
2013/09/27 22:28:46
Really modified file #2.
| |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Classes and functions for generic network communication over HTTP.""" | 5 """Classes and functions for generic network communication over HTTP.""" |
6 | 6 |
7 import cookielib | 7 import cookielib |
8 import cStringIO as StringIO | 8 import cStringIO as StringIO |
9 import httplib | 9 import httplib |
10 import itertools | 10 import itertools |
11 import logging | 11 import logging |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
378 logging.error( | 378 logging.error( |
379 'Authentication is required for %s on attempt %d.\n%s', | 379 'Authentication is required for %s on attempt %d.\n%s', |
380 request.get_full_url(), attempt.attempt, e.format()) | 380 request.get_full_url(), attempt.attempt, e.format()) |
381 # Try to authenticate only once. If it doesn't help, then server does | 381 # Try to authenticate only once. If it doesn't help, then server does |
382 # not support app engine authentication. | 382 # not support app engine authentication. |
383 if not auth_attempted: | 383 if not auth_attempted: |
384 auth_attempted = True | 384 auth_attempted = True |
385 if self.authenticator and self.authenticator.authenticate(): | 385 if self.authenticator and self.authenticator.authenticate(): |
386 # Success! Run request again immediately. | 386 # Success! Run request again immediately. |
387 attempt.skip_sleep = True | 387 attempt.skip_sleep = True |
388 # Also refresh cookies used by request engine. | |
389 self.engine.reload_cookies() | |
388 continue | 390 continue |
389 # Authentication attempt was unsuccessful. | 391 # Authentication attempt was unsuccessful. |
390 logging.error( | 392 logging.error( |
391 'Unable to authenticate to %s.\n%s', | 393 'Unable to authenticate to %s.\n%s', |
392 request.get_full_url(), e.format(verbose=True)) | 394 request.get_full_url(), e.format(verbose=True)) |
393 return None | 395 return None |
394 | 396 |
395 # Hit a error that can not be retried -> stop retry loop. | 397 # Hit a error that can not be retried -> stop retry loop. |
396 if not self.is_transient_http_error(e.code, retry_404, retry_50x): | 398 if not self.is_transient_http_error(e.code, retry_404, retry_50x): |
397 # This HttpError means we reached the server and there was a problem | 399 # This HttpError means we reached the server and there was a problem |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
499 | 501 |
500 Returns HttpResponse. | 502 Returns HttpResponse. |
501 | 503 |
502 Raises: | 504 Raises: |
503 ConnectionError - failed to establish connection to the server. | 505 ConnectionError - failed to establish connection to the server. |
504 TimeoutError - timeout while connecting or reading response. | 506 TimeoutError - timeout while connecting or reading response. |
505 HttpError - server responded with >= 400 error code. | 507 HttpError - server responded with >= 400 error code. |
506 """ | 508 """ |
507 raise NotImplementedError() | 509 raise NotImplementedError() |
508 | 510 |
511 def reload_cookies(self): | |
512 """Reloads cookies from original cookie jar.""" | |
513 # This method is optional. | |
514 pass | |
515 | |
509 | 516 |
510 class Authenticator(object): | 517 class Authenticator(object): |
511 """Base class for objects that know how to authenticate into http services.""" | 518 """Base class for objects that know how to authenticate into http services.""" |
512 | 519 |
513 def authenticate(self): | 520 def authenticate(self): |
514 """Authenticates in the app engine service.""" | 521 """Authenticates in the app engine service.""" |
515 raise NotImplementedError() | 522 raise NotImplementedError() |
516 | 523 |
517 | 524 |
518 class Urllib2Engine(RequestEngine): | 525 class Urllib2Engine(RequestEngine): |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
551 # Preferred number of connections in a connection pool. | 558 # Preferred number of connections in a connection pool. |
552 CONNECTION_POOL_SIZE = 64 | 559 CONNECTION_POOL_SIZE = 64 |
553 # If True will not open more than CONNECTION_POOL_SIZE connections. | 560 # If True will not open more than CONNECTION_POOL_SIZE connections. |
554 CONNECTION_POOL_BLOCK = False | 561 CONNECTION_POOL_BLOCK = False |
555 # Maximum number of internal connection retries in a connection pool. | 562 # Maximum number of internal connection retries in a connection pool. |
556 CONNECTION_RETRIES = 0 | 563 CONNECTION_RETRIES = 0 |
557 | 564 |
558 def __init__(self, cookie_jar, ca_certs): | 565 def __init__(self, cookie_jar, ca_certs): |
559 super(RequestsLibEngine, self).__init__() | 566 super(RequestsLibEngine, self).__init__() |
560 self.session = requests.Session() | 567 self.session = requests.Session() |
568 self.cookie_jar = cookie_jar | |
561 # Configure session. | 569 # Configure session. |
562 self.session.trust_env = False | 570 self.session.trust_env = False |
563 if cookie_jar: | 571 if cookie_jar: |
564 self.session.cookies = cookie_jar | 572 self.session.cookies = cookie_jar |
565 self.session.verify = ca_certs | 573 self.session.verify = ca_certs |
566 # Configure connection pools. | 574 # Configure connection pools. |
567 for protocol in ('https://', 'http://'): | 575 for protocol in ('https://', 'http://'): |
568 self.session.mount(protocol, adapters.HTTPAdapter( | 576 self.session.mount(protocol, adapters.HTTPAdapter( |
569 pool_connections=self.CONNECTION_POOL_SIZE, | 577 pool_connections=self.CONNECTION_POOL_SIZE, |
570 pool_maxsize=self.CONNECTION_POOL_SIZE, | 578 pool_maxsize=self.CONNECTION_POOL_SIZE, |
(...skipping 16 matching lines...) Expand all Loading... | |
587 else: | 595 else: |
588 stream = StringIO.StringIO(response.content) | 596 stream = StringIO.StringIO(response.content) |
589 return HttpResponse(stream, request.get_full_url(), response.headers) | 597 return HttpResponse(stream, request.get_full_url(), response.headers) |
590 except requests.Timeout as e: | 598 except requests.Timeout as e: |
591 raise TimeoutError(e) | 599 raise TimeoutError(e) |
592 except requests.HTTPError as e: | 600 except requests.HTTPError as e: |
593 raise HttpError(e.response.status_code, e) | 601 raise HttpError(e.response.status_code, e) |
594 except (requests.ConnectionError, socket.timeout, ssl.SSLError) as e: | 602 except (requests.ConnectionError, socket.timeout, ssl.SSLError) as e: |
595 raise ConnectionError(e) | 603 raise ConnectionError(e) |
596 | 604 |
605 def reload_cookies(self): | |
606 if self.cookie_jar: | |
607 self.session.cookies = self.cookie_jar | |
608 | |
597 | 609 |
598 class AppEngineAuthenticator(Authenticator): | 610 class AppEngineAuthenticator(Authenticator): |
599 """Helper class to perform AppEngine authentication dance via upload.py.""" | 611 """Helper class to perform AppEngine authentication dance via upload.py.""" |
600 | 612 |
601 # This lock ensures that user won't be confused with multiple concurrent | 613 # This lock ensures that user won't be confused with multiple concurrent |
602 # login prompts. | 614 # login prompts. |
603 _auth_lock = threading.Lock() | 615 _auth_lock = threading.Lock() |
604 | 616 |
605 def __init__(self, urlhost, cookie_jar, email=None, password=None): | 617 def __init__(self, urlhost, cookie_jar, email=None, password=None): |
606 super(AppEngineAuthenticator, self).__init__() | 618 super(AppEngineAuthenticator, self).__init__() |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
765 attemp_obj = RetryAttempt(attempt, remaining) | 777 attemp_obj = RetryAttempt(attempt, remaining) |
766 yield attemp_obj | 778 yield attemp_obj |
767 if attemp_obj.skip_sleep: | 779 if attemp_obj.skip_sleep: |
768 continue | 780 continue |
769 # Only sleep if we are going to try again. | 781 # Only sleep if we are going to try again. |
770 if max_attempts and attempt != max_attempts - 1: | 782 if max_attempts and attempt != max_attempts - 1: |
771 remaining = (timeout - (current_time() - start)) if timeout else None | 783 remaining = (timeout - (current_time() - start)) if timeout else None |
772 if remaining is not None and remaining < 0: | 784 if remaining is not None and remaining < 0: |
773 break | 785 break |
774 sleep_before_retry(attempt, remaining) | 786 sleep_before_retry(attempt, remaining) |
OLD | NEW |