# Path: pyTOP/packages/requests/utils.py
# def randombytes(n):
#     """Return n random bytes."""
#     # Use /dev/urandom if it is available.  Fall back to random module
#     # if not.  It might be worthwhile to extend this function to use
#     # other platform-specific mechanisms for getting random bytes.
#     if os.path.exists("/dev/urandom"):
#         f = open("/dev/urandom")
#         s = f.read(n)
#         f.close()
#         return s
#     else:
#         L = [chr(random.randrange(0, 256)) for i in range(n)]
#         return "".join(L)
# 
# def parse_dict_header(value):
#     """Parse lists of key, value pairs as described by RFC 2068 Section 2 and
#     convert them into a python dict:
# 
#     >>> d = parse_dict_header('foo="is a fish", bar="as well"')
#     >>> type(d) is dict
#     True
#     >>> sorted(d.items())
#     [('bar', 'as well'), ('foo', 'is a fish')]
# 
#     If there is no value for a key it will be `None`:
# 
#     >>> parse_dict_header('key_without_value')
#     {'key_without_value': None}
# 
#     To create a header from the :class:`dict` again, use the
#     :func:`dump_header` function.
# 
#     :param value: a string with a dict header.
#     :return: :class:`dict`
#     """
#     result = {}
#     for item in _parse_list_header(value):
#         if '=' not in item:
#             result[item] = None
#             continue
#         name, value = item.split('=', 1)
#         if value[:1] == value[-1:] == '"':
#             value = unquote_header_value(value[1:-1])
#         result[name] = value
#     return result


import time
import hashlib
from base64 import b64encode
from urlparse import urlparse
from .utils import randombytes, parse_dict_header

def http_basic(r, username, password):
    """Attaches HTTP Basic Authentication to the given Request object.
    Arguments should be considered non-positional.

    """
    username = str(username)
    password = str(password)

    auth_s = b64encode('%s:%s' % (username, password))
    r.headers['Authorization'] = ('Basic %s' % auth_s)

    return r


def http_digest(r, username, password):
    """Attaches HTTP Digest Authentication to the given Request object.
    Arguments should be considered non-positional.
    """

    def handle_401(r):
        """Takes the given response and tries digest-auth, if needed."""

        s_auth = r.headers.get('www-authenticate', '')

        if 'digest' in s_auth.lower():

            last_nonce = ''
            nonce_count = 0


# Path: pyTOP/packages/requests/utils.py
# def randombytes(n):
#     """Return n random bytes."""
#     # Use /dev/urandom if it is available.  Fall back to random module
#     # if not.  It might be worthwhile to extend this function to use
#     # other platform-specific mechanisms for getting random bytes.
#     if os.path.exists("/dev/urandom"):
#         f = open("/dev/urandom")
#         s = f.read(n)
#         f.close()
#         return s
#     else:
#         L = [chr(random.randrange(0, 256)) for i in range(n)]
#         return "".join(L)
# 
# def parse_dict_header(value):
#     """Parse lists of key, value pairs as described by RFC 2068 Section 2 and
#     convert them into a python dict:
# 
#     >>> d = parse_dict_header('foo="is a fish", bar="as well"')
#     >>> type(d) is dict
#     True
#     >>> sorted(d.items())
#     [('bar', 'as well'), ('foo', 'is a fish')]
# 
#     If there is no value for a key it will be `None`:
# 
#     >>> parse_dict_header('key_without_value')
#     {'key_without_value': None}
# 
#     To create a header from the :class:`dict` again, use the
#     :func:`dump_header` function.
# 
#     :param value: a string with a dict header.
#     :return: :class:`dict`
#     """
#     result = {}
#     for item in _parse_list_header(value):
#         if '=' not in item:
#             result[item] = None
#             continue
#         name, value = item.split('=', 1)
#         if value[:1] == value[-1:] == '"':
#             value = unquote_header_value(value[1:-1])
#         result[name] = value
#     return result


# Path: pyTOP/packages/requests/auth.py
import time
import hashlib
from base64 import b64encode
from urlparse import urlparse
from .utils import randombytes, parse_dict_header


def http_basic(r, username, password):
    """Attaches HTTP Basic Authentication to the given Request object.
    Arguments should be considered non-positional.

    """
    username = str(username)
    password = str(password)

    auth_s = b64encode('%s:%s' % (username, password))
    r.headers['Authorization'] = ('Basic %s' % auth_s)

    return r


def http_digest(r, username, password):
    """Attaches HTTP Digest Authentication to the given Request object.
    Arguments should be considered non-positional.
    """

    def handle_401(r):
        """Takes the given response and tries digest-auth, if needed."""

        s_auth = r.headers.get('www-authenticate', '')

        if 'digest' in s_auth.lower():

            last_nonce = ''
            nonce_count = 0


            chal = parse_dict_header(s_auth.replace('Digest ', ''))