|
14 | 14 | import signal |
15 | 15 | import sys |
16 | 16 | import tempfile |
17 | | -import urllib |
18 | 17 | import unicodedata |
19 | 18 | import warnings |
20 | 19 | from collections import defaultdict |
|
27 | 26 | # pylint: disable=no-name-in-module, import-error, wrong-import-position |
28 | 27 | if sys.version_info >= (3,): |
29 | 28 | from urllib.parse import urlparse |
30 | | - from urllib.request import urlopen, FancyURLopener |
31 | | - import urllib.request |
| 29 | + from urllib.request import ProxyHandler, Request, build_opener |
32 | 30 | else: |
33 | | - from urllib import urlopen, FancyURLopener |
| 31 | + from urllib2 import ProxyHandler, Request, build_opener |
34 | 32 | from urlparse import urlparse |
35 | 33 |
|
36 | 34 |
|
@@ -591,16 +589,17 @@ def fetch_files_to_be_fetched(self): |
591 | 589 | """ |
592 | 590 | Fetches files from the fetch.txt |
593 | 591 | """ |
594 | | - if sys.version_info >= (3,): |
595 | | - urllib.request._urlopener = BagFetcherURLOpener() # pylint: disable=protected-access |
596 | | - else: |
597 | | - urllib._urlopener = BagFetcherURLOpener() # pylint: disable=protected-access |
| 592 | + proxy_handler = ProxyHandler() # will default to adhere to *_proxy env vars |
| 593 | + opener = build_opener(proxy_handler) |
| 594 | + user_agent = "bagit.py/%s (Python/%s)" % (VERSION, sys.version_info) |
598 | 595 | for url, expected_size, filename in self.fetch_entries(): |
599 | 596 | expected_size = int(expected_size) # FIXME should be int in the first place |
600 | 597 | if filename in self.payload_files(): |
601 | 598 | LOGGER.info(_("File already fetched: %s"), filename) |
602 | 599 | continue |
603 | | - resp = urlopen(url) |
| 600 | + req = Request(url) |
| 601 | + req.add_header('User-Agent', user_agent) |
| 602 | + resp = opener.open(req) |
604 | 603 | headers = resp.info() |
605 | 604 | if "content-length" not in headers: |
606 | 605 | LOGGER.warning(_("Server sent no content-length for <%s>"), url) |
@@ -977,8 +976,6 @@ def _path_is_dangerous(self, path): |
977 | 976 | common = os.path.commonprefix((bag_path, real_path)) |
978 | 977 | return not (common == bag_path) |
979 | 978 |
|
980 | | -class BagFetcherURLOpener(FancyURLopener): |
981 | | - version = "bagit.py/%s (Python/%s)" % (VERSION, sys.version_info) |
982 | 979 |
|
983 | 980 | class BagError(Exception): |
984 | 981 | pass |
|
0 commit comments