# Path: pyTOP/packages/requests/auth.py
# def dispatch(t):
#     """Given an auth tuple, return an expanded version."""
# 
#     if not t:
#         return t
#     else:
#         t = list(t)
# 
#     # Make sure they're passing in something.
#     assert len(t) >= 2
# 
#     # If only two items are passed in, assume HTTPBasic.
#     if (len(t) == 2):
#         t.insert(0, 'basic')
# 
#     # Allow built-in string referenced auths.
#     if isinstance(t[0], basestring):
#         if t[0] in ('basic', 'forced_basic'):
#             t[0] = http_basic
#         elif t[0] in ('digest',):
#             t[0] = http_digest
# 
#     # Return a custom callable.
#     return (t[0], tuple(t[1:]))
# 
# Path: pyTOP/packages/requests/utils.py
# def get_unicode_from_response(r):
#     """Returns the requested content back in unicode.
# 
#     :param r: Response object to get unicode content from.
# 
#     Tried:
# 
#     1. charset from content-type
# 
#     2. every encodings from ``<meta ... charset=XXX>``
# 
#     3. fall back and replace all unicode characters
# 
#     """
# 
#     tried_encodings = []
# 
#     # Try charset from content-type
#     encoding = get_encoding_from_headers(r.headers)
# 
#     if encoding:
#         try:
#             return unicode(r.content, encoding)
#         except UnicodeError:
#             tried_encodings.append(encoding)
# 
#     # Fall back:
#     try:
#         return unicode(r.content, encoding, errors='replace')
#     except TypeError:
#         return r.content
# 
# def stream_decode_response_unicode(iterator, r):
#     """Stream decodes a iterator."""
#     encoding = get_encoding_from_headers(r.headers)
#     if encoding is None:
#         for item in iterator:
#             yield item
#         return
# 
#     decoder = codecs.getincrementaldecoder(encoding)(errors='replace')
#     for chunk in iterator:
#         rv = decoder.decode(chunk)
#         if rv:
#             yield rv
#     rv = decoder.decode('', final=True)
#     if rv:
#         yield rv
# 
# def decode_gzip(content):
#     """Return gzip-decoded string.
# 
#     :param content: bytestring to gzip-decode.
#     """
# 
#     return zlib.decompress(content, 16 + zlib.MAX_WBITS)
# 
# def stream_decode_gzip(iterator):
#     """Stream decodes a gzip-encoded iterator"""
#     try:
#         dec = zlib.decompressobj(16 + zlib.MAX_WBITS)
#         for chunk in iterator:
#             rv = dec.decompress(chunk)
#             if rv:
#                 yield rv
#         buf = dec.decompress('')
#         rv = buf + dec.flush()
#         if rv:
#             yield rv
#     except zlib.error:
#         pass
# 
# def guess_filename(obj):
#     """Tries to guess the filename of the given object."""
#     name = getattr(obj, 'name', None)
#     if name and name[0] != '<' and name[-1] != '>':
#         return name


import urllib
import zlib
from Cookie import SimpleCookie
from urlparse import urlparse, urlunparse, urljoin, urlsplit
from datetime import datetime
from .auth import dispatch as auth_dispatch
from .hooks import dispatch_hook
from .structures import CaseInsensitiveDict
from .status_codes import codes
from .packages.urllib3.exceptions import MaxRetryError
from .packages.urllib3.exceptions import SSLError as _SSLError
from .packages.urllib3.exceptions import HTTPError as _HTTPError
from .packages.urllib3 import connectionpool, poolmanager
from .packages.urllib3.filepost import encode_multipart_formdata
from .exceptions import (
    Timeout, URLRequired, TooManyRedirects, HTTPError, ConnectionError)
from .utils import (
    get_unicode_from_response, stream_decode_response_unicode,
    decode_gzip, stream_decode_gzip, guess_filename)


    def iter_content(self, chunk_size=10 * 1024, decode_unicode=None):
        """Iterates over the response data.  This avoids reading the content
        at once into memory for large responses.  The chunk size is the number
        of bytes it should read into memory.  This is not necessarily the
        length of each item returned as decoding can take place.
        """
        if self._content_consumed:
            raise RuntimeError(
                'The content for this response was already consumed'
            )

        def generate():
            while 1:
                chunk = self.raw.read(chunk_size)
                if not chunk:
                    break
                yield chunk
            self._content_consumed = True

        gen = generate()

        if 'gzip' in self.headers.get('content-encoding', ''):
            gen = stream_decode_gzip(gen)

        if decode_unicode is None:
            decode_unicode = self.config.get('decode_unicode')

        if decode_unicode:

