Skip to content

Commit 3a2ff97

Browse files
committed
Move _http_get_download to network.download
1 parent e354b72 commit 3a2ff97

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

src/pip/_internal/network/download.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from pip._vendor.requests.models import Response
2525

2626
from pip._internal.models.link import Link
27+
from pip._internal.network.session import PipSession
2728

2829
logger = logging.getLogger(__name__)
2930

@@ -125,3 +126,34 @@ def _get_http_response_filename(resp, link):
125126
if ext:
126127
filename += ext
127128
return filename
129+
130+
131+
def _http_get_download(session, link):
132+
# type: (PipSession, Link) -> Response
133+
target_url = link.url.split('#', 1)[0]
134+
resp = session.get(
135+
target_url,
136+
# We use Accept-Encoding: identity here because requests
137+
# defaults to accepting compressed responses. This breaks in
138+
# a variety of ways depending on how the server is configured.
139+
# - Some servers will notice that the file isn't a compressible
140+
# file and will leave the file alone and with an empty
141+
# Content-Encoding
142+
# - Some servers will notice that the file is already
143+
# compressed and will leave the file alone and will add a
144+
# Content-Encoding: gzip header
145+
# - Some servers won't notice anything at all and will take
146+
# a file that's already been compressed and compress it again
147+
# and set the Content-Encoding: gzip header
148+
# By setting this to request only the identity encoding We're
149+
# hoping to eliminate the third case. Hopefully there does not
150+
# exist a server which when given a file will notice it is
151+
# already compressed and that you're not asking for a
152+
# compressed file and will then decompress it before sending
153+
# because if that's the case I don't think it'll ever be
154+
# possible to make this work.
155+
headers={"Accept-Encoding": "identity"},
156+
stream=True,
157+
)
158+
resp.raise_for_status()
159+
return resp

src/pip/_internal/operations/prepare.py

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
)
3030
from pip._internal.network.download import (
3131
_get_http_response_filename,
32+
_http_get_download,
3233
_prepare_download,
3334
)
34-
from pip._internal.network.session import PipSession
3535
from pip._internal.utils.compat import expanduser
3636
from pip._internal.utils.filesystem import copy2_fixed
3737
from pip._internal.utils.hashes import MissingHashes
@@ -61,6 +61,7 @@
6161
from pip._internal.distributions import AbstractDistribution
6262
from pip._internal.index.package_finder import PackageFinder
6363
from pip._internal.models.link import Link
64+
from pip._internal.network.session import PipSession
6465
from pip._internal.req.req_install import InstallRequirement
6566
from pip._internal.req.req_tracker import RequirementTracker
6667
from pip._internal.utils.hashes import Hashes
@@ -306,37 +307,6 @@ def unpack_url(
306307
)
307308

308309

309-
def _http_get_download(session, link):
310-
# type: (PipSession, Link) -> Response
311-
target_url = link.url.split('#', 1)[0]
312-
resp = session.get(
313-
target_url,
314-
# We use Accept-Encoding: identity here because requests
315-
# defaults to accepting compressed responses. This breaks in
316-
# a variety of ways depending on how the server is configured.
317-
# - Some servers will notice that the file isn't a compressible
318-
# file and will leave the file alone and with an empty
319-
# Content-Encoding
320-
# - Some servers will notice that the file is already
321-
# compressed and will leave the file alone and will add a
322-
# Content-Encoding: gzip header
323-
# - Some servers won't notice anything at all and will take
324-
# a file that's already been compressed and compress it again
325-
# and set the Content-Encoding: gzip header
326-
# By setting this to request only the identity encoding We're
327-
# hoping to eliminate the third case. Hopefully there does not
328-
# exist a server which when given a file will notice it is
329-
# already compressed and that you're not asking for a
330-
# compressed file and will then decompress it before sending
331-
# because if that's the case I don't think it'll ever be
332-
# possible to make this work.
333-
headers={"Accept-Encoding": "identity"},
334-
stream=True,
335-
)
336-
resp.raise_for_status()
337-
return resp
338-
339-
340310
class Download(object):
341311
def __init__(
342312
self,

0 commit comments

Comments
 (0)