Beispiel #1
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
Beispiel #2
0
    def proxy_connection(self, request, req_addr, is_ssl):
        """ do the proxy connection """
        proxy_settings = os.environ.get('%s_proxy' %
                request.parsed_url.scheme)

        if proxy_settings and proxy_settings is not None:
            request.is_proxied = True

            proxy_settings, proxy_auth =  _get_proxy_auth(proxy_settings)
            addr = parse_netloc(urlparse.urlparse(proxy_settings))

            if is_ssl:
                if proxy_auth:
                    proxy_auth = 'Proxy-authorization: %s' % proxy_auth
                proxy_connect = 'CONNECT %s:%s HTTP/1.0\r\n' % req_addr

                user_agent = request.headers.iget('user_agent')
                if not user_agent:
                    user_agent = "User-Agent: restkit/%s\r\n" % __version__

                proxy_pieces = '%s%s%s\r\n' % (proxy_connect, proxy_auth,
                        user_agent)

                sck = self._manager.find_socket(addr, is_ssl)
                if sck is None:
                    sck = self.connect(addr, is_ssl)

                send(sck, proxy_pieces)
                unreader = http.Unreader(sck)
                resp = http.Request(unreader)
                body = resp.body.read()
                if resp.status_int != 200:
                    raise ProxyError("Tunnel connection failed: %d %s" %
                            (resp.status_int, body))

                return sck, addr, []
            else:
                headers = []
                if proxy_auth:
                    headers = [('Proxy-authorization', proxy_auth)]

                sck = self._manager.find_socket(addr, is_ssl)
                if sck is None:
                    sck = self.connect(addr, is_ssl)
                return sck, addr, headers
        return None, req_addr, []
Beispiel #3
0
    def proxy_connection(self, request, req_addr, is_ssl):
        """ do the proxy connection """
        proxy_settings = os.environ.get('%s_proxy' %
                request.parsed_url.scheme)

        if proxy_settings and proxy_settings is not None:
            request.is_proxied = True

            proxy_settings, proxy_auth =  _get_proxy_auth(proxy_settings)
            addr = parse_netloc(urlparse.urlparse(proxy_settings))

            if is_ssl:
                if proxy_auth:
                    proxy_auth = 'Proxy-authorization: %s' % proxy_auth
                proxy_connect = 'CONNECT %s:%s HTTP/1.0\r\n' % req_addr

                user_agent = request.headers.iget('user_agent')
                if not user_agent:
                    user_agent = "User-Agent: restkit/%s\r\n" % __version__

                proxy_pieces = '%s%s%s\r\n' % (proxy_connect, proxy_auth,
                        user_agent)

                sck = self._manager.find_socket(addr, is_ssl)
                if sck is None:
                    sck = self.connect(addr, is_ssl)

                send(sck, proxy_pieces)
                unreader = http.Unreader(sck)
                resp = Request(unreader)
                body = resp.body.read()
                if resp.status_int != 200:
                    raise ProxyError("Tunnel connection failed: %d %s" %
                            (resp.status_int, body))

                return sck, addr, []
            else:
                headers = []
                if proxy_auth:
                    headers = [('Proxy-authorization', proxy_auth)]

                sck = self._manager.find_socket(addr, is_ssl)
                if sck is None:
                    sck = self.connect(addr, is_ssl)
                return sck, addr, headers
        return None, req_addr, []
