home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / atom / http.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  8.3 KB  |  244 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''HttpClients in this module use httplib to make HTTP requests.
  5.  
  6. This module make HTTP requests based on httplib, but there are environments
  7. in which an httplib based approach will not work (if running in Google App
  8. Engine for example). In those cases, higher level classes (like AtomService
  9. and GDataService) can swap out the HttpClient to transparently use a 
  10. different mechanism for making HTTP requests.
  11.  
  12.   HttpClient: Contains a request method which performs an HTTP call to the 
  13.       server.
  14.       
  15.   ProxiedHttpClient: Contains a request method which connects to a proxy using
  16.       settings stored in operating system environment variables then 
  17.       performs an HTTP call to the endpoint server.
  18. '''
  19. __author__ = 'api.jscudder (Jeff Scudder)'
  20. import types
  21. import os
  22. import httplib
  23. import atom.url as atom
  24. import atom.http_interface as atom
  25. import socket
  26. import base64
  27.  
  28. class ProxyError(atom.http_interface.Error):
  29.     pass
  30.  
  31. DEFAULT_CONTENT_TYPE = 'application/atom+xml'
  32.  
  33. class HttpClient(atom.http_interface.GenericHttpClient):
  34.     
  35.     def __init__(self, headers = None):
  36.         self.debug = False
  37.         if not headers:
  38.             pass
  39.         self.headers = { }
  40.  
  41.     
  42.     def request(self, operation, url, data = None, headers = None):
  43.         """Performs an HTTP call to the server, supports GET, POST, PUT, and 
  44.     DELETE.
  45.  
  46.     Usage example, perform and HTTP GET on http://www.google.com/:
  47.       import atom.http
  48.       client = atom.http.HttpClient()
  49.       http_response = client.request('GET', 'http://www.google.com/')
  50.  
  51.     Args:
  52.       operation: str The HTTP operation to be performed. This is usually one
  53.           of 'GET', 'POST', 'PUT', or 'DELETE'
  54.       data: filestream, list of parts, or other object which can be converted
  55.           to a string. Should be set to None when performing a GET or DELETE.
  56.           If data is a file-like object which can be read, this method will 
  57.           read a chunk of 100K bytes at a time and send them. 
  58.           If the data is a list of parts to be sent, each part will be 
  59.           evaluated and sent.
  60.       url: The full URL to which the request should be sent. Can be a string
  61.           or atom.url.Url.
  62.       headers: dict of strings. HTTP headers which should be sent
  63.           in the request. 
  64.     """
  65.         if not isinstance(url, atom.url.Url):
  66.             if isinstance(url, types.StringTypes):
  67.                 url = atom.url.parse_url(url)
  68.             else:
  69.                 raise atom.http_interface.UnparsableUrlObject('Unable to parse url parameter because it was not a string or atom.url.Url')
  70.         isinstance(url, types.StringTypes)
  71.         all_headers = self.headers.copy()
  72.         if headers:
  73.             all_headers.update(headers)
  74.         
  75.         connection = self._prepare_connection(url, all_headers)
  76.         if self.debug:
  77.             connection.debuglevel = 1
  78.         
  79.         connection.putrequest(operation, self._get_access_url(url), skip_host = True)
  80.         connection.putheader('Host', url.host)
  81.         if url.protocol == 'https':
  82.             if not url.port:
  83.                 pass
  84.             if int(443) == 443 and hasattr(connection, '_buffer') and isinstance(connection._buffer, list):
  85.                 header_line = 'Host: %s:443' % url.host
  86.                 replacement_header_line = 'Host: %s' % url.host
  87.                 
  88.                 try:
  89.                     connection._buffer[connection._buffer.index(header_line)] = replacement_header_line
  90.                 except ValueError:
  91.                     pass
  92.                 except:
  93.                     None<EXCEPTION MATCH>ValueError
  94.                 
  95.  
  96.         None<EXCEPTION MATCH>ValueError
  97.         if data and 'Content-Length' not in all_headers:
  98.             if isinstance(data, types.StringTypes):
  99.                 all_headers['Content-Length'] = len(data)
  100.             else:
  101.                 raise atom.http_interface.ContentLengthRequired('Unable to calculate the length of the data parameter. Specify a value for Content-Length')
  102.         isinstance(data, types.StringTypes)
  103.         if 'Content-Type' not in all_headers:
  104.             all_headers['Content-Type'] = DEFAULT_CONTENT_TYPE
  105.         
  106.         for header_name in all_headers:
  107.             connection.putheader(header_name, all_headers[header_name])
  108.         
  109.         connection.endheaders()
  110.         if data:
  111.             if isinstance(data, list):
  112.                 for data_part in data:
  113.                     _send_data_part(data_part, connection)
  114.                 
  115.             else:
  116.                 _send_data_part(data, connection)
  117.         
  118.         return connection.getresponse()
  119.  
  120.     
  121.     def _prepare_connection(self, url, headers):
  122.         if not isinstance(url, atom.url.Url):
  123.             if isinstance(url, types.StringTypes):
  124.                 url = atom.url.parse_url(url)
  125.             else:
  126.                 raise atom.http_interface.UnparsableUrlObject('Unable to parse url parameter because it was not a string or atom.url.Url')
  127.         isinstance(url, types.StringTypes)
  128.         if url.protocol == 'https':
  129.             if not url.port:
  130.                 return httplib.HTTPSConnection(url.host)
  131.             return httplib.HTTPSConnection(url.host, int(url.port))
  132.         if not url.port:
  133.             return httplib.HTTPConnection(url.host)
  134.         return httplib.HTTPConnection(url.host, int(url.port))
  135.  
  136.     
  137.     def _get_access_url(self, url):
  138.         return url.to_string()
  139.  
  140.  
  141.  
  142. class ProxiedHttpClient(HttpClient):
  143.     """Performs an HTTP request through a proxy.
  144.   
  145.   The proxy settings are obtained from enviroment variables. The URL of the 
  146.   proxy server is assumed to be stored in the environment variables 
  147.   'https_proxy' and 'http_proxy' respectively. If the proxy server requires
  148.   a Basic Auth authorization header, the username and password are expected to 
  149.   be in the 'proxy-username' or 'proxy_username' variable and the 
  150.   'proxy-password' or 'proxy_password' variable.
  151.   
  152.   After connecting to the proxy server, the request is completed as in 
  153.   HttpClient.request.
  154.   """
  155.     
  156.     def _prepare_connection(self, url, headers):
  157.         proxy_auth = _get_proxy_auth()
  158.         if url.protocol == 'https':
  159.             proxy = os.environ.get('https_proxy')
  160.             if proxy:
  161.                 if proxy_auth:
  162.                     proxy_auth = 'Proxy-authorization: %s' % proxy_auth
  163.                 
  164.                 port = url.port
  165.                 if not port:
  166.                     port = '443'
  167.                 
  168.                 proxy_connect = 'CONNECT %s:%s HTTP/1.0\r\n' % (url.host, port)
  169.                 if headers and 'User-Agent' in headers:
  170.                     user_agent = 'User-Agent: %s\r\n' % headers['User-Agent']
  171.                 else:
  172.                     user_agent = ''
  173.                 proxy_pieces = '%s%s%s\r\n' % (proxy_connect, proxy_auth, user_agent)
  174.                 proxy_url = atom.url.parse_url(proxy)
  175.                 if not proxy_url.port:
  176.                     proxy_url.port = '80'
  177.                 
  178.                 p_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  179.                 p_sock.connect((proxy_url.host, int(proxy_url.port)))
  180.                 p_sock.sendall(proxy_pieces)
  181.                 response = ''
  182.                 while response.find('\r\n\r\n') == -1:
  183.                     response += p_sock.recv(8192)
  184.                 p_status = response.split()[1]
  185.                 if p_status != str(200):
  186.                     raise ProxyError('Error status=%s' % str(p_status))
  187.                 p_status != str(200)
  188.                 ssl = socket.ssl(p_sock, None, None)
  189.                 fake_sock = httplib.FakeSocket(p_sock, ssl)
  190.                 connection = httplib.HTTPConnection(proxy_url.host)
  191.                 connection.sock = fake_sock
  192.                 return connection
  193.             return HttpClient._prepare_connection(self, url, headers)
  194.         url.protocol == 'https'
  195.         proxy = os.environ.get('http_proxy')
  196.         if proxy:
  197.             proxy_url = atom.url.parse_url(proxy)
  198.             if not proxy_url.port:
  199.                 proxy_url.port = '80'
  200.             
  201.             if proxy_auth:
  202.                 headers['Proxy-Authorization'] = proxy_auth.strip()
  203.             
  204.             return httplib.HTTPConnection(proxy_url.host, int(proxy_url.port))
  205.         return HttpClient._prepare_connection(self, url, headers)
  206.  
  207.     
  208.     def _get_access_url(self, url):
  209.         return url.to_string()
  210.  
  211.  
  212.  
  213. def _get_proxy_auth():
  214.     proxy_username = os.environ.get('proxy-username')
  215.     if not proxy_username:
  216.         proxy_username = os.environ.get('proxy_username')
  217.     
  218.     proxy_password = os.environ.get('proxy-password')
  219.     if not proxy_password:
  220.         proxy_password = os.environ.get('proxy_password')
  221.     
  222.     if proxy_username:
  223.         user_auth = base64.encodestring('%s:%s' % (proxy_username, proxy_password))
  224.         return 'Basic %s\r\n' % user_auth.strip()
  225.     return ''
  226.  
  227.  
  228. def _send_data_part(data, connection):
  229.     if isinstance(data, types.StringTypes):
  230.         connection.send(data)
  231.         return None
  232.     if hasattr(data, 'read'):
  233.         while None:
  234.             binarydata = data.read(100000)
  235.             if binarydata == '':
  236.                 break
  237.             
  238.             continue
  239.             return None
  240.             connection.send(str(data))
  241.             return None
  242.             return None
  243.  
  244.