コード例 #1
0
    def on_connect(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 = util.parse_netloc(proxy_uri)

                if req.pool is not None:
                    s = req.pool.get((proxy_host, proxy_port))
                    if s:
                        self._sock = s
                        req.host = proxy_host
                        req.port = proxy_port
                        return

                # 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
                parser = http.ResponseParser(p_sock)
                resp = parser.next()

                if resp.status_int != 200:
                    raise ProxyError('Error status=%s' % resp.status)

                sock._ssl_wrap_socket(p_sock, None, None)

                # update socket
                req._sock = p_sock
                req.host = proxy_host
                req.port = proxy_port
        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:
                    req.headers.append(
                        ('Proxy-Authorization', proxy_auth.strip()))

                req.host = proxy_host
                req.port = proxy_port
コード例 #2
0
ファイル: simpleproxy.py プロジェクト: sivang/navistore
    def on_connect(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 = util.parse_netloc(proxy_uri)
                
                if req.pool is not None:
                    s = req.pool.get((proxy_host, proxy_port))
                    if s:
                        self._sock = s
                        req.host = proxy_host
                        req.port = proxy_port
                        return
                
                # 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
                parser = http.ResponseParser(p_sock)
                resp = parser.next()
 
                if resp.status_int != 200:
                    raise ProxyError('Error status=%s' % resp.status)
                    
                sock._ssl_wrap_socket(p_sock, None, None)
                
                # update socket
                req._sock = p_sock
                req.host = proxy_host
                req.port = proxy_port
        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:
                    req.headers.append(('Proxy-Authorization', 
                             proxy_auth.strip()))
                             
                req.host = proxy_host
                req.port = proxy_port
コード例 #3
0
ファイル: client.py プロジェクト: sivang/navistore
 def do_send(self):
     tries = 2
     while True:
         try:
             # get socket
             self._sock = self.make_connection()
             
             # apply on request filters
             self.filters.apply("on_request", self)
             
             # build request headers
             self.req_headers = req_headers = self._req_headers()
             
             # send request
             log.info('Start request: %s %s', self.method, self.url)
             log.debug("Request headers: [%s]", req_headers)
             
             self._sock.sendall("".join(req_headers))
             
             if self.body is not None:
                 if hasattr(self.body, 'read'):
                     if hasattr(self.body, 'seek'): self.body.seek(0)
                     sock.sendfile(self._sock, self.body, self.chunked)
                 elif isinstance(self.body, types.StringTypes):
                     sock.send(self._sock, self.body, self.chunked)
                 else:
                     sock.sendlines(self._sock, self.body, self.chunked)
                     
                 if self.chunked: # final chunk
                     sock.send_chunk(self._sock, "")
                     
             return self.start_response()
         except socket.gaierror, e:
             self.clean_connections()
             raise
         except socket.error, e:
             if e[0] not in (errno.EAGAIN, errno.ECONNABORTED, errno.EPIPE,
                         errno.ECONNREFUSED) or tries <= 0:
                 self.clean_connections()
                 raise
             if e[0] == errno.EPIPE:
                 log.debug("Got EPIPE")
                 self.clean_connections()
コード例 #4
0
    def do_send(self):
        tries = 2
        while True:
            try:
                # get socket
                self._sock = self.make_connection()

                # apply on request filters
                self.filters.apply("on_request", self)

                # build request headers
                self.req_headers = req_headers = self._req_headers()

                # send request
                log.info('Start request: %s %s', self.method, self.url)
                log.debug("Request headers: [%s]", req_headers)

                self._sock.sendall("".join(req_headers))

                if self.body is not None:
                    if hasattr(self.body, 'read'):
                        if hasattr(self.body, 'seek'): self.body.seek(0)
                        sock.sendfile(self._sock, self.body, self.chunked)
                    elif isinstance(self.body, types.StringTypes):
                        sock.send(self._sock, self.body, self.chunked)
                    else:
                        sock.sendlines(self._sock, self.body, self.chunked)

                    if self.chunked:  # final chunk
                        sock.send_chunk(self._sock, "")

                return self.start_response()
            except socket.gaierror, e:
                self.clean_connections()
                raise
            except socket.error, e:
                if e[0] not in (errno.EAGAIN, errno.ECONNABORTED, errno.EPIPE,
                                errno.ECONNREFUSED) or tries <= 0:
                    self.clean_connections()
                    raise
                if e[0] == errno.EPIPE:
                    log.debug("Got EPIPE")
                    self.clean_connections()