Beispiel #4
0
 def do_send(self):
     tries = 2
     while True:
         try:
             # get socket
             self._sock = self.make_connection()
             
             # apply on request filters
             for bf in self.request_filters:
                 bf.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]" % str(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()
    def perform(self, request):
        """ perform the request. If an error happen it will first try to
        restart it """

        if log.isEnabledFor(logging.DEBUG):
            log.debug("Start to perform request: %s %s %s" %
                    (request.host, request.method, request.path))

        tries = self.max_tries
        wait = self.wait_tries
        while tries > 0:
            try:
                # get or create a connection to the remote host
                connection = self.get_connection(request)
                sck = connection.socket()

                # send headers
                msg = self.make_headers_string(request,
                        connection.extra_headers)

                # send body
                if request.body is not None:
                    chunked = request.is_chunked()
                    if request.headers.iget('content-length') is None and \
                            not chunked:
                        raise RequestError(
                                "Can't determine content length and " +
                                "Transfer-Encoding header is not chunked")


                    # handle 100-Continue status
                    # http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3
                    hdr_expect = request.headers.iget("expect")
                    if hdr_expect is not None and \
                            hdr_expect.lower() == "100-continue":
                        sck.sendall(msg)
                        msg = None
                        resp = http.Request(http.Unreader(self._sock))
                        if resp.status_int != 100:
                            self.reset_request()
                            if log.isEnabledFor(logging.DEBUG):
                                log.debug("return response class")
                            return self.response_class(connection,
                                    request, resp)

                    chunked = request.is_chunked()
                    if log.isEnabledFor(logging.DEBUG):
                        log.debug("send body (chunked: %s)" % chunked)


                    if isinstance(request.body, types.StringTypes):
                        if msg is not None:
                            send(sck, msg + request.body, chunked)
                        else:
                            send(sck, request.body, chunked)
                    else:
                        if msg is not None:
                            sck.sendall(msg)

                        if hasattr(request.body, 'read'):
                            if hasattr(request.body, 'seek'): request.body.seek(0)
                            sendfile(sck, request.body, chunked)
                        else:
                            sendlines(sck, request.body, chunked)
                    if chunked:
                        send_chunk(sck, "")
                else:
                    sck.sendall(msg)

                return self.get_response(request, connection)
            except socket.gaierror, e:
                try:
                    connection.close()
                except:
                    pass

                raise RequestError(str(e))
            except socket.timeout, e:
                try:
                    connection.close()
                except:
                    pass

                if tries <= 0:
                    raise RequestTimeout(str(e))
Beispiel #6
0
    def perform(self, request):
        """ perform the request. If an error happen it will first try to
        restart it """

        if log.isEnabledFor(logging.DEBUG):
            log.debug("Start to perform request: %s %s %s" %
                    (request.host, request.method, request.path))

        tries = self.max_tries
        wait = self.wait_tries
        while tries > 0:
            try:
                # get or create a connection to the remote host
                connection = self.get_connection(request)
                sck = connection.socket()

                # send headers
                msg = self.make_headers_string(request,
                        connection.extra_headers)

                # send body
                if request.body is not None:
                    chunked = request.is_chunked()
                    if request.headers.iget('content-length') is None and \
                            not chunked:
                        raise RequestError(
                                "Can't determine content length and " +
                                "Transfer-Encoding header is not chunked")


                    # handle 100-Continue status
                    # http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3
                    hdr_expect = request.headers.iget("expect")
                    if hdr_expect is not None and \
                            hdr_expect.lower() == "100-continue":
                        sck.sendall(msg)
                        msg = None
                        resp = Request(http.Unreader(self._sock))
                        if resp.status_int != 100:
                            self.reset_request()
                            if log.isEnabledFor(logging.DEBUG):
                                log.debug("return response class")
                            return self.response_class(connection,
                                    request, resp)

                    chunked = request.is_chunked()
                    if log.isEnabledFor(logging.DEBUG):
                        log.debug("send body (chunked: %s)" % chunked)


                    if isinstance(request.body, types.StringTypes):
                        if msg is not None:
                            send(sck, msg + request.body, chunked)
                        else:
                            send(sck, request.body, chunked)
                    else:
                        if msg is not None:
                            sck.sendall(msg)

                        if hasattr(request.body, 'read'):
                            if hasattr(request.body, 'seek'): request.body.seek(0)
                            sendfile(sck, request.body, chunked)
                        else:
                            sendlines(sck, request.body, chunked)
                    if chunked:
                        send_chunk(sck, "")
                else:
                    sck.sendall(msg)

                return self.get_response(request, connection)
            except socket.gaierror, e:
                try:
                    connection.close()
                except:
                    pass

                raise RequestError(str(e))
            except socket.timeout, e:
                try:
                    connection.close()
                except:
                    pass

                if tries <= 0:
                    raise RequestTimeout(str(e))