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