##// END OF EJS Templates
httprepo: lowercase debug output
Martin Geisler -
r7923:016d6357 default
parent child Browse files
Show More
@@ -1,243 +1,243 b''
1 1 # httprepo.py - HTTP repository proxy classes for mercurial
2 2 #
3 3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
4 4 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
5 5 #
6 6 # This software may be used and distributed according to the terms
7 7 # of the GNU General Public License, incorporated herein by reference.
8 8
9 9 from node import bin, hex, nullid
10 10 from i18n import _
11 11 import repo, os, urllib, urllib2, urlparse, zlib, util, httplib
12 12 import errno, socket, changegroup, statichttprepo, error, url
13 13
14 14 def zgenerator(f):
15 15 zd = zlib.decompressobj()
16 16 try:
17 17 for chunk in util.filechunkiter(f):
18 18 yield zd.decompress(chunk)
19 19 except httplib.HTTPException:
20 20 raise IOError(None, _('connection ended unexpectedly'))
21 21 yield zd.flush()
22 22
23 23 class httprepository(repo.repository):
24 24 def __init__(self, ui, path):
25 25 self.path = path
26 26 self.caps = None
27 27 self.handler = None
28 28 scheme, netloc, urlpath, query, frag = urlparse.urlsplit(path)
29 29 if query or frag:
30 30 raise util.Abort(_('unsupported URL component: "%s"') %
31 31 (query or frag))
32 32
33 33 # urllib cannot handle URLs with embedded user or passwd
34 34 self._url, authinfo = url.getauthinfo(path)
35 35
36 36 self.ui = ui
37 37 self.ui.debug(_('using %s\n') % self._url)
38 38
39 39 self.urlopener = url.opener(ui, authinfo)
40 40
41 41 def __del__(self):
42 42 for h in self.urlopener.handlers:
43 43 h.close()
44 44 if hasattr(h, "close_all"):
45 45 h.close_all()
46 46
47 47 def url(self):
48 48 return self.path
49 49
50 50 # look up capabilities only when needed
51 51
52 52 def get_caps(self):
53 53 if self.caps is None:
54 54 try:
55 55 self.caps = util.set(self.do_read('capabilities').split())
56 56 except error.RepoError:
57 57 self.caps = util.set()
58 58 self.ui.debug(_('capabilities: %s\n') %
59 59 (' '.join(self.caps or ['none'])))
60 60 return self.caps
61 61
62 62 capabilities = property(get_caps)
63 63
64 64 def lock(self):
65 65 raise util.Abort(_('operation not supported over http'))
66 66
67 67 def do_cmd(self, cmd, **args):
68 68 data = args.pop('data', None)
69 69 headers = args.pop('headers', {})
70 70 self.ui.debug(_("sending %s command\n") % cmd)
71 71 q = {"cmd": cmd}
72 72 q.update(args)
73 73 qs = '?%s' % urllib.urlencode(q)
74 74 cu = "%s%s" % (self._url, qs)
75 75 try:
76 76 if data:
77 77 self.ui.debug(_("sending %s bytes\n") % len(data))
78 78 resp = self.urlopener.open(urllib2.Request(cu, data, headers))
79 79 except urllib2.HTTPError, inst:
80 80 if inst.code == 401:
81 81 raise util.Abort(_('authorization failed'))
82 82 raise
83 83 except httplib.HTTPException, inst:
84 84 self.ui.debug(_('http error while sending %s command\n') % cmd)
85 85 self.ui.print_exc()
86 86 raise IOError(None, inst)
87 87 except IndexError:
88 88 # this only happens with Python 2.3, later versions raise URLError
89 89 raise util.Abort(_('http error, possibly caused by proxy setting'))
90 90 # record the url we got redirected to
91 91 resp_url = resp.geturl()
92 92 if resp_url.endswith(qs):
93 93 resp_url = resp_url[:-len(qs)]
94 94 if self._url != resp_url:
95 95 self.ui.status(_('real URL is %s\n') % resp_url)
96 96 self._url = resp_url
97 97 try:
98 98 proto = resp.getheader('content-type')
99 99 except AttributeError:
100 100 proto = resp.headers['content-type']
101 101
102 102 # accept old "text/plain" and "application/hg-changegroup" for now
103 103 if not (proto.startswith('application/mercurial-') or
104 104 proto.startswith('text/plain') or
105 105 proto.startswith('application/hg-changegroup')):
106 self.ui.debug(_("Requested URL: '%s'\n") % cu)
106 self.ui.debug(_("requested URL: '%s'\n") % cu)
107 107 raise error.RepoError(_("'%s' does not appear to be an hg repository")
108 108 % self._url)
109 109
110 110 if proto.startswith('application/mercurial-'):
111 111 try:
112 112 version = proto.split('-', 1)[1]
113 113 version_info = tuple([int(n) for n in version.split('.')])
114 114 except ValueError:
115 115 raise error.RepoError(_("'%s' sent a broken Content-Type "
116 116 "header (%s)") % (self._url, proto))
117 117 if version_info > (0, 1):
118 118 raise error.RepoError(_("'%s' uses newer protocol %s") %
119 119 (self._url, version))
120 120
121 121 return resp
122 122
123 123 def do_read(self, cmd, **args):
124 124 fp = self.do_cmd(cmd, **args)
125 125 try:
126 126 return fp.read()
127 127 finally:
128 128 # if using keepalive, allow connection to be reused
129 129 fp.close()
130 130
131 131 def lookup(self, key):
132 132 self.requirecap('lookup', _('look up remote revision'))
133 133 d = self.do_cmd("lookup", key = key).read()
134 134 success, data = d[:-1].split(' ', 1)
135 135 if int(success):
136 136 return bin(data)
137 137 raise error.RepoError(data)
138 138
139 139 def heads(self):
140 140 d = self.do_read("heads")
141 141 try:
142 142 return map(bin, d[:-1].split(" "))
143 143 except:
144 144 raise error.ResponseError(_("unexpected response:"), d)
145 145
146 146 def branches(self, nodes):
147 147 n = " ".join(map(hex, nodes))
148 148 d = self.do_read("branches", nodes=n)
149 149 try:
150 150 br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ]
151 151 return br
152 152 except:
153 153 raise error.ResponseError(_("unexpected response:"), d)
154 154
155 155 def between(self, pairs):
156 156 batch = 8 # avoid giant requests
157 157 r = []
158 158 for i in xrange(0, len(pairs), batch):
159 159 n = " ".join(["-".join(map(hex, p)) for p in pairs[i:i + batch]])
160 160 d = self.do_read("between", pairs=n)
161 161 try:
162 162 r += [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ]
163 163 except:
164 164 raise error.ResponseError(_("unexpected response:"), d)
165 165 return r
166 166
167 167 def changegroup(self, nodes, kind):
168 168 n = " ".join(map(hex, nodes))
169 169 f = self.do_cmd("changegroup", roots=n)
170 170 return util.chunkbuffer(zgenerator(f))
171 171
172 172 def changegroupsubset(self, bases, heads, source):
173 173 self.requirecap('changegroupsubset', _('look up remote changes'))
174 174 baselst = " ".join([hex(n) for n in bases])
175 175 headlst = " ".join([hex(n) for n in heads])
176 176 f = self.do_cmd("changegroupsubset", bases=baselst, heads=headlst)
177 177 return util.chunkbuffer(zgenerator(f))
178 178
179 179 def unbundle(self, cg, heads, source):
180 180 # have to stream bundle to a temp file because we do not have
181 181 # http 1.1 chunked transfer.
182 182
183 183 type = ""
184 184 types = self.capable('unbundle')
185 185 # servers older than d1b16a746db6 will send 'unbundle' as a
186 186 # boolean capability
187 187 try:
188 188 types = types.split(',')
189 189 except AttributeError:
190 190 types = [""]
191 191 if types:
192 192 for x in types:
193 193 if x in changegroup.bundletypes:
194 194 type = x
195 195 break
196 196
197 197 tempname = changegroup.writebundle(cg, None, type)
198 198 fp = url.httpsendfile(tempname, "rb")
199 199 try:
200 200 try:
201 201 resp = self.do_read(
202 202 'unbundle', data=fp,
203 203 headers={'Content-Type': 'application/octet-stream'},
204 204 heads=' '.join(map(hex, heads)))
205 205 resp_code, output = resp.split('\n', 1)
206 206 try:
207 207 ret = int(resp_code)
208 208 except ValueError, err:
209 209 raise error.ResponseError(
210 210 _('push failed (unexpected response):'), resp)
211 211 self.ui.write(output)
212 212 return ret
213 213 except socket.error, err:
214 214 if err[0] in (errno.ECONNRESET, errno.EPIPE):
215 215 raise util.Abort(_('push failed: %s') % err[1])
216 216 raise util.Abort(err[1])
217 217 finally:
218 218 fp.close()
219 219 os.unlink(tempname)
220 220
221 221 def stream_out(self):
222 222 return self.do_cmd('stream_out')
223 223
224 224 class httpsrepository(httprepository):
225 225 def __init__(self, ui, path):
226 226 if not url.has_https:
227 227 raise util.Abort(_('Python support for SSL and HTTPS '
228 228 'is not installed'))
229 229 httprepository.__init__(self, ui, path)
230 230
231 231 def instance(ui, path, create):
232 232 if create:
233 233 raise util.Abort(_('cannot create new http repository'))
234 234 try:
235 235 if path.startswith('https:'):
236 236 inst = httpsrepository(ui, path)
237 237 else:
238 238 inst = httprepository(ui, path)
239 239 inst.between([(nullid, nullid)])
240 240 return inst
241 241 except error.RepoError:
242 242 ui.note('(falling back to static-http)\n')
243 243 return statichttprepo.instance(ui, "static-" + path, create)
General Comments 0
You need to be logged in to leave comments. Login now