示例#1
0
    def get_credentials_for_url(self, url: str) -> HTTPAuthCredential:
        parsed_url = urllib.parse.urlsplit(url)
        netloc = parsed_url.netloc

        if url not in self._credentials:
            if "@" not in netloc:
                # no credentials were provided in the url, try finding the
                # best repository configuration
                self._credentials[url] = self._get_credentials_for_url(url)
            else:
                # Split from the right because that's how urllib.parse.urlsplit()
                # behaves if more than one @ is present (which can be checked using
                # the password attribute of urlsplit()'s return value).
                auth, netloc = netloc.rsplit("@", 1)
                # Split from the left because that's how urllib.parse.urlsplit()
                # behaves if more than one : is present (which again can be checked
                # using the password attribute of the return value)
                user, password = auth.split(":", 1) if ":" in auth else (auth,
                                                                         "")
                self._credentials[url] = HTTPAuthCredential(
                    urllib.parse.unquote(user),
                    urllib.parse.unquote(password),
                )

        return self._credentials[url]
示例#2
0
    def get_credentials_for_url(
            self, url):  # type: (str) -> Tuple[Optional[str], Optional[str]]
        parsed_url = urllib.parse.urlsplit(url)

        netloc = parsed_url.netloc

        credentials = self._credentials.get(netloc, (None, None))

        if credentials == (None, None):
            if "@" not in netloc:
                credentials = self._get_credentials_for_netloc_from_config(
                    netloc)
            else:
                # Split from the right because that's how urllib.parse.urlsplit()
                # behaves if more than one @ is present (which can be checked using
                # the password attribute of urlsplit()'s return value).
                auth, netloc = netloc.rsplit("@", 1)
                if ":" in auth:
                    # Split from the left because that's how urllib.parse.urlsplit()
                    # behaves if more than one : is present (which again can be checked
                    # using the password attribute of the return value)
                    credentials = auth.split(":", 1)
                else:
                    credentials = auth, None

                credentials = tuple(
                    None if x is None else urllib.parse.unquote(x)
                    for x in credentials)

        if credentials[0] is not None or credentials[1] is not None:
            credentials = (credentials[0] or "", credentials[1] or "")

            self._credentials[netloc] = credentials

        return credentials[0], credentials[1]
示例#3
0
    def __init__(self,
                 url,
                 params={},
                 data=None,
                 cookiejarfile=None,
                 auth=None,
                 method='GET',
                 user_agent='reqgen',
                 auth_type='basic',
                 headers={},
                 files=[],
                 insecure=False,
                 nokeepalive=False,
                 http2=False):

        if url[:4] != "http":
            url = "http://" + url

        url_parts = urlparse(url)
        params = url_parts.params
        hostname = url_parts.netloc.split(':')[0]
        host_ip = socket.gethostbyname(hostname)
        url_with_ip = url.replace(hostname, host_ip)

        if not isinstance(headers, dict):
            raise Exception('Headers must be in dict form')
        if not 'user-agent' in headers:
            headers['user-agent'] = user_agent
        if nokeepalive:
            headers['connection'] = 'close'

        authobj = None
        if auth:
            if auth_type not in ('basic', 'digest'):
                raise Exception('Auth type must be one of: basic, digest')

            auth = auth.split(':', 1)
            if len(auth) == 1:
                raise Exception(
                    'Credentials must be in username:password format')

            if auth_type == "digest":
                authobj = requests.auth.HTTPDigestAuth(auth)
            else:
                authobj = requests.auth.HTTPBasicAuth(auth)

        try:
            cj = MozillaCookieJar()
            if cookiejarfile is not None:
                cj.load(cookiejarfile)
                cookiejar = cj
            else:
                cookiejar = None
        except Exception as e:
            raise Exception(f'Unable to load cookie jar: {e}')

        if not isinstance(insecure, bool):
            raise Exception('Insecure flag must be a boolean')

        upload = []
        for file_data in files:
            i = file_data.split(':')
            file_var = i[0]
            file_path = i[1]
            upload.append((file_var, file_path))

        if upload:
            method = "POST"

        self.method = method.upper()
        self.ipurl = url_with_ip
        self.url = url
        self.params = params
        self.data = data
        self.headers = headers
        self.files = upload
        self.auth = authobj
        self.cookies = cookiejar
        self.verify = not insecure
        self.http2 = http2  # sess.mount('https://', HTTP20Adapter())