<s> from __future__ import generators <EOL> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __contributors__ = [ "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> __license__ = "<STR_LIT>" <EOL> __version__ = "<STR_LIT>" <EOL> import re <EOL> import sys <EOL> import email <EOL> import email . Utils <EOL> import email . Message <EOL> import email . FeedParser <EOL> import StringIO <EOL> import gzip <EOL> import zlib <EOL> import httplib <EOL> import urlparse <EOL> import base64 <EOL> import os <EOL> import copy <EOL> import calendar <EOL> import time <EOL> import random <EOL> import errno <EOL> try : <EOL> from hashlib import sha1 as _sha , md5 as _md5 <EOL> except ImportError : <EOL> import sha <EOL> import md5 <EOL> _sha = sha . new <EOL> _md5 = md5 . new <EOL> import hmac <EOL> from gettext import gettext as _ <EOL> import socket <EOL> try : <EOL> from httplib2 import socks <EOL> except ImportError : <EOL> socks = None <EOL> try : <EOL> import ssl <EOL> ssl_SSLError = ssl . SSLError <EOL> def _ssl_wrap_socket ( sock , key_file , cert_file , <EOL> disable_validation , ca_certs ) : <EOL> if disable_validation : <EOL> cert_reqs = ssl . CERT_NONE <EOL> else : <EOL> cert_reqs = ssl . CERT_REQUIRED <EOL> return ssl . wrap_socket ( sock , keyfile = key_file , certfile = cert_file , <EOL> cert_reqs = cert_reqs , ca_certs = ca_certs ) <EOL> except ( AttributeError , ImportError ) : <EOL> ssl_SSLError = None <EOL> def _ssl_wrap_socket ( sock , key_file , cert_file , <EOL> disable_validation , ca_certs ) : <EOL> if not disable_validation : <EOL> raise CertificateValidationUnsupported ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ssl_sock = socket . ssl ( sock , key_file , cert_file ) <EOL> return httplib . FakeSocket ( sock , ssl_sock ) <EOL> if sys . version_info >= ( <NUM_LIT:2> , <NUM_LIT:3> ) : <EOL> from iri2uri import iri2uri <EOL> else : <EOL> def iri2uri ( uri ) : <EOL> return uri <EOL> def has_timeout ( timeout ) : <EOL> if hasattr ( socket , '<STR_LIT>' ) : <EOL> return ( timeout is not None and timeout is not socket . _GLOBAL_DEFAULT_TIMEOUT ) <EOL> return ( timeout is not None ) <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> debuglevel = <NUM_LIT:0> <EOL> if sys . version_info < ( <NUM_LIT:2> , <NUM_LIT:4> ) : <EOL> def sorted ( seq ) : <EOL> seq . sort ( ) <EOL> return seq <EOL> def HTTPResponse__getheaders ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . msg is None : <EOL> raise httplib . ResponseNotReady ( ) <EOL> return self . msg . items ( ) <EOL> if not hasattr ( httplib . HTTPResponse , '<STR_LIT>' ) : <EOL> httplib . HTTPResponse . getheaders = HTTPResponse__getheaders <EOL> class HttpLib2Error ( Exception ) : pass <EOL> class HttpLib2ErrorWithResponse ( HttpLib2Error ) : <EOL> def __init__ ( self , desc , response , content ) : <EOL> self . response = response <EOL> self . content = content <EOL> HttpLib2Error . __init__ ( self , desc ) <EOL> class RedirectMissingLocation ( HttpLib2ErrorWithResponse ) : pass <EOL> class RedirectLimit ( HttpLib2ErrorWithResponse ) : pass <EOL> class FailedToDecompressContent ( HttpLib2ErrorWithResponse ) : pass <EOL> class UnimplementedDigestAuthOptionError ( HttpLib2ErrorWithResponse ) : pass <EOL> class UnimplementedHmacDigestAuthOptionError ( HttpLib2ErrorWithResponse ) : pass <EOL> class MalformedHeader ( HttpLib2Error ) : pass <EOL> class RelativeURIError ( HttpLib2Error ) : pass <EOL> class ServerNotFoundError ( HttpLib2Error ) : pass <EOL> class ProxiesUnavailableError ( HttpLib2Error ) : pass <EOL> class CertificateValidationUnsupported ( HttpLib2Error ) : pass <EOL> class SSLHandshakeError ( HttpLib2Error ) : pass <EOL> class NotSupportedOnThisPlatform ( HttpLib2Error ) : pass <EOL> class CertificateHostnameMismatch ( SSLHandshakeError ) : <EOL> def __init__ ( self , desc , host , cert ) : <EOL> HttpLib2Error . __init__ ( self , desc ) <EOL> self . host = host <EOL> self . cert = cert <EOL> DEFAULT_MAX_REDIRECTS = <NUM_LIT:5> <EOL> CA_CERTS = "<STR_LIT>" <EOL> HOP_BY_HOP = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def _get_end2end_headers ( response ) : <EOL> hopbyhop = list ( HOP_BY_HOP ) <EOL> hopbyhop . extend ( [ x . strip ( ) for x in response . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) ] ) <EOL> return [ header for header in response . keys ( ) if header not in hopbyhop ] <EOL> URI = re . compile ( r"<STR_LIT>" ) <EOL> def parse_uri ( uri ) : <EOL> """<STR_LIT>""" <EOL> groups = URI . match ( uri ) . groups ( ) <EOL> return ( groups [ <NUM_LIT:1> ] , groups [ <NUM_LIT:3> ] , groups [ <NUM_LIT:4> ] , groups [ <NUM_LIT:6> ] , groups [ <NUM_LIT:8> ] ) <EOL> def urlnorm ( uri ) : <EOL> ( scheme , authority , path , query , fragment ) = parse_uri ( uri ) <EOL> if not scheme or not authority : <EOL> raise RelativeURIError ( "<STR_LIT>" % uri ) <EOL> authority = authority . lower ( ) <EOL> scheme = scheme . lower ( ) <EOL> if not path : <EOL> path = "<STR_LIT:/>" <EOL> request_uri = query and "<STR_LIT:?>" . join ( [ path , query ] ) or path <EOL> scheme = scheme . lower ( ) <EOL> defrag_uri = scheme + "<STR_LIT>" + authority + request_uri <EOL> return scheme , authority , request_uri , defrag_uri <EOL> re_url_scheme = re . compile ( r'<STR_LIT>' ) <EOL> re_slash = re . compile ( r'<STR_LIT>' ) <EOL> def safename ( filename ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if re_url_scheme . match ( filename ) : <EOL> if isinstance ( filename , str ) : <EOL> filename = filename . decode ( '<STR_LIT:utf-8>' ) <EOL> filename = filename . encode ( '<STR_LIT>' ) <EOL> else : <EOL> filename = filename . encode ( '<STR_LIT>' ) <EOL> except UnicodeError : <EOL> pass <EOL> if isinstance ( filename , unicode ) : <EOL> filename = filename . encode ( '<STR_LIT:utf-8>' ) <EOL> filemd5 = _md5 ( filename ) . hexdigest ( ) <EOL> filename = re_url_scheme . sub ( "<STR_LIT>" , filename ) <EOL> filename = re_slash . sub ( "<STR_LIT:U+002C>" , filename ) <EOL> if len ( filename ) > <NUM_LIT:200> : <EOL> filename = filename [ : <NUM_LIT:200> ] <EOL> return "<STR_LIT:U+002C>" . join ( ( filename , filemd5 ) ) <EOL> NORMALIZE_SPACE = re . compile ( r'<STR_LIT>' ) <EOL> def _normalize_headers ( headers ) : <EOL> return dict ( [ ( key . lower ( ) , NORMALIZE_SPACE . sub ( value , '<STR_LIT:U+0020>' ) . strip ( ) ) for ( key , value ) in headers . iteritems ( ) ] ) <EOL> def _parse_cache_control ( headers ) : <EOL> retval = { } <EOL> if headers . has_key ( '<STR_LIT>' ) : <EOL> parts = headers [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) <EOL> parts_with_args = [ tuple ( [ x . strip ( ) . lower ( ) for x in part . split ( "<STR_LIT:=>" , <NUM_LIT:1> ) ] ) for part in parts if - <NUM_LIT:1> != part . find ( "<STR_LIT:=>" ) ] <EOL> parts_wo_args = [ ( name . strip ( ) . lower ( ) , <NUM_LIT:1> ) for name in parts if - <NUM_LIT:1> == name . find ( "<STR_LIT:=>" ) ] <EOL> retval = dict ( parts_with_args + parts_wo_args ) <EOL> return retval <EOL> USE_WWW_AUTH_STRICT_PARSING = <NUM_LIT:0> <EOL> WWW_AUTH_STRICT = re . compile ( r"<STR_LIT>" ) <EOL> WWW_AUTH_RELAXED = re . compile ( r"<STR_LIT>" ) <EOL> UNQUOTE_PAIRS = re . compile ( r'<STR_LIT>' ) <EOL> def _parse_www_authenticate ( headers , headername = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> retval = { } <EOL> if headers . has_key ( headername ) : <EOL> try : <EOL> authenticate = headers [ headername ] . strip ( ) <EOL> www_auth = USE_WWW_AUTH_STRICT_PARSING and WWW_AUTH_STRICT or WWW_AUTH_RELAXED <EOL> while authenticate : <EOL> if headername == '<STR_LIT>' : <EOL> ( auth_scheme , the_rest ) = ( '<STR_LIT>' , authenticate ) <EOL> else : <EOL> ( auth_scheme , the_rest ) = authenticate . split ( "<STR_LIT:U+0020>" , <NUM_LIT:1> ) <EOL> match = www_auth . search ( the_rest ) <EOL> auth_params = { } <EOL> while match : <EOL> if match and len ( match . groups ( ) ) == <NUM_LIT:3> : <EOL> ( key , value , the_rest ) = match . groups ( ) <EOL> auth_params [ key . lower ( ) ] = UNQUOTE_PAIRS . sub ( r'<STR_LIT>' , value ) <EOL> match = www_auth . search ( the_rest ) <EOL> retval [ auth_scheme . lower ( ) ] = auth_params <EOL> authenticate = the_rest . strip ( ) <EOL> except ValueError : <EOL> raise MalformedHeader ( "<STR_LIT>" ) <EOL> return retval <EOL> def _entry_disposition ( response_headers , request_headers ) : <EOL> """<STR_LIT>""" <EOL> retval = "<STR_LIT>" <EOL> cc = _parse_cache_control ( request_headers ) <EOL> cc_response = _parse_cache_control ( response_headers ) <EOL> if request_headers . has_key ( '<STR_LIT>' ) and request_headers [ '<STR_LIT>' ] . lower ( ) . find ( '<STR_LIT>' ) != - <NUM_LIT:1> : <EOL> retval = "<STR_LIT>" <EOL> if '<STR_LIT>' not in request_headers : <EOL> request_headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> elif cc . has_key ( '<STR_LIT>' ) : <EOL> retval = "<STR_LIT>" <EOL> elif cc_response . has_key ( '<STR_LIT>' ) : <EOL> retval = "<STR_LIT>" <EOL> elif cc . has_key ( '<STR_LIT>' ) : <EOL> retval = "<STR_LIT>" <EOL> elif response_headers . has_key ( '<STR_LIT:date>' ) : <EOL> date = calendar . timegm ( email . Utils . parsedate_tz ( response_headers [ '<STR_LIT:date>' ] ) ) <EOL> now = time . time ( ) <EOL> current_age = max ( <NUM_LIT:0> , now - date ) <EOL> if cc_response . has_key ( '<STR_LIT>' ) : <EOL> try : <EOL> freshness_lifetime = int ( cc_response [ '<STR_LIT>' ] ) <EOL> except ValueError : <EOL> freshness_lifetime = <NUM_LIT:0> <EOL> elif response_headers . has_key ( '<STR_LIT>' ) : <EOL> expires = email . Utils . parsedate_tz ( response_headers [ '<STR_LIT>' ] ) <EOL> if None == expires : <EOL> freshness_lifetime = <NUM_LIT:0> <EOL> else : <EOL> freshness_lifetime = max ( <NUM_LIT:0> , calendar . timegm ( expires ) - date ) <EOL> else : <EOL> freshness_lifetime = <NUM_LIT:0> <EOL> if cc . has_key ( '<STR_LIT>' ) : <EOL> try : <EOL> freshness_lifetime = int ( cc [ '<STR_LIT>' ] ) <EOL> except ValueError : <EOL> freshness_lifetime = <NUM_LIT:0> <EOL> if cc . has_key ( '<STR_LIT>' ) : <EOL> try : <EOL> min_fresh = int ( cc [ '<STR_LIT>' ] ) <EOL> except ValueError : <EOL> min_fresh = <NUM_LIT:0> <EOL> current_age += min_fresh <EOL> if freshness_lifetime > current_age : <EOL> retval = "<STR_LIT>" <EOL> return retval <EOL> def _decompressContent ( response , new_content ) : <EOL> content = new_content <EOL> try : <EOL> encoding = response . get ( '<STR_LIT>' , None ) <EOL> if encoding in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if encoding == '<STR_LIT>' : <EOL> content = gzip . GzipFile ( fileobj = StringIO . StringIO ( new_content ) ) . read ( ) <EOL> if encoding == '<STR_LIT>' : <EOL> content = zlib . decompress ( content ) <EOL> response [ '<STR_LIT>' ] = str ( len ( content ) ) <EOL> response [ '<STR_LIT>' ] = response [ '<STR_LIT>' ] <EOL> del response [ '<STR_LIT>' ] <EOL> except IOError : <EOL> content = "<STR_LIT>" <EOL> raise FailedToDecompressContent ( _ ( "<STR_LIT>" ) % response . get ( '<STR_LIT>' ) , response , content ) <EOL> return content <EOL> def _updateCache ( request_headers , response_headers , content , cache , cachekey ) : <EOL> if cachekey : <EOL> cc = _parse_cache_control ( request_headers ) <EOL> cc_response = _parse_cache_control ( response_headers ) <EOL> if cc . has_key ( '<STR_LIT>' ) or cc_response . has_key ( '<STR_LIT>' ) : <EOL> cache . delete ( cachekey ) <EOL> else : <EOL> info = email . Message . Message ( ) <EOL> for key , value in response_headers . iteritems ( ) : <EOL> if key not in [ '<STR_LIT:status>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> info [ key ] = value <EOL> vary = response_headers . get ( '<STR_LIT>' , None ) <EOL> if vary : <EOL> vary_headers = vary . lower ( ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) <EOL> for header in vary_headers : <EOL> key = '<STR_LIT>' % header <EOL> try : <EOL> info [ key ] = request_headers [ header ] <EOL> except KeyError : <EOL> pass <EOL> status = response_headers . status <EOL> if status == <NUM_LIT> : <EOL> status = <NUM_LIT:200> <EOL> status_header = '<STR_LIT>' % status <EOL> header_str = info . as_string ( ) <EOL> header_str = re . sub ( "<STR_LIT>" , "<STR_LIT:\r\n>" , header_str ) <EOL> text = "<STR_LIT>" . join ( [ status_header , header_str , content ] ) <EOL> cache . set ( cachekey , text ) <EOL> def _cnonce ( ) : <EOL> dig = _md5 ( "<STR_LIT>" % ( time . ctime ( ) , [ "<STR_LIT>" [ random . randrange ( <NUM_LIT:0> , <NUM_LIT:9> ) ] for i in range ( <NUM_LIT:20> ) ] ) ) . hexdigest ( ) <EOL> return dig [ : <NUM_LIT:16> ] <EOL> def _wsse_username_token ( cnonce , iso_now , password ) : <EOL> return base64 . b64encode ( _sha ( "<STR_LIT>" % ( cnonce , iso_now , password ) ) . digest ( ) ) . strip ( ) <EOL> class Authentication ( object ) : <EOL> def __init__ ( self , credentials , host , request_uri , headers , response , content , http ) : <EOL> ( scheme , authority , path , query , fragment ) = parse_uri ( request_uri ) <EOL> self . path = path <EOL> self . host = host <EOL> self . credentials = credentials <EOL> self . http = http <EOL> def depth ( self , request_uri ) : <EOL> ( scheme , authority , path , query , fragment ) = parse_uri ( request_uri ) <EOL> return request_uri [ len ( self . path ) : ] . count ( "<STR_LIT:/>" ) <EOL> def inscope ( self , host , request_uri ) : <EOL> ( scheme , authority , path , query , fragment ) = parse_uri ( request_uri ) <EOL> return ( host == self . host ) and path . startswith ( self . path ) <EOL> def request ( self , method , request_uri , headers , content ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def response ( self , response , content ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> class BasicAuthentication ( Authentication ) : <EOL> def __init__ ( self , credentials , host , request_uri , headers , response , content , http ) : <EOL> Authentication . __init__ ( self , credentials , host , request_uri , headers , response , content , http ) <EOL> def request ( self , method , request_uri , headers , content ) : <EOL> """<STR_LIT>""" <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' + base64 . b64encode ( "<STR_LIT>" % self . credentials ) . strip ( ) <EOL> class DigestAuthentication ( Authentication ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , credentials , host , request_uri , headers , response , content , http ) : <EOL> Authentication . __init__ ( self , credentials , host , request_uri , headers , response , content , http ) <EOL> challenge = _parse_www_authenticate ( response , '<STR_LIT>' ) <EOL> self . challenge = challenge [ '<STR_LIT>' ] <EOL> qop = self . challenge . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . challenge [ '<STR_LIT>' ] = ( '<STR_LIT>' in [ x . strip ( ) for x in qop . split ( ) ] ) and '<STR_LIT>' or None <EOL> if self . challenge [ '<STR_LIT>' ] is None : <EOL> raise UnimplementedDigestAuthOptionError ( _ ( "<STR_LIT>" % qop ) ) <EOL> self . challenge [ '<STR_LIT>' ] = self . challenge . get ( '<STR_LIT>' , '<STR_LIT>' ) . upper ( ) <EOL> if self . challenge [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> raise UnimplementedDigestAuthOptionError ( _ ( "<STR_LIT>" % self . challenge [ '<STR_LIT>' ] ) ) <EOL> self . A1 = "<STR_LIT>" . join ( [ self . credentials [ <NUM_LIT:0> ] , "<STR_LIT::>" , self . challenge [ '<STR_LIT>' ] , "<STR_LIT::>" , self . credentials [ <NUM_LIT:1> ] ] ) <EOL> self . challenge [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> def request ( self , method , request_uri , headers , content , cnonce = None ) : <EOL> """<STR_LIT>""" <EOL> H = lambda x : _md5 ( x ) . hexdigest ( ) <EOL> KD = lambda s , d : H ( "<STR_LIT>" % ( s , d ) ) <EOL> A2 = "<STR_LIT>" . join ( [ method , "<STR_LIT::>" , request_uri ] ) <EOL> self . challenge [ '<STR_LIT>' ] = cnonce or _cnonce ( ) <EOL> request_digest = '<STR_LIT>' % KD ( H ( self . A1 ) , "<STR_LIT>" % ( self . challenge [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' % self . challenge [ '<STR_LIT>' ] , <EOL> self . challenge [ '<STR_LIT>' ] , <EOL> self . challenge [ '<STR_LIT>' ] , H ( A2 ) <EOL> ) ) <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' % ( <EOL> self . credentials [ <NUM_LIT:0> ] , <EOL> self . challenge [ '<STR_LIT>' ] , <EOL> self . challenge [ '<STR_LIT>' ] , <EOL> request_uri , <EOL> self . challenge [ '<STR_LIT>' ] , <EOL> request_digest , <EOL> self . challenge [ '<STR_LIT>' ] , <EOL> self . challenge [ '<STR_LIT>' ] , <EOL> self . challenge [ '<STR_LIT>' ] , <EOL> ) <EOL> if self . challenge . get ( '<STR_LIT>' ) : <EOL> headers [ '<STR_LIT>' ] += '<STR_LIT>' % self . challenge [ '<STR_LIT>' ] <EOL> self . challenge [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> def response ( self , response , content ) : <EOL> if not response . has_key ( '<STR_LIT>' ) : <EOL> challenge = _parse_www_authenticate ( response , '<STR_LIT>' ) . get ( '<STR_LIT>' , { } ) <EOL> if '<STR_LIT:true>' == challenge . get ( '<STR_LIT>' ) : <EOL> self . challenge [ '<STR_LIT>' ] = challenge [ '<STR_LIT>' ] <EOL> self . challenge [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> return True <EOL> else : <EOL> updated_challenge = _parse_www_authenticate ( response , '<STR_LIT>' ) . get ( '<STR_LIT>' , { } ) <EOL> if updated_challenge . has_key ( '<STR_LIT>' ) : <EOL> self . challenge [ '<STR_LIT>' ] = updated_challenge [ '<STR_LIT>' ] <EOL> self . challenge [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> return False <EOL> class HmacDigestAuthentication ( Authentication ) : <EOL> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> def __init__ ( self , credentials , host , request_uri , headers , response , content , http ) : <EOL> Authentication . __init__ ( self , credentials , host , request_uri , headers , response , content , http ) <EOL> challenge = _parse_www_authenticate ( response , '<STR_LIT>' ) <EOL> self . challenge = challenge [ '<STR_LIT>' ] <EOL> self . challenge [ '<STR_LIT>' ] = self . challenge . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if self . challenge [ '<STR_LIT>' ] not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . challenge [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . challenge [ '<STR_LIT>' ] = self . challenge . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not self . challenge . get ( '<STR_LIT>' ) : <EOL> raise UnimplementedHmacDigestAuthOptionError ( _ ( "<STR_LIT>" ) ) <EOL> self . challenge [ '<STR_LIT>' ] = self . challenge . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if self . challenge [ '<STR_LIT>' ] not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise UnimplementedHmacDigestAuthOptionError ( _ ( "<STR_LIT>" % self . challenge [ '<STR_LIT>' ] ) ) <EOL> self . challenge [ '<STR_LIT>' ] = self . challenge . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if self . challenge [ '<STR_LIT>' ] not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise UnimplementedHmacDigestAuthOptionError ( _ ( "<STR_LIT>" % self . challenge [ '<STR_LIT>' ] ) ) <EOL> if self . challenge [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> self . hashmod = _md5 <EOL> else : <EOL> self . hashmod = _sha <EOL> if self . challenge [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> self . pwhashmod = _md5 <EOL> else : <EOL> self . pwhashmod = _sha <EOL> self . key = "<STR_LIT>" . join ( [ self . credentials [ <NUM_LIT:0> ] , "<STR_LIT::>" , <EOL> self . pwhashmod . new ( "<STR_LIT>" . join ( [ self . credentials [ <NUM_LIT:1> ] , self . challenge [ '<STR_LIT>' ] ] ) ) . hexdigest ( ) . lower ( ) , <EOL> "<STR_LIT::>" , self . challenge [ '<STR_LIT>' ] <EOL> ] ) <EOL> self . key = self . pwhashmod . new ( self . key ) . hexdigest ( ) . lower ( ) <EOL> def request ( self , method , request_uri , headers , content ) : <EOL> """<STR_LIT>""" <EOL> keys = _get_end2end_headers ( headers ) <EOL> keylist = "<STR_LIT>" . join ( [ "<STR_LIT>" % k for k in keys ] ) <EOL> headers_val = "<STR_LIT>" . join ( [ headers [ k ] for k in keys ] ) <EOL> created = time . strftime ( '<STR_LIT>' , time . gmtime ( ) ) <EOL> cnonce = _cnonce ( ) <EOL> request_digest = "<STR_LIT>" % ( method , request_uri , cnonce , self . challenge [ '<STR_LIT>' ] , headers_val ) <EOL> request_digest = hmac . new ( self . key , request_digest , self . hashmod ) . hexdigest ( ) . lower ( ) <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' % ( <EOL> self . credentials [ <NUM_LIT:0> ] , <EOL> self . challenge [ '<STR_LIT>' ] , <EOL> self . challenge [ '<STR_LIT>' ] , <EOL> cnonce , <EOL> request_uri , <EOL> created , <EOL> request_digest , <EOL> keylist , <EOL> ) <EOL> def response ( self , response , content ) : <EOL> challenge = _parse_www_authenticate ( response , '<STR_LIT>' ) . get ( '<STR_LIT>' , { } ) <EOL> if challenge . get ( '<STR_LIT>' ) in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return True <EOL> return False <EOL> class WsseAuthentication ( Authentication ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , credentials , host , request_uri , headers , response , content , http ) : <EOL> Authentication . __init__ ( self , credentials , host , request_uri , headers , response , content , http ) <EOL> def request ( self , method , request_uri , headers , content ) : <EOL> """<STR_LIT>""" <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> iso_now = time . strftime ( "<STR_LIT>" , time . gmtime ( ) ) <EOL> cnonce = _cnonce ( ) <EOL> password_digest = _wsse_username_token ( cnonce , iso_now , self . credentials [ <NUM_LIT:1> ] ) <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' % ( <EOL> self . credentials [ <NUM_LIT:0> ] , <EOL> password_digest , <EOL> cnonce , <EOL> iso_now ) <EOL> class GoogleLoginAuthentication ( Authentication ) : <EOL> def __init__ ( self , credentials , host , request_uri , headers , response , content , http ) : <EOL> from urllib import urlencode <EOL> Authentication . __init__ ( self , credentials , host , request_uri , headers , response , content , http ) <EOL> challenge = _parse_www_authenticate ( response , '<STR_LIT>' ) <EOL> service = challenge [ '<STR_LIT>' ] . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if service == '<STR_LIT>' and request_uri . find ( "<STR_LIT>" ) > <NUM_LIT:0> : <EOL> service = "<STR_LIT>" <EOL> auth = dict ( Email = credentials [ <NUM_LIT:0> ] , Passwd = credentials [ <NUM_LIT:1> ] , service = service , source = headers [ '<STR_LIT>' ] ) <EOL> resp , content = self . http . request ( "<STR_LIT>" , method = "<STR_LIT:POST>" , body = urlencode ( auth ) , headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } ) <EOL> lines = content . split ( '<STR_LIT:\n>' ) <EOL> d = dict ( [ tuple ( line . split ( "<STR_LIT:=>" , <NUM_LIT:1> ) ) for line in lines if line ] ) <EOL> if resp . status == <NUM_LIT> : <EOL> self . Auth = "<STR_LIT>" <EOL> else : <EOL> self . Auth = d [ '<STR_LIT>' ] <EOL> def request ( self , method , request_uri , headers , content ) : <EOL> """<STR_LIT>""" <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' + self . Auth <EOL> AUTH_SCHEME_CLASSES = { <EOL> "<STR_LIT>" : BasicAuthentication , <EOL> "<STR_LIT>" : WsseAuthentication , <EOL> "<STR_LIT>" : DigestAuthentication , <EOL> "<STR_LIT>" : HmacDigestAuthentication , <EOL> "<STR_LIT>" : GoogleLoginAuthentication <EOL> } <EOL> AUTH_SCHEME_ORDER = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> class FileCache ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cache , safe = safename ) : <EOL> self . cache = cache <EOL> self . safe = safe <EOL> if not os . path . exists ( cache ) : <EOL> os . makedirs ( self . cache ) <EOL> def get ( self , key ) : <EOL> retval = None <EOL> cacheFullPath = os . path . join ( self . cache , self . safe ( key ) ) <EOL> try : <EOL> f = file ( cacheFullPath , "<STR_LIT:rb>" ) <EOL> retval = f . read ( ) <EOL> f . close ( ) <EOL> except IOError : <EOL> pass <EOL> return retval <EOL> def set ( self , key , value ) : <EOL> cacheFullPath = os . path . join ( self . cache , self . safe ( key ) ) <EOL> f = file ( cacheFullPath , "<STR_LIT:wb>" ) <EOL> f . write ( value ) <EOL> f . close ( ) <EOL> def delete ( self , key ) : <EOL> cacheFullPath = os . path . join ( self . cache , self . safe ( key ) ) <EOL> if os . path . exists ( cacheFullPath ) : <EOL> os . remove ( cacheFullPath ) <EOL> class Credentials ( object ) : <EOL> def __init__ ( self ) : <EOL> self . credentials = [ ] <EOL> def add ( self , name , password , domain = "<STR_LIT>" ) : <EOL> self . credentials . append ( ( domain . lower ( ) , name , password ) ) <EOL> def clear ( self ) : <EOL> self . credentials = [ ] <EOL> def iter ( self , domain ) : <EOL> for ( cdomain , name , password ) in self . credentials : <EOL> if cdomain == "<STR_LIT>" or domain == cdomain : <EOL> yield ( name , password ) <EOL> class KeyCerts ( Credentials ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class ProxyInfo ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , proxy_type , proxy_host , proxy_port , proxy_rdns = None , proxy_user = None , proxy_pass = None ) : <EOL> """<STR_LIT>""" <EOL> self . proxy_type , self . proxy_host , self . proxy_port , self . proxy_rdns , self . proxy_user , self . proxy_pass = proxy_type , proxy_host , proxy_port , proxy_rdns , proxy_user , proxy_pass <EOL> def astuple ( self ) : <EOL> return ( self . proxy_type , self . proxy_host , self . proxy_port , self . proxy_rdns , <EOL> self . proxy_user , self . proxy_pass ) <EOL> def isgood ( self ) : <EOL> return ( self . proxy_host != None ) and ( self . proxy_port != None ) <EOL> class HTTPConnectionWithTimeout ( httplib . HTTPConnection ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , port = None , strict = None , timeout = None , proxy_info = None ) : <EOL> httplib . HTTPConnection . __init__ ( self , host , port , strict ) <EOL> self . timeout = timeout <EOL> self . proxy_info = proxy_info <EOL> def connect ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . proxy_info and socks is None : <EOL> raise ProxiesUnavailableError ( <EOL> '<STR_LIT>' ) <EOL> msg = "<STR_LIT>" <EOL> for res in socket . getaddrinfo ( self . host , self . port , <NUM_LIT:0> , <EOL> socket . SOCK_STREAM ) : <EOL> af , socktype , proto , canonname , sa = res <EOL> try : <EOL> if self . proxy_info and self . proxy_info . isgood ( ) : <EOL> self . sock = socks . socksocket ( af , socktype , proto ) <EOL> self . sock . setproxy ( * self . proxy_info . astuple ( ) ) <EOL> else : <EOL> self . sock = socket . socket ( af , socktype , proto ) <EOL> self . sock . setsockopt ( socket . IPPROTO_TCP , socket . TCP_NODELAY , <NUM_LIT:1> ) <EOL> if has_timeout ( self . timeout ) : <EOL> self . sock . settimeout ( self . timeout ) <EOL> if self . debuglevel > <NUM_LIT:0> : <EOL> print "<STR_LIT>" % ( self . host , self . port ) <EOL> self . sock . connect ( sa ) <EOL> except socket . error , msg : <EOL> if self . debuglevel > <NUM_LIT:0> : <EOL> print '<STR_LIT>' , ( self . host , self . port ) <EOL> if self . sock : <EOL> self . sock . close ( ) <EOL> self . sock = None <EOL> continue <EOL> break <EOL> if not self . sock : <EOL> raise socket . error , msg <EOL> class HTTPSConnectionWithTimeout ( httplib . HTTPSConnection ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , port = None , key_file = None , cert_file = None , <EOL> strict = None , timeout = None , proxy_info = None , <EOL> ca_certs = None , disable_ssl_certificate_validation = False ) : <EOL> httplib . HTTPSConnection . __init__ ( self , host , port = port , key_file = key_file , <EOL> cert_file = cert_file , strict = strict ) <EOL> self . timeout = timeout <EOL> self . proxy_info = proxy_info <EOL> if ca_certs is None : <EOL> ca_certs = CA_CERTS <EOL> self . ca_certs = ca_certs <EOL> self . disable_ssl_certificate_validation = disable_ssl_certificate_validation <EOL> def _GetValidHostsForCert ( self , cert ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in cert : <EOL> return [ x [ <NUM_LIT:1> ] for x in cert [ '<STR_LIT>' ] <EOL> if x [ <NUM_LIT:0> ] . lower ( ) == '<STR_LIT>' ] <EOL> else : <EOL> return [ x [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] for x in cert [ '<STR_LIT>' ] <EOL> if x [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . lower ( ) == '<STR_LIT>' ] <EOL> def _ValidateCertificateHostname ( self , cert , hostname ) : <EOL> """<STR_LIT>""" <EOL> hosts = self . _GetValidHostsForCert ( cert ) <EOL> for host in hosts : <EOL> host_re = host . replace ( '<STR_LIT:.>' , '<STR_LIT>' ) . replace ( '<STR_LIT:*>' , '<STR_LIT>' ) <EOL> if re . search ( '<STR_LIT>' % ( host_re , ) , hostname , re . I ) : <EOL> return True <EOL> return False <EOL> def connect ( self ) : <EOL> "<STR_LIT>" <EOL> msg = "<STR_LIT>" <EOL> for family , socktype , proto , canonname , sockaddr in socket . getaddrinfo ( <EOL> self . host , self . port , <NUM_LIT:0> , socket . SOCK_STREAM ) : <EOL> try : <EOL> if self . proxy_info and self . proxy_info . isgood ( ) : <EOL> sock = socks . socksocket ( family , socktype , proto ) <EOL> sock . setproxy ( * self . proxy_info . astuple ( ) ) <EOL> else : <EOL> sock = socket . socket ( family , socktype , proto ) <EOL> sock . setsockopt ( socket . IPPROTO_TCP , socket . TCP_NODELAY , <NUM_LIT:1> ) <EOL> if has_timeout ( self . timeout ) : <EOL> sock . settimeout ( self . timeout ) <EOL> sock . connect ( ( self . host , self . port ) ) <EOL> self . sock = _ssl_wrap_socket ( <EOL> sock , self . key_file , self . cert_file , <EOL> self . disable_ssl_certificate_validation , self . ca_certs ) <EOL> if self . debuglevel > <NUM_LIT:0> : <EOL> print "<STR_LIT>" % ( self . host , self . port ) <EOL> if not self . disable_ssl_certificate_validation : <EOL> cert = self . sock . getpeercert ( ) <EOL> hostname = self . host . split ( '<STR_LIT::>' , <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> if not self . _ValidateCertificateHostname ( cert , hostname ) : <EOL> raise CertificateHostnameMismatch ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( hostname , cert ) , hostname , cert ) <EOL> except ssl_SSLError , e : <EOL> if sock : <EOL> sock . close ( ) <EOL> if self . sock : <EOL> self . sock . close ( ) <EOL> self . sock = None <EOL> if e . errno == ssl . SSL_ERROR_SSL : <EOL> raise SSLHandshakeError ( e ) <EOL> else : <EOL> raise <EOL> except ( socket . timeout , socket . gaierror ) : <EOL> raise <EOL> except socket . error , msg : <EOL> if self . debuglevel > <NUM_LIT:0> : <EOL> print '<STR_LIT>' , ( self . host , self . port ) <EOL> if self . sock : <EOL> self . sock . close ( ) <EOL> self . sock = None <EOL> continue <EOL> break <EOL> if not self . sock : <EOL> raise socket . error , msg <EOL> SCHEME_TO_CONNECTION = { <EOL> '<STR_LIT:http>' : HTTPConnectionWithTimeout , <EOL> '<STR_LIT>' : HTTPSConnectionWithTimeout <EOL> } <EOL> try : <EOL> from google . appengine . api import apiproxy_stub_map <EOL> if apiproxy_stub_map . apiproxy . GetStub ( '<STR_LIT>' ) is None : <EOL> raise ImportError <EOL> from google . appengine . api . urlfetch import fetch <EOL> from google . appengine . api . urlfetch import InvalidURLError <EOL> from google . appengine . api . urlfetch import DownloadError <EOL> from google . appengine . api . urlfetch import ResponseTooLargeError <EOL> from google . appengine . api . urlfetch import SSLCertificateError <EOL> class ResponseDict ( dict ) : <EOL> """<STR_LIT>""" <EOL> def read ( self ) : <EOL> pass <EOL> class AppEngineHttpConnection ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , port = None , key_file = None , cert_file = None , <EOL> strict = None , timeout = None , proxy_info = None , ca_certs = None , <EOL> disable_certificate_validation = False ) : <EOL> self . host = host <EOL> self . port = port <EOL> self . timeout = timeout <EOL> if key_file or cert_file or proxy_info or ca_certs : <EOL> raise NotSupportedOnThisPlatform ( ) <EOL> self . response = None <EOL> self . scheme = '<STR_LIT:http>' <EOL> self . validate_certificate = not disable_certificate_validation <EOL> self . sock = True <EOL> def request ( self , method , url , body , headers ) : <EOL> netloc = self . host <EOL> if self . port : <EOL> netloc = '<STR_LIT>' % ( self . host , self . port ) <EOL> absolute_uri = '<STR_LIT>' % ( self . scheme , netloc , url ) <EOL> try : <EOL> response = fetch ( absolute_uri , payload = body , method = method , <EOL> headers = headers , allow_truncated = False , follow_redirects = False , <EOL> deadline = self . timeout , <EOL> validate_certificate = self . validate_certificate ) <EOL> self . response = ResponseDict ( response . headers ) <EOL> self . response [ '<STR_LIT:status>' ] = str ( response . status_code ) <EOL> self . response . status = response . status_code <EOL> setattr ( self . response , '<STR_LIT>' , lambda : response . content ) <EOL> except InvalidURLError : <EOL> raise socket . gaierror ( '<STR_LIT>' ) <EOL> except ( DownloadError , ResponseTooLargeError , SSLCertificateError ) : <EOL> raise httplib . HTTPException ( ) <EOL> def getresponse ( self ) : <EOL> if self . response : <EOL> return self . response <EOL> else : <EOL> raise httplib . HTTPException ( ) <EOL> def set_debuglevel ( self , level ) : <EOL> pass <EOL> def connect ( self ) : <EOL> pass <EOL> def close ( self ) : <EOL> pass <EOL> class AppEngineHttpsConnection ( AppEngineHttpConnection ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , port = None , key_file = None , cert_file = None , <EOL> strict = None , timeout = None , proxy_info = None ) : <EOL> AppEngineHttpConnection . __init__ ( self , host , port , key_file , cert_file , <EOL> strict , timeout , proxy_info ) <EOL> self . scheme = '<STR_LIT>' <EOL> SCHEME_TO_CONNECTION = { <EOL> '<STR_LIT:http>' : AppEngineHttpConnection , <EOL> '<STR_LIT>' : AppEngineHttpsConnection <EOL> } <EOL> except ImportError : <EOL> pass <EOL> class Http ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cache = None , timeout = None , proxy_info = None , <EOL> ca_certs = None , disable_ssl_certificate_validation = False ) : <EOL> """<STR_LIT>""" <EOL> self . proxy_info = proxy_info <EOL> self . ca_certs = ca_certs <EOL> self . disable_ssl_certificate_validation = disable_ssl_certificate_validation <EOL> self . connections = { } <EOL> if cache and isinstance ( cache , basestring ) : <EOL> self . cache = FileCache ( cache ) <EOL> else : <EOL> self . cache = cache <EOL> self . credentials = Credentials ( ) <EOL> self . certificates = KeyCerts ( ) <EOL> self . authorizations = [ ] <EOL> self . follow_redirects = True <EOL> self . optimistic_concurrency_methods = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . follow_all_redirects = False <EOL> self . ignore_etag = False <EOL> self . force_exception_to_status_code = False <EOL> self . timeout = timeout <EOL> def _auth_from_challenge ( self , host , request_uri , headers , response , content ) : <EOL> """<STR_LIT>""" <EOL> challenges = _parse_www_authenticate ( response , '<STR_LIT>' ) <EOL> for cred in self . credentials . iter ( host ) : <EOL> for scheme in AUTH_SCHEME_ORDER : <EOL> if challenges . has_key ( scheme ) : <EOL> yield AUTH_SCHEME_CLASSES [ scheme ] ( cred , host , request_uri , headers , response , content , self ) <EOL> def add_credentials ( self , name , password , domain = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> self . credentials . add ( name , password , domain ) <EOL> def add_certificate ( self , key , cert , domain ) : <EOL> """<STR_LIT>""" <EOL> self . certificates . add ( key , cert , domain ) <EOL> def clear_credentials ( self ) : <EOL> """<STR_LIT>""" <EOL> self . credentials . clear ( ) <EOL> self . authorizations = [ ] <EOL> def _conn_request ( self , conn , request_uri , method , body , headers ) : <EOL> for i in range ( <NUM_LIT:2> ) : <EOL> try : <EOL> if conn . sock is None : <EOL> conn . connect ( ) <EOL> conn . request ( method , request_uri , body , headers ) <EOL> except socket . timeout : <EOL> raise <EOL> except socket . gaierror : <EOL> conn . close ( ) <EOL> raise ServerNotFoundError ( "<STR_LIT>" % conn . host ) <EOL> except ssl_SSLError : <EOL> conn . close ( ) <EOL> raise <EOL> except socket . error , e : <EOL> err = <NUM_LIT:0> <EOL> if hasattr ( e , '<STR_LIT:args>' ) : <EOL> err = getattr ( e , '<STR_LIT:args>' ) [ <NUM_LIT:0> ] <EOL> else : <EOL> err = e . errno <EOL> if err == errno . ECONNREFUSED : <EOL> raise <EOL> except httplib . HTTPException : <EOL> if conn . sock is None : <EOL> if i == <NUM_LIT:0> : <EOL> conn . close ( ) <EOL> conn . connect ( ) <EOL> continue <EOL> else : <EOL> conn . close ( ) <EOL> raise <EOL> if i == <NUM_LIT:0> : <EOL> conn . close ( ) <EOL> conn . connect ( ) <EOL> continue <EOL> try : <EOL> response = conn . getresponse ( ) <EOL> except ( socket . error , httplib . HTTPException ) : <EOL> if i == <NUM_LIT:0> : <EOL> conn . close ( ) <EOL> conn . connect ( ) <EOL> continue <EOL> else : <EOL> raise <EOL> else : <EOL> content = "<STR_LIT>" <EOL> if method == "<STR_LIT>" : <EOL> response . close ( ) <EOL> else : <EOL> content = response . read ( ) <EOL> response = Response ( response ) <EOL> if method != "<STR_LIT>" : <EOL> content = _decompressContent ( response , content ) <EOL> break <EOL> return ( response , content ) <EOL> def _request ( self , conn , host , absolute_uri , request_uri , method , body , headers , redirections , cachekey ) : <EOL> """<STR_LIT>""" <EOL> auths = [ ( auth . depth ( request_uri ) , auth ) for auth in self . authorizations if auth . inscope ( host , request_uri ) ] <EOL> auth = auths and sorted ( auths ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] or None <EOL> if auth : <EOL> auth . request ( method , request_uri , headers , body ) <EOL> ( response , content ) = self . _conn_request ( conn , request_uri , method , body , headers ) <EOL> if auth : <EOL> if auth . response ( response , body ) : <EOL> auth . request ( method , request_uri , headers , body ) <EOL> ( response , content ) = self . _conn_request ( conn , request_uri , method , body , headers ) <EOL> response . _stale_digest = <NUM_LIT:1> <EOL> if response . status == <NUM_LIT> : <EOL> for authorization in self . _auth_from_challenge ( host , request_uri , headers , response , content ) : <EOL> authorization . request ( method , request_uri , headers , body ) <EOL> ( response , content ) = self . _conn_request ( conn , request_uri , method , body , headers , ) <EOL> if response . status != <NUM_LIT> : <EOL> self . authorizations . append ( authorization ) <EOL> authorization . response ( response , body ) <EOL> break <EOL> if ( self . follow_all_redirects or ( method in [ "<STR_LIT:GET>" , "<STR_LIT>" ] ) or response . status == <NUM_LIT> ) : <EOL> if self . follow_redirects and response . status in [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] : <EOL> if redirections : <EOL> if not response . has_key ( '<STR_LIT:location>' ) and response . status != <NUM_LIT> : <EOL> raise RedirectMissingLocation ( _ ( "<STR_LIT>" ) , response , content ) <EOL> if response . has_key ( '<STR_LIT:location>' ) : <EOL> location = response [ '<STR_LIT:location>' ] <EOL> ( scheme , authority , path , query , fragment ) = parse_uri ( location ) <EOL> if authority == None : <EOL> response [ '<STR_LIT:location>' ] = urlparse . urljoin ( absolute_uri , location ) <EOL> if response . status == <NUM_LIT> and method in [ "<STR_LIT:GET>" , "<STR_LIT>" ] : <EOL> response [ '<STR_LIT>' ] = response [ '<STR_LIT:location>' ] <EOL> if not response . has_key ( '<STR_LIT>' ) : <EOL> response [ '<STR_LIT>' ] = absolute_uri <EOL> _updateCache ( headers , response , content , self . cache , cachekey ) <EOL> if headers . has_key ( '<STR_LIT>' ) : <EOL> del headers [ '<STR_LIT>' ] <EOL> if headers . has_key ( '<STR_LIT>' ) : <EOL> del headers [ '<STR_LIT>' ] <EOL> if response . has_key ( '<STR_LIT:location>' ) : <EOL> location = response [ '<STR_LIT:location>' ] <EOL> old_response = copy . deepcopy ( response ) <EOL> if not old_response . has_key ( '<STR_LIT>' ) : <EOL> old_response [ '<STR_LIT>' ] = absolute_uri <EOL> redirect_method = method <EOL> if response . status in [ <NUM_LIT> , <NUM_LIT> ] : <EOL> redirect_method = "<STR_LIT:GET>" <EOL> body = None <EOL> ( response , content ) = self . request ( location , redirect_method , body = body , headers = headers , redirections = redirections - <NUM_LIT:1> ) <EOL> response . previous = old_response <EOL> else : <EOL> raise RedirectLimit ( "<STR_LIT>" , response , content ) <EOL> elif response . status in [ <NUM_LIT:200> , <NUM_LIT> ] and method in [ "<STR_LIT:GET>" , "<STR_LIT>" ] : <EOL> if not response . has_key ( '<STR_LIT>' ) : <EOL> response [ '<STR_LIT>' ] = absolute_uri <EOL> _updateCache ( headers , response , content , self . cache , cachekey ) <EOL> return ( response , content ) <EOL> def _normalize_headers ( self , headers ) : <EOL> return _normalize_headers ( headers ) <EOL> def request ( self , uri , method = "<STR_LIT:GET>" , body = None , headers = None , redirections = DEFAULT_MAX_REDIRECTS , connection_type = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if headers is None : <EOL> headers = { } <EOL> else : <EOL> headers = self . _normalize_headers ( headers ) <EOL> if not headers . has_key ( '<STR_LIT>' ) : <EOL> headers [ '<STR_LIT>' ] = "<STR_LIT>" % __version__ <EOL> uri = iri2uri ( uri ) <EOL> ( scheme , authority , request_uri , defrag_uri ) = urlnorm ( uri ) <EOL> domain_port = authority . split ( "<STR_LIT::>" ) [ <NUM_LIT:0> : <NUM_LIT:2> ] <EOL> if len ( domain_port ) == <NUM_LIT:2> and domain_port [ <NUM_LIT:1> ] == '<STR_LIT>' and scheme == '<STR_LIT:http>' : <EOL> scheme = '<STR_LIT>' <EOL> authority = domain_port [ <NUM_LIT:0> ] <EOL> conn_key = scheme + "<STR_LIT::>" + authority <EOL> if conn_key in self . connections : <EOL> conn = self . connections [ conn_key ] <EOL> else : <EOL> if not connection_type : <EOL> connection_type = SCHEME_TO_CONNECTION [ scheme ] <EOL> certs = list ( self . certificates . iter ( authority ) ) <EOL> if issubclass ( connection_type , HTTPSConnectionWithTimeout ) : <EOL> if certs : <EOL> conn = self . connections [ conn_key ] = connection_type ( <EOL> authority , key_file = certs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , <EOL> cert_file = certs [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , timeout = self . timeout , <EOL> proxy_info = self . proxy_info , <EOL> ca_certs = self . ca_certs , <EOL> disable_ssl_certificate_validation = <EOL> self . disable_ssl_certificate_validation ) <EOL> else : <EOL> conn = self . connections [ conn_key ] = connection_type ( <EOL> authority , timeout = self . timeout , <EOL> proxy_info = self . proxy_info , <EOL> ca_certs = self . ca_certs , <EOL> disable_ssl_certificate_validation = <EOL> self . disable_ssl_certificate_validation ) <EOL> else : <EOL> conn = self . connections [ conn_key ] = connection_type ( <EOL> authority , timeout = self . timeout , <EOL> proxy_info = self . proxy_info ) <EOL> conn . set_debuglevel ( debuglevel ) <EOL> if '<STR_LIT>' not in headers and '<STR_LIT>' not in headers : <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> info = email . Message . Message ( ) <EOL> cached_value = None <EOL> if self . cache : <EOL> cachekey = defrag_uri <EOL> cached_value = self . cache . get ( cachekey ) <EOL> if cached_value : <EOL> try : <EOL> info , content = cached_value . split ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> feedparser = email . FeedParser . FeedParser ( ) <EOL> feedparser . feed ( info ) <EOL> info = feedparser . close ( ) <EOL> feedparser . _parse = None <EOL> except IndexError : <EOL> self . cache . delete ( cachekey ) <EOL> cachekey = None <EOL> cached_value = None <EOL> else : <EOL> cachekey = None <EOL> if method in self . optimistic_concurrency_methods and self . cache and info . has_key ( '<STR_LIT>' ) and not self . ignore_etag and '<STR_LIT>' not in headers : <EOL> headers [ '<STR_LIT>' ] = info [ '<STR_LIT>' ] <EOL> if method not in [ "<STR_LIT:GET>" , "<STR_LIT>" ] and self . cache and cachekey : <EOL> self . cache . delete ( cachekey ) <EOL> if method in [ '<STR_LIT:GET>' , '<STR_LIT>' ] and '<STR_LIT>' in info : <EOL> vary = info [ '<STR_LIT>' ] <EOL> vary_headers = vary . lower ( ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) <EOL> for header in vary_headers : <EOL> key = '<STR_LIT>' % header <EOL> value = info [ key ] <EOL> if headers . get ( header , None ) != value : <EOL> cached_value = None <EOL> break <EOL> if cached_value and method in [ "<STR_LIT:GET>" , "<STR_LIT>" ] and self . cache and '<STR_LIT>' not in headers : <EOL> if info . has_key ( '<STR_LIT>' ) : <EOL> if redirections <= <NUM_LIT:0> : <EOL> raise RedirectLimit ( "<STR_LIT>" , { } , "<STR_LIT>" ) <EOL> ( response , new_content ) = self . request ( info [ '<STR_LIT>' ] , "<STR_LIT:GET>" , headers = headers , redirections = redirections - <NUM_LIT:1> ) <EOL> response . previous = Response ( info ) <EOL> response . previous . fromcache = True <EOL> else : <EOL> entry_disposition = _entry_disposition ( info , headers ) <EOL> if entry_disposition == "<STR_LIT>" : <EOL> if not cached_value : <EOL> info [ '<STR_LIT:status>' ] = '<STR_LIT>' <EOL> content = "<STR_LIT>" <EOL> response = Response ( info ) <EOL> if cached_value : <EOL> response . fromcache = True <EOL> return ( response , content ) <EOL> if entry_disposition == "<STR_LIT>" : <EOL> if info . has_key ( '<STR_LIT>' ) and not self . ignore_etag and not '<STR_LIT>' in headers : <EOL> headers [ '<STR_LIT>' ] = info [ '<STR_LIT>' ] <EOL> if info . has_key ( '<STR_LIT>' ) and not '<STR_LIT>' in headers : <EOL> headers [ '<STR_LIT>' ] = info [ '<STR_LIT>' ] <EOL> elif entry_disposition == "<STR_LIT>" : <EOL> pass <EOL> ( response , new_content ) = self . _request ( conn , authority , uri , request_uri , method , body , headers , redirections , cachekey ) <EOL> if response . status == <NUM_LIT> and method == "<STR_LIT:GET>" : <EOL> for key in _get_end2end_headers ( response ) : <EOL> info [ key ] = response [ key ] <EOL> merged_response = Response ( info ) <EOL> if hasattr ( response , "<STR_LIT>" ) : <EOL> merged_response . _stale_digest = response . _stale_digest <EOL> _updateCache ( headers , merged_response , content , self . cache , cachekey ) <EOL> response = merged_response <EOL> response . status = <NUM_LIT:200> <EOL> response . fromcache = True <EOL> elif response . status == <NUM_LIT:200> : <EOL> content = new_content <EOL> else : <EOL> self . cache . delete ( cachekey ) <EOL> content = new_content <EOL> else : <EOL> cc = _parse_cache_control ( headers ) <EOL> if cc . has_key ( '<STR_LIT>' ) : <EOL> info [ '<STR_LIT:status>' ] = '<STR_LIT>' <EOL> response = Response ( info ) <EOL> content = "<STR_LIT>" <EOL> else : <EOL> ( response , content ) = self . _request ( conn , authority , uri , request_uri , method , body , headers , redirections , cachekey ) <EOL> except Exception , e : <EOL> if self . force_exception_to_status_code : <EOL> if isinstance ( e , HttpLib2ErrorWithResponse ) : <EOL> response = e . response <EOL> content = e . content <EOL> response . status = <NUM_LIT> <EOL> response . reason = str ( e ) <EOL> elif isinstance ( e , socket . timeout ) : <EOL> content = "<STR_LIT>" <EOL> response = Response ( { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:status>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : len ( content ) <EOL> } ) <EOL> response . reason = "<STR_LIT>" <EOL> else : <EOL> content = str ( e ) <EOL> response = Response ( { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:status>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : len ( content ) <EOL> } ) <EOL> response . reason = "<STR_LIT>" <EOL> else : <EOL> raise <EOL> return ( response , content ) <EOL> class Response ( dict ) : <EOL> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> fromcache = False <EOL> """<STR_LIT>""" <EOL> version = <NUM_LIT:11> <EOL> "<STR_LIT>" <EOL> status = <NUM_LIT:200> <EOL> """<STR_LIT>""" <EOL> reason = "<STR_LIT>" <EOL> previous = None <EOL> def __init__ ( self , info ) : <EOL> if isinstance ( info , httplib . HTTPResponse ) : <EOL> for key , value in info . getheaders ( ) : <EOL> self [ key . lower ( ) ] = value <EOL> self . status = info . status <EOL> self [ '<STR_LIT:status>' ] = str ( self . status ) <EOL> self . reason = info . reason <EOL> self . version = info . version <EOL> elif isinstance ( info , email . Message . Message ) : <EOL> for key , value in info . items ( ) : <EOL> self [ key ] = value <EOL> self . status = int ( self [ '<STR_LIT:status>' ] ) <EOL> else : <EOL> for key , value in info . iteritems ( ) : <EOL> self [ key ] = value <EOL> self . status = int ( self . get ( '<STR_LIT:status>' , self . status ) ) <EOL> def __getattr__ ( self , name ) : <EOL> if name == '<STR_LIT>' : <EOL> return self <EOL> else : <EOL> raise AttributeError , name </s>
