1 |
commit: 02940e7725e03f0fd23c6f957aa6c9cf3a35713a |
2 |
Author: Zac Medico <zmedico <AT> gentoo <DOT> org> |
3 |
AuthorDate: Thu Aug 2 02:20:17 2012 +0000 |
4 |
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> |
5 |
CommitDate: Thu Aug 2 02:20:17 2012 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/portage.git;a=commit;h=02940e77 |
7 |
|
8 |
_urlopen: fix python3 http password breakage |
9 |
|
10 |
This broke in commit e06cb6d66db37ac7ab77acf65038b1f770c13c96 since |
11 |
CompressedResponseProcessor did not include password auth support. |
12 |
|
13 |
--- |
14 |
pym/portage/util/_urlopen.py | 40 +++++++++++----------------------------- |
15 |
1 files changed, 11 insertions(+), 29 deletions(-) |
16 |
|
17 |
diff --git a/pym/portage/util/_urlopen.py b/pym/portage/util/_urlopen.py |
18 |
index bcd8f7c..768ccb8 100644 |
19 |
--- a/pym/portage/util/_urlopen.py |
20 |
+++ b/pym/portage/util/_urlopen.py |
21 |
@@ -27,14 +27,20 @@ TIMESTAMP_TOLERANCE=5 |
22 |
|
23 |
def urlopen(url, if_modified_since=None): |
24 |
parse_result = urllib_parse.urlparse(url) |
25 |
- try: |
26 |
- if parse_result.scheme not in ("http", "https"): |
27 |
- return _urlopen(url) |
28 |
+ if parse_result.scheme not in ("http", "https"): |
29 |
+ return _urlopen(url) |
30 |
+ else: |
31 |
+ netloc = urllib_parse_splituser(parse_result.netloc)[1] |
32 |
+ url = urllib_parse.urlunparse((parse_result.scheme, netloc, parse_result.path, parse_result.params, parse_result.query, parse_result.fragment)) |
33 |
+ password_manager = urllib_request.HTTPPasswordMgrWithDefaultRealm() |
34 |
request = urllib_request.Request(url) |
35 |
request.add_header('User-Agent', 'Gentoo Portage') |
36 |
if if_modified_since: |
37 |
request.add_header('If-Modified-Since', _timestamp_to_http(if_modified_since)) |
38 |
- opener = urllib_request.build_opener(CompressedResponseProcessor) |
39 |
+ if parse_result.username is not None: |
40 |
+ password_manager.add_password(None, url, parse_result.username, parse_result.password) |
41 |
+ auth_handler = CompressedResponseProcessor(password_manager) |
42 |
+ opener = urllib_request.build_opener(auth_handler) |
43 |
hdl = opener.open(request) |
44 |
if hdl.headers.get('last-modified', ''): |
45 |
try: |
46 |
@@ -44,30 +50,6 @@ def urlopen(url, if_modified_since=None): |
47 |
add_header = hdl.headers.addheader |
48 |
add_header('timestamp', _http_to_timestamp(hdl.headers.get('last-modified'))) |
49 |
return hdl |
50 |
- except SystemExit: |
51 |
- raise |
52 |
- except Exception as e: |
53 |
- if hasattr(e, 'code') and e.code == 304: # HTTPError 304: not modified |
54 |
- raise |
55 |
- if sys.hexversion < 0x3000000: |
56 |
- raise |
57 |
- if parse_result.scheme not in ("http", "https") or \ |
58 |
- not parse_result.username: |
59 |
- raise |
60 |
- |
61 |
- return _new_urlopen(url) |
62 |
- |
63 |
-def _new_urlopen(url): |
64 |
- # This is experimental code for bug #413983. |
65 |
- parse_result = urllib_parse.urlparse(url) |
66 |
- netloc = urllib_parse_splituser(parse_result.netloc)[1] |
67 |
- url = urllib_parse.urlunparse((parse_result.scheme, netloc, parse_result.path, parse_result.params, parse_result.query, parse_result.fragment)) |
68 |
- password_manager = urllib_request.HTTPPasswordMgrWithDefaultRealm() |
69 |
- if parse_result.username is not None: |
70 |
- password_manager.add_password(None, url, parse_result.username, parse_result.password) |
71 |
- auth_handler = urllib_request.HTTPBasicAuthHandler(password_manager) |
72 |
- opener = urllib_request.build_opener(auth_handler) |
73 |
- return opener.open(url) |
74 |
|
75 |
def _timestamp_to_http(timestamp): |
76 |
dt = datetime.fromtimestamp(float(long(timestamp)+TIMESTAMP_TOLERANCE)) |
77 |
@@ -79,7 +61,7 @@ def _http_to_timestamp(http_datetime_string): |
78 |
timestamp = mktime(tuple) |
79 |
return str(long(timestamp)) |
80 |
|
81 |
-class CompressedResponseProcessor(urllib_request.BaseHandler): |
82 |
+class CompressedResponseProcessor(urllib_request.HTTPBasicAuthHandler): |
83 |
# Handler for compressed responses. |
84 |
|
85 |
def http_request(self, req): |