Show More
@@ -1,169 +1,172 b'' | |||
|
1 | 1 | # httprepo.py - HTTP repository proxy classes for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms |
|
6 | 6 | # of the GNU General Public License, incorporated herein by reference. |
|
7 | 7 | |
|
8 | 8 | from node import * |
|
9 | 9 | from remoterepo import * |
|
10 | 10 | from i18n import gettext as _ |
|
11 | 11 | from demandload import * |
|
12 | 12 | demandload(globals(), "hg os urllib urllib2 urlparse zlib util httplib") |
|
13 | 13 | |
|
14 | 14 | class passwordmgr(urllib2.HTTPPasswordMgr): |
|
15 | 15 | def __init__(self, ui): |
|
16 | 16 | urllib2.HTTPPasswordMgr.__init__(self) |
|
17 | 17 | self.ui = ui |
|
18 | 18 | |
|
19 | 19 | def find_user_password(self, realm, authuri): |
|
20 | 20 | authinfo = urllib2.HTTPPasswordMgr.find_user_password( |
|
21 | 21 | self, realm, authuri) |
|
22 | 22 | if authinfo != (None, None): |
|
23 | 23 | return authinfo |
|
24 | 24 | |
|
25 | 25 | self.ui.write(_("http authorization required\n")) |
|
26 | 26 | self.ui.status(_("realm: %s\n") % realm) |
|
27 | 27 | user = self.ui.prompt(_("user:"), default=None) |
|
28 | 28 | passwd = self.ui.getpass() |
|
29 | 29 | |
|
30 | 30 | self.add_password(realm, authuri, user, passwd) |
|
31 | 31 | return (user, passwd) |
|
32 | 32 | |
|
33 | 33 | class httprepository(remoterepository): |
|
34 | 34 | def __init__(self, ui, path): |
|
35 | 35 | # fix missing / after hostname |
|
36 | 36 | s = urlparse.urlsplit(path) |
|
37 | 37 | partial = s[2] |
|
38 | 38 | if not partial: partial = "/" |
|
39 | 39 | self.url = urlparse.urlunsplit((s[0], s[1], partial, '', '')) |
|
40 | 40 | self.ui = ui |
|
41 | 41 | no_list = [ "localhost", "127.0.0.1" ] |
|
42 | 42 | host = ui.config("http_proxy", "host") |
|
43 | 43 | if host is None: |
|
44 | 44 | host = os.environ.get("http_proxy") |
|
45 | 45 | if host and host.startswith('http://'): |
|
46 | 46 | host = host[7:] |
|
47 | 47 | user = ui.config("http_proxy", "user") |
|
48 | 48 | passwd = ui.config("http_proxy", "passwd") |
|
49 | 49 | no = ui.config("http_proxy", "no") |
|
50 | 50 | if no is None: |
|
51 | 51 | no = os.environ.get("no_proxy") |
|
52 | 52 | if no: |
|
53 | 53 | no_list = no_list + no.split(",") |
|
54 | 54 | |
|
55 | 55 | no_proxy = 0 |
|
56 | 56 | for h in no_list: |
|
57 | 57 | if (path.startswith("http://" + h + "/") or |
|
58 | 58 | path.startswith("http://" + h + ":") or |
|
59 | 59 | path == "http://" + h): |
|
60 | 60 | no_proxy = 1 |
|
61 | 61 | |
|
62 | 62 | # Note: urllib2 takes proxy values from the environment and those will |
|
63 | 63 | # take precedence |
|
64 | 64 | for env in ["HTTP_PROXY", "http_proxy", "no_proxy"]: |
|
65 | 65 | try: |
|
66 | 66 | if os.environ.has_key(env): |
|
67 | 67 | del os.environ[env] |
|
68 | 68 | except OSError: |
|
69 | 69 | pass |
|
70 | 70 | |
|
71 | 71 | proxy_handler = urllib2.BaseHandler() |
|
72 | 72 | if host and not no_proxy: |
|
73 | 73 | proxy_handler = urllib2.ProxyHandler({"http" : "http://" + host}) |
|
74 | 74 | |
|
75 | 75 | proxyauthinfo = None |
|
76 | 76 | if user and passwd: |
|
77 | 77 | passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm() |
|
78 | 78 | passmgr.add_password(None, host, user, passwd) |
|
79 | 79 | proxyauthinfo = urllib2.ProxyBasicAuthHandler(passmgr) |
|
80 | 80 | |
|
81 | 81 | if ui.interactive: |
|
82 | 82 | passmgr = passwordmgr(ui) |
|
83 | 83 | opener = urllib2.build_opener( |
|
84 | 84 | proxy_handler, proxyauthinfo, |
|
85 | 85 | urllib2.HTTPBasicAuthHandler(passmgr), |
|
86 | 86 | urllib2.HTTPDigestAuthHandler(passmgr)) |
|
87 | 87 | else: |
|
88 | 88 | opener = urllib2.build_opener(proxy_handler, proxyauthinfo) |
|
89 | 89 | |
|
90 | 90 | # 1.0 here is the _protocol_ version |
|
91 | 91 | opener.addheaders = [('User-agent', 'mercurial/proto-1.0')] |
|
92 | 92 | urllib2.install_opener(opener) |
|
93 | 93 | |
|
94 | 94 | def dev(self): |
|
95 | 95 | return -1 |
|
96 | 96 | |
|
97 | 97 | def lock(self): |
|
98 | 98 | raise util.Abort(_('operation not supported over http')) |
|
99 | 99 | |
|
100 | 100 | def do_cmd(self, cmd, **args): |
|
101 | 101 | self.ui.debug(_("sending %s command\n") % cmd) |
|
102 | 102 | q = {"cmd": cmd} |
|
103 | 103 | q.update(args) |
|
104 | 104 | qs = urllib.urlencode(q) |
|
105 | 105 | cu = "%s?%s" % (self.url, qs) |
|
106 | resp = urllib2.urlopen(cu) | |
|
106 | try: | |
|
107 | resp = urllib2.urlopen(cu) | |
|
108 | except httplib.HTTPException, inst: | |
|
109 | raise IOError(None, _('http error while sending %s command') % cmd) | |
|
107 | 110 | proto = resp.headers['content-type'] |
|
108 | 111 | |
|
109 | 112 | # accept old "text/plain" and "application/hg-changegroup" for now |
|
110 | 113 | if not proto.startswith('application/mercurial') and \ |
|
111 | 114 | not proto.startswith('text/plain') and \ |
|
112 | 115 | not proto.startswith('application/hg-changegroup'): |
|
113 | 116 | raise hg.RepoError(_("'%s' does not appear to be an hg repository") % |
|
114 | 117 | self.url) |
|
115 | 118 | |
|
116 | 119 | if proto.startswith('application/mercurial'): |
|
117 | 120 | version = proto[22:] |
|
118 | 121 | if float(version) > 0.1: |
|
119 | 122 | raise hg.RepoError(_("'%s' uses newer protocol %s") % |
|
120 | 123 | (self.url, version)) |
|
121 | 124 | |
|
122 | 125 | return resp |
|
123 | 126 | |
|
124 | 127 | def heads(self): |
|
125 | 128 | d = self.do_cmd("heads").read() |
|
126 | 129 | try: |
|
127 | 130 | return map(bin, d[:-1].split(" ")) |
|
128 | 131 | except: |
|
129 | 132 | self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n") |
|
130 | 133 | raise |
|
131 | 134 | |
|
132 | 135 | def branches(self, nodes): |
|
133 | 136 | n = " ".join(map(hex, nodes)) |
|
134 | 137 | d = self.do_cmd("branches", nodes=n).read() |
|
135 | 138 | try: |
|
136 | 139 | br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ] |
|
137 | 140 | return br |
|
138 | 141 | except: |
|
139 | 142 | self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n") |
|
140 | 143 | raise |
|
141 | 144 | |
|
142 | 145 | def between(self, pairs): |
|
143 | 146 | n = "\n".join(["-".join(map(hex, p)) for p in pairs]) |
|
144 | 147 | d = self.do_cmd("between", pairs=n).read() |
|
145 | 148 | try: |
|
146 | 149 | p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ] |
|
147 | 150 | return p |
|
148 | 151 | except: |
|
149 | 152 | self.ui.warn(_("unexpected response:\n") + d[:400] + "\n...\n") |
|
150 | 153 | raise |
|
151 | 154 | |
|
152 | 155 | def changegroup(self, nodes, kind): |
|
153 | 156 | n = " ".join(map(hex, nodes)) |
|
154 | 157 | f = self.do_cmd("changegroup", roots=n) |
|
155 | 158 | bytes = 0 |
|
156 | 159 | |
|
157 | 160 | def zgenerator(f): |
|
158 | 161 | zd = zlib.decompressobj() |
|
159 | 162 | try: |
|
160 | 163 | for chnk in f: |
|
161 | 164 | yield zd.decompress(chnk) |
|
162 | 165 | except httplib.HTTPException, inst: |
|
163 | 166 | raise IOError(None, _('connection ended unexpectedly')) |
|
164 | 167 | yield zd.flush() |
|
165 | 168 | |
|
166 | 169 | return util.chunkbuffer(zgenerator(util.filechunkiter(f))) |
|
167 | 170 | |
|
168 | 171 | class httpsrepository(httprepository): |
|
169 | 172 | pass |
General Comments 0
You need to be logged in to leave comments.
Login now