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