# Path: pyTOP/packages/requests/auth.py
# def dispatch(t):
#     """Given an auth tuple, return an expanded version."""
# 
#     if not t:
#         return t
#     else:
#         t = list(t)
# 
#     # Make sure they're passing in something.
#     assert len(t) >= 2
# 
#     # If only two items are passed in, assume HTTPBasic.
#     if (len(t) == 2):
#         t.insert(0, 'basic')
# 
#     # Allow built-in string referenced auths.
#     if isinstance(t[0], basestring):
#         if t[0] in ('basic', 'forced_basic'):
#             t[0] = http_basic
#         elif t[0] in ('digest',):
#             t[0] = http_digest
# 
#     # Return a custom callable.
#     return (t[0], tuple(t[1:]))
# 
# Path: pyTOP/packages/requests/utils.py
# def get_unicode_from_response(r):
#     """Returns the requested content back in unicode.
# 
#     :param r: Response object to get unicode content from.
# 
#     Tried:
# 
#     1. charset from content-type
# 
#     2. every encodings from ``<meta ... charset=XXX>``
# 
#     3. fall back and replace all unicode characters
# 
#     """
# 
#     tried_encodings = []
# 
#     # Try charset from content-type
#     encoding = get_encoding_from_headers(r.headers)
# 
#     if encoding:
#         try:
#             return unicode(r.content, encoding)
#         except UnicodeError:
#             tried_encodings.append(encoding)
# 
#     # Fall back:
#     try:
#         return unicode(r.content, encoding, errors='replace')
#     except TypeError:
#         return r.content
# 
# def stream_decode_response_unicode(iterator, r):
#     """Stream decodes a iterator."""
#     encoding = get_encoding_from_headers(r.headers)
#     if encoding is None:
#         for item in iterator:
#             yield item
#         return
# 
#     decoder = codecs.getincrementaldecoder(encoding)(errors='replace')
#     for chunk in iterator:
#         rv = decoder.decode(chunk)
#         if rv:
#             yield rv
#     rv = decoder.decode('', final=True)
#     if rv:
#         yield rv
# 
# def decode_gzip(content):
#     """Return gzip-decoded string.
# 
#     :param content: bytestring to gzip-decode.
#     """
# 
#     return zlib.decompress(content, 16 + zlib.MAX_WBITS)
# 
# def stream_decode_gzip(iterator):
#     """Stream decodes a gzip-encoded iterator"""
#     try:
#         dec = zlib.decompressobj(16 + zlib.MAX_WBITS)
#         for chunk in iterator:
#             rv = dec.decompress(chunk)
#             if rv:
#                 yield rv
#         buf = dec.decompress('')
#         rv = buf + dec.flush()
#         if rv:
#             yield rv
#     except zlib.error:
#         pass
# 
# def guess_filename(obj):
#     """Tries to guess the filename of the given object."""
#     name = getattr(obj, 'name', None)
#     if name and name[0] != '<' and name[-1] != '>':
#         return name


# Path: pyTOP/packages/requests/models.py
import urllib
import zlib
from Cookie import SimpleCookie
from urlparse import urlparse, urlunparse, urljoin, urlsplit
from datetime import datetime
from .auth import dispatch as auth_dispatch
from .hooks import dispatch_hook
from .structures import CaseInsensitiveDict
from .status_codes import codes
from .packages.urllib3.exceptions import MaxRetryError
from .packages.urllib3.exceptions import SSLError as _SSLError
from .packages.urllib3.exceptions import HTTPError as _HTTPError
from .packages.urllib3 import connectionpool, poolmanager
from .packages.urllib3.filepost import encode_multipart_formdata
from .exceptions import (
    Timeout, URLRequired, TooManyRedirects, HTTPError, ConnectionError)
from .utils import (
    get_unicode_from_response, stream_decode_response_unicode,
    decode_gzip, stream_decode_gzip, guess_filename)



    def iter_content(self, chunk_size=10 * 1024, decode_unicode=None):
        """Iterates over the response data.  This avoids reading the content
        at once into memory for large responses.  The chunk size is the number
        of bytes it should read into memory.  This is not necessarily the
        length of each item returned as decoding can take place.
        """
        if self._content_consumed:
            raise RuntimeError(
                'The content for this response was already consumed'
            )

        def generate():
            while 1:
                chunk = self.raw.read(chunk_size)
                if not chunk:
                    break
                yield chunk
            self._content_consumed = True

        gen = generate()

        if 'gzip' in self.headers.get('content-encoding', ''):
            gen = stream_decode_gzip(gen)

        if decode_unicode is None:
            decode_unicode = self.config.get('decode_unicode')

        if decode_unicode:

            gen = stream_decode_response_unicode(gen, self)