Package restkit :: Package filters :: Module simpleproxy
[hide private]
[frames] | no frames]

Source Code for Module restkit.filters.simpleproxy

  1  # -*- coding: utf-8 - 
  2  # 
  3  # This file is part of restkit released under the MIT license.  
  4  # See the NOTICE for more information. 
  5   
  6  """ 
  7  filters - Http filters 
  8   
  9  Http filters are object used before sending the request to the server 
 10  and after. The `HttpClient` instance is passed as argument. 
 11   
 12  An object with a method `on_request` is called just before the request.  
 13  An object with a method `on_response` is called after fetching response headers. 
 14   
 15  ex:: 
 16   
 17      class MyFilter(object): 
 18           
 19          def on_request(self, http_client): 
 20              "do something with/to http_client instance" 
 21   
 22          def on_response(self, http_client): 
 23              "do something on http_client and get response infos" 
 24               
 25               
 26  """ 
 27   
 28  import base64 
 29  import os 
 30  import urlparse 
 31   
 32  from restkit import http 
 33  from restkit import util 
 34  from restkit.util import sock 
 35   
 36  from restkit import __version__ 
 37           
38 -class ProxyError(Exception):
39 pass
40
41 -class SimpleProxy(object):
42 """ Simple proxy filter. 43 This filter find proxy from environment and if it exists it 44 connect to the proxy and modify connection headers. 45 """ 46
47 - def on_connect(self, req):
48 proxy_auth = _get_proxy_auth() 49 if req.uri.scheme == "https": 50 proxy = os.environ.get('https_proxy') 51 if proxy: 52 if proxy_auth: 53 proxy_auth = 'Proxy-authorization: %s' % proxy_auth 54 proxy_connect = 'CONNECT %s HTTP/1.0\r\n' % (req.uri.netloc) 55 user_agent = "User-Agent: restkit/%s\r\n" % __version__ 56 proxy_pieces = '%s%s%s\r\n' % (proxy_connect, proxy_auth, 57 user_agent) 58 proxy_uri = urlparse.urlparse(proxy) 59 proxy_host, proxy_port = util.parse_netloc(proxy_uri) 60 61 if req.pool is not None: 62 s = req.pool.get((proxy_host, proxy_port)) 63 if s: 64 self._sock = s 65 req.host = proxy_host 66 req.port = proxy_port 67 return 68 69 # Connect to the proxy server, 70 # very simple recv and error checking 71 72 p_sock = sock.connect((proxy_host, int(proxy_port))) 73 sock.send(p_sock, proxy_pieces) 74 75 # wait header 76 parser = http.ResponseParser(p_sock) 77 resp = parser.next() 78 79 if resp.status_int != 200: 80 raise ProxyError('Error status=%s' % resp.status) 81 82 sock._ssl_wrap_socket(p_sock, None, None) 83 84 # update socket 85 req._sock = p_sock 86 req.host = proxy_host 87 req.port = proxy_port 88 else: 89 proxy = os.environ.get('http_proxy') 90 if proxy: 91 proxy_uri = urlparse.urlparse(proxy) 92 proxy_host, proxy_port = self._host_port(proxy_uri) 93 if proxy_auth: 94 req.headers.append(('Proxy-Authorization', 95 proxy_auth.strip())) 96 97 req.host = proxy_host 98 req.port = proxy_port
99 100 101
102 -def _get_proxy_auth():
103 proxy_username = os.environ.get('proxy-username') 104 if not proxy_username: 105 proxy_username = os.environ.get('proxy_username') 106 proxy_password = os.environ.get('proxy-password') 107 if not proxy_password: 108 proxy_password = os.environ.get('proxy_password') 109 if proxy_username: 110 user_auth = base64.encodestring('%s:%s' % (proxy_username, 111 proxy_password)) 112 return 'Basic %s\r\n' % (user_auth.strip()) 113 else: 114 return ''
115