Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the raw stream to prevent decoding the response #1435

Merged
merged 1 commit into from
Jan 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Changelog
* Remove the version requirement for setuptools so that it still functions with
setuptools < 0.8 installed (Pull #1434).

* Don't decode downloaded files that have a ``Content-Encoding`` header.


1.5 (2014-01-01)
----------------
Expand Down
22 changes: 20 additions & 2 deletions pip/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
from pip._vendor import requests
from pip._vendor.requests.adapters import BaseAdapter
from pip._vendor.requests.auth import AuthBase, HTTPBasicAuth
from pip._vendor.requests.exceptions import InvalidURL
from pip._vendor.requests.compat import IncompleteRead
from pip._vendor.requests.exceptions import InvalidURL, ChunkedEncodingError
from pip._vendor.requests.models import Response
from pip._vendor.requests.structures import CaseInsensitiveDict

Expand Down Expand Up @@ -420,7 +421,24 @@ def _download_url(resp, link, temp_location):
logger.notify('Downloading %s' % show_url)
logger.info('Downloading from URL %s' % link)

for chunk in resp.iter_content(4096):
def resp_read(chunk_size):
try:
# Special case for urllib3.
try:
for chunk in resp.raw.stream(
chunk_size, decode_content=False):
yield chunk
except IncompleteRead as e:
raise ChunkedEncodingError(e)
except AttributeError:
# Standard file-like object.
while True:
chunk = resp.raw.read(chunk_size)
if not chunk:
break
yield chunk

for chunk in resp_read(4096):
downloaded += len(chunk)
if show_progress:
if not total_length:
Expand Down
13 changes: 11 additions & 2 deletions tests/unit/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,23 @@ def _write_file(fn, contents):
fh.write(contents)


class MockResponse(object):
class FakeStream(object):

def __init__(self, contents):
self._io = BytesIO(contents)

def iter_content(self, size):
def read(self, size, decode_content=None):
return self._io.read(size)

def stream(self, size, decode_content=None):
yield self._io.read(size)


class MockResponse(object):

def __init__(self, contents):
self.raw = FakeStream(contents)

def raise_for_status(self):
pass

Expand Down