Example #1
0
 def make_connection(self):
     """ initate a connection if needed or reuse a socket"""
     addr = (self.host, self.port)
     s = None
     # if we defined a pool use it
     if self.connections is not None:
         s = self.connections.get(addr)
         
     if not s:
         # pool is empty or we don't use a pool
         if self.uri.scheme == "https":
             s = sock.connect(addr, self.timeout, True, 
                             self.key_file, self.cert_file)
         else:
             s = sock.connect(addr, self.timeout)
     return s
Example #2
0
 def on_request(self, req):
     proxy_auth = _get_proxy_auth()
     if req.uri.scheme == "https":
         proxy = os.environ.get('https_proxy')
         if proxy:
             if proxy_auth:
                 proxy_auth = 'Proxy-authorization: %s' % proxy_auth
             proxy_connect = 'CONNECT %s HTTP/1.0\r\n' % (req.uri.netloc)
             user_agent = "User-Agent: restkit/%s\r\n" % __version__
             proxy_pieces = '%s%s%s\r\n' % (proxy_connect, proxy_auth, 
                                     user_agent)
             proxy_uri = urlparse.urlparse(proxy)
             proxy_host, proxy_port = self._host_port(proxy_uri)
             # Connect to the proxy server, 
             # very simple recv and error checking
             
             p_sock = sock.connect((proxy_host, int(proxy_port))   )          
             sock.send(p_sock, proxy_pieces)
         
             # wait header
             p = Parser.parse_response()
             headers = []
             buf = ""
             buf = sock.recv(p_sock, util.CHUNK_SIZE)
             i = self.parser.filter_headers(headers, buf)
             if i == -1 and buf:
                 while True:
                     data = sock.recv(p_sock, util.CHUNK_SIZE)
                     if not data: break
                     buf += data
                     i = self.parser.filter_headers(headers, buf)
                     if i != -1: break
                     
             if p.status_int != 200:
                 raise ProxyError('Error status=%s' % p.status)
                 
             sock._ssl_wrap_socket(p_sock, None, None)
             
             # update socket
             req.socket = p_sock
             req.host = proxy_host
     else:
         proxy = os.environ.get('http_proxy')
         if proxy:
             proxy_uri = urlparse.urlparse(proxy)
             proxy_host, proxy_port = self._host_port(proxy_uri)
             if proxy_auth:
                 headers['Proxy-Authorization'] = proxy_auth.strip()
                 req.headers.append(('Proxy-Authorization', 
                          proxy_auth.strip()))
             req.host = proxy_host
             req.port = proxy_port