home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / common / asynchttp / handlers.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  6.5 KB  |  153 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import logging
  5. import urlparse
  6. import urllib
  7. import urllib2
  8. log = logging.getLogger('asynchttp.handlers')
  9. __all__ = [
  10.     'AsyncHTTPBasicAuthHandler',
  11.     'AsyncHTTPDigestAuthHandler',
  12.     'AsyncHTTPRedirectHandler',
  13.     'AsyncProxyBasicAuthHandler',
  14.     'AsyncProxyDigestAuthHandler',
  15.     'AsyncProxyHandler']
  16. redirect_codes = frozenset((301, 302, 303, 307))
  17.  
  18. class AsyncHTTPRedirectHandler(urllib2.HTTPRedirectHandler):
  19.     max_repeats = 4
  20.     max_redirections = 10
  21.     
  22.     def http_response(self, request, response):
  23.         if response.code not in redirect_codes or not (request.follow_redirects):
  24.             return None
  25.         headers = response.headers
  26.         newurl = headers.get('location', headers.get('uri', None))
  27.         if newurl is None:
  28.             return None
  29.         newurl = urlparse.urljoin(request.get_full_url(), newurl).replace(' ', '%20')
  30.         new_request = request.copy(url = newurl, unverifiable = True)
  31.         del new_request.headers['Host']
  32.         visited[newurl] = visited.get(newurl, 0) + 1
  33.         log.info('redirect: %r -> %r', request.get_full_url(), new_request.get_full_url())
  34.         return ('request', new_request)
  35.  
  36.  
  37.  
  38. class AsyncProxyHandler(urllib2.ProxyHandler):
  39.     
  40.     def http_request(self, request):
  41.         
  42.         try:
  43.             proxy = self.proxies['http']
  44.         except KeyError:
  45.             return None
  46.  
  47.         (proxy_type, user, password, hostport) = urllib2._parse_proxy(proxy)
  48.         if proxy_type != 'http':
  49.             log.warning('Got unexpected proxy type %r in asynchttp. not modifying request', proxy_type)
  50.             return None
  51.         if proxy_type is None:
  52.             proxy_type = 'http'
  53.         
  54.         if user and password:
  55.             user_pass = '%s:%s' % (urllib2.unquote(user), urllib2.unquote(password))
  56.             creds = user_pass.encode('base64').strip()
  57.             request.headers['Proxy-Authorization'] = 'Basic ' + creds
  58.         
  59.         hostport = urllib.unquote(hostport)
  60.         request.set_proxy(hostport, proxy_type)
  61.  
  62.  
  63.  
  64. class AsyncAbstractBasicAuthHandler(urllib2.AbstractBasicAuthHandler):
  65.     
  66.     def http_error_auth_reqed(self, req, resp, host, authheader):
  67.         authheader = resp.headers.get(authheader, None)
  68.         if authheader:
  69.             mo = urllib2.AbstractBasicAuthHandler.rx.search(authheader)
  70.             if mo:
  71.                 (scheme, a_, realm) = mo.groups()
  72.                 if scheme.lower() == 'basic':
  73.                     return self.retry_http_basic_auth(req, resp, host, realm)
  74.             
  75.         
  76.  
  77.     
  78.     def retry_http_basic_auth(self, req, resp, host, realm):
  79.         (user, password) = self.passwd.find_user_password(realm, host)
  80.         if password is not None:
  81.             raw = '%s:%s' % (user, password)
  82.             auth = 'Basic %s' % raw.encode('base64').strip()
  83.             if req.headers.get(self.auth_header, None) == auth:
  84.                 return None
  85.             req.add_header(self.auth_header, auth)
  86.             return ('request', req)
  87.  
  88.  
  89.  
  90. class AsyncHTTPBasicAuthHandler(AsyncAbstractBasicAuthHandler, urllib2.BaseHandler):
  91.     auth_header = 'Authorization'
  92.     
  93.     def http_response_401(self, req, resp):
  94.         url = req.get_full_url()
  95.         return self.http_error_auth_reqed(req, resp, url, 'www-authenticate')
  96.  
  97.  
  98.  
  99. class AsyncProxyBasicAuthHandler(AsyncAbstractBasicAuthHandler, urllib2.BaseHandler):
  100.     auth_header = 'Proxy-authorization'
  101.     
  102.     def http_response_407(self, req, resp):
  103.         authority = req.get_host()
  104.         return self.http_error_auth_reqed(req, resp, authority, 'proxy-authenticate')
  105.  
  106.  
  107.  
  108. class AsyncAbstractDigestAuthHandler(urllib2.AbstractDigestAuthHandler):
  109.     
  110.     def http_error_auth_reqed(self, req, resp, host, authheader):
  111.         authreq = req.headers.get(authheader, None)
  112.         if self.retried > 5:
  113.             return None
  114.         self.retried += 1
  115.  
  116.     
  117.     def retry_http_digest_auth(self, req, resp, host, auth):
  118.         (_token, challenge) = auth.split(' ', 1)
  119.         chal = urllib2.parse_keqv_list(urllib2.parse_http_list(challenge))
  120.         auth = self.get_authorization(req, chal)
  121.         if auth:
  122.             auth_val = 'Digest %s' % auth
  123.             if req.headers.get(self.auth_header, None) == auth_val:
  124.                 return None
  125.             req.add_unredirected_header(self.auth_header, auth_val)
  126.             return ('request', req)
  127.  
  128.  
  129.  
  130. class AsyncHTTPDigestAuthHandler(AsyncAbstractDigestAuthHandler, urllib2.BaseHandler):
  131.     auth_header = 'Authorization'
  132.     handler_order = 490
  133.     
  134.     def http_response_401(self, req, resp):
  135.         host = urlparse.urlparse(req.get_full_url())[1]
  136.         val = self.http_error_auth_reqed(req, resp, host, 'www-authenticate')
  137.         self.reset_retry_count()
  138.         return val
  139.  
  140.  
  141.  
  142. class AsyncProxyDigestAuthHandler(AsyncAbstractDigestAuthHandler, urllib2.BaseHandler):
  143.     auth_header = 'Proxy-Authorization'
  144.     handler_order = 490
  145.     
  146.     def http_response_407(self, req, resp):
  147.         host = req.get_host()
  148.         val = self.http_error_auth_reqed(req, resp, host, 'proxy-authenticate')
  149.         self.reset_retry_count()
  150.         return val
  151.  
  152.  
  153.