##// END OF EJS Templates
httprepo: ensure Content-Type header exists when pushing data...
Pierre-Yves.David@ens-lyon.org -
r17221:988974c2 default
parent child Browse files
Show More
@@ -1,245 +1,248
1 # httppeer.py - HTTP repository proxy classes for mercurial
1 # httppeer.py - HTTP repository proxy classes for mercurial
2 #
2 #
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
4 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
4 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
5 #
5 #
6 # This software may be used and distributed according to the terms of the
6 # This software may be used and distributed according to the terms of the
7 # GNU General Public License version 2 or any later version.
7 # GNU General Public License version 2 or any later version.
8
8
9 from node import nullid
9 from node import nullid
10 from i18n import _
10 from i18n import _
11 import changegroup, statichttprepo, error, httpconnection, url, util, wireproto
11 import changegroup, statichttprepo, error, httpconnection, url, util, wireproto
12 import os, urllib, urllib2, zlib, httplib
12 import os, urllib, urllib2, zlib, httplib
13 import errno, socket
13 import errno, socket
14
14
15 def zgenerator(f):
15 def zgenerator(f):
16 zd = zlib.decompressobj()
16 zd = zlib.decompressobj()
17 try:
17 try:
18 for chunk in util.filechunkiter(f):
18 for chunk in util.filechunkiter(f):
19 while chunk:
19 while chunk:
20 yield zd.decompress(chunk, 2**18)
20 yield zd.decompress(chunk, 2**18)
21 chunk = zd.unconsumed_tail
21 chunk = zd.unconsumed_tail
22 except httplib.HTTPException:
22 except httplib.HTTPException:
23 raise IOError(None, _('connection ended unexpectedly'))
23 raise IOError(None, _('connection ended unexpectedly'))
24 yield zd.flush()
24 yield zd.flush()
25
25
26 class httppeer(wireproto.wirepeer):
26 class httppeer(wireproto.wirepeer):
27 def __init__(self, ui, path):
27 def __init__(self, ui, path):
28 self.path = path
28 self.path = path
29 self.caps = None
29 self.caps = None
30 self.handler = None
30 self.handler = None
31 self.urlopener = None
31 self.urlopener = None
32 u = util.url(path)
32 u = util.url(path)
33 if u.query or u.fragment:
33 if u.query or u.fragment:
34 raise util.Abort(_('unsupported URL component: "%s"') %
34 raise util.Abort(_('unsupported URL component: "%s"') %
35 (u.query or u.fragment))
35 (u.query or u.fragment))
36
36
37 # urllib cannot handle URLs with embedded user or passwd
37 # urllib cannot handle URLs with embedded user or passwd
38 self._url, authinfo = u.authinfo()
38 self._url, authinfo = u.authinfo()
39
39
40 self.ui = ui
40 self.ui = ui
41 self.ui.debug('using %s\n' % self._url)
41 self.ui.debug('using %s\n' % self._url)
42
42
43 self.urlopener = url.opener(ui, authinfo)
43 self.urlopener = url.opener(ui, authinfo)
44
44
45 def __del__(self):
45 def __del__(self):
46 if self.urlopener:
46 if self.urlopener:
47 for h in self.urlopener.handlers:
47 for h in self.urlopener.handlers:
48 h.close()
48 h.close()
49 getattr(h, "close_all", lambda : None)()
49 getattr(h, "close_all", lambda : None)()
50
50
51 def url(self):
51 def url(self):
52 return self.path
52 return self.path
53
53
54 # look up capabilities only when needed
54 # look up capabilities only when needed
55
55
56 def _fetchcaps(self):
56 def _fetchcaps(self):
57 self.caps = set(self._call('capabilities').split())
57 self.caps = set(self._call('capabilities').split())
58
58
59 def _capabilities(self):
59 def _capabilities(self):
60 if self.caps is None:
60 if self.caps is None:
61 try:
61 try:
62 self._fetchcaps()
62 self._fetchcaps()
63 except error.RepoError:
63 except error.RepoError:
64 self.caps = set()
64 self.caps = set()
65 self.ui.debug('capabilities: %s\n' %
65 self.ui.debug('capabilities: %s\n' %
66 (' '.join(self.caps or ['none'])))
66 (' '.join(self.caps or ['none'])))
67 return self.caps
67 return self.caps
68
68
69 def lock(self):
69 def lock(self):
70 raise util.Abort(_('operation not supported over http'))
70 raise util.Abort(_('operation not supported over http'))
71
71
72 def _callstream(self, cmd, **args):
72 def _callstream(self, cmd, **args):
73 if cmd == 'pushkey':
73 if cmd == 'pushkey':
74 args['data'] = ''
74 args['data'] = ''
75 data = args.pop('data', None)
75 data = args.pop('data', None)
76 size = 0
76 size = 0
77 if util.safehasattr(data, 'length'):
77 if util.safehasattr(data, 'length'):
78 size = data.length
78 size = data.length
79 elif data is not None:
79 elif data is not None:
80 size = len(data)
80 size = len(data)
81 headers = args.pop('headers', {})
81 headers = args.pop('headers', {})
82 if data is not None and 'Content-Type' not in headers:
83 headers['Content-Type'] = 'application/mercurial-0.1'
84
82
85
83 if size and self.ui.configbool('ui', 'usehttp2', False):
86 if size and self.ui.configbool('ui', 'usehttp2', False):
84 headers['Expect'] = '100-Continue'
87 headers['Expect'] = '100-Continue'
85 headers['X-HgHttp2'] = '1'
88 headers['X-HgHttp2'] = '1'
86
89
87 self.ui.debug("sending %s command\n" % cmd)
90 self.ui.debug("sending %s command\n" % cmd)
88 q = [('cmd', cmd)]
91 q = [('cmd', cmd)]
89 headersize = 0
92 headersize = 0
90 if len(args) > 0:
93 if len(args) > 0:
91 httpheader = self.capable('httpheader')
94 httpheader = self.capable('httpheader')
92 if httpheader:
95 if httpheader:
93 headersize = int(httpheader.split(',')[0])
96 headersize = int(httpheader.split(',')[0])
94 if headersize > 0:
97 if headersize > 0:
95 # The headers can typically carry more data than the URL.
98 # The headers can typically carry more data than the URL.
96 encargs = urllib.urlencode(sorted(args.items()))
99 encargs = urllib.urlencode(sorted(args.items()))
97 headerfmt = 'X-HgArg-%s'
100 headerfmt = 'X-HgArg-%s'
98 contentlen = headersize - len(headerfmt % '000' + ': \r\n')
101 contentlen = headersize - len(headerfmt % '000' + ': \r\n')
99 headernum = 0
102 headernum = 0
100 for i in xrange(0, len(encargs), contentlen):
103 for i in xrange(0, len(encargs), contentlen):
101 headernum += 1
104 headernum += 1
102 header = headerfmt % str(headernum)
105 header = headerfmt % str(headernum)
103 headers[header] = encargs[i:i + contentlen]
106 headers[header] = encargs[i:i + contentlen]
104 varyheaders = [headerfmt % str(h) for h in range(1, headernum + 1)]
107 varyheaders = [headerfmt % str(h) for h in range(1, headernum + 1)]
105 headers['Vary'] = ','.join(varyheaders)
108 headers['Vary'] = ','.join(varyheaders)
106 else:
109 else:
107 q += sorted(args.items())
110 q += sorted(args.items())
108 qs = '?%s' % urllib.urlencode(q)
111 qs = '?%s' % urllib.urlencode(q)
109 cu = "%s%s" % (self._url, qs)
112 cu = "%s%s" % (self._url, qs)
110 req = urllib2.Request(cu, data, headers)
113 req = urllib2.Request(cu, data, headers)
111 if data is not None:
114 if data is not None:
112 self.ui.debug("sending %s bytes\n" % size)
115 self.ui.debug("sending %s bytes\n" % size)
113 req.add_unredirected_header('Content-Length', '%d' % size)
116 req.add_unredirected_header('Content-Length', '%d' % size)
114 try:
117 try:
115 resp = self.urlopener.open(req)
118 resp = self.urlopener.open(req)
116 except urllib2.HTTPError, inst:
119 except urllib2.HTTPError, inst:
117 if inst.code == 401:
120 if inst.code == 401:
118 raise util.Abort(_('authorization failed'))
121 raise util.Abort(_('authorization failed'))
119 raise
122 raise
120 except httplib.HTTPException, inst:
123 except httplib.HTTPException, inst:
121 self.ui.debug('http error while sending %s command\n' % cmd)
124 self.ui.debug('http error while sending %s command\n' % cmd)
122 self.ui.traceback()
125 self.ui.traceback()
123 raise IOError(None, inst)
126 raise IOError(None, inst)
124 except IndexError:
127 except IndexError:
125 # this only happens with Python 2.3, later versions raise URLError
128 # this only happens with Python 2.3, later versions raise URLError
126 raise util.Abort(_('http error, possibly caused by proxy setting'))
129 raise util.Abort(_('http error, possibly caused by proxy setting'))
127 # record the url we got redirected to
130 # record the url we got redirected to
128 resp_url = resp.geturl()
131 resp_url = resp.geturl()
129 if resp_url.endswith(qs):
132 if resp_url.endswith(qs):
130 resp_url = resp_url[:-len(qs)]
133 resp_url = resp_url[:-len(qs)]
131 if self._url.rstrip('/') != resp_url.rstrip('/'):
134 if self._url.rstrip('/') != resp_url.rstrip('/'):
132 if not self.ui.quiet:
135 if not self.ui.quiet:
133 self.ui.warn(_('real URL is %s\n') % resp_url)
136 self.ui.warn(_('real URL is %s\n') % resp_url)
134 self._url = resp_url
137 self._url = resp_url
135 try:
138 try:
136 proto = resp.getheader('content-type')
139 proto = resp.getheader('content-type')
137 except AttributeError:
140 except AttributeError:
138 proto = resp.headers.get('content-type', '')
141 proto = resp.headers.get('content-type', '')
139
142
140 safeurl = util.hidepassword(self._url)
143 safeurl = util.hidepassword(self._url)
141 if proto.startswith('application/hg-error'):
144 if proto.startswith('application/hg-error'):
142 raise error.OutOfBandError(resp.read())
145 raise error.OutOfBandError(resp.read())
143 # accept old "text/plain" and "application/hg-changegroup" for now
146 # accept old "text/plain" and "application/hg-changegroup" for now
144 if not (proto.startswith('application/mercurial-') or
147 if not (proto.startswith('application/mercurial-') or
145 proto.startswith('text/plain') or
148 proto.startswith('text/plain') or
146 proto.startswith('application/hg-changegroup')):
149 proto.startswith('application/hg-changegroup')):
147 self.ui.debug("requested URL: '%s'\n" % util.hidepassword(cu))
150 self.ui.debug("requested URL: '%s'\n" % util.hidepassword(cu))
148 raise error.RepoError(
151 raise error.RepoError(
149 _("'%s' does not appear to be an hg repository:\n"
152 _("'%s' does not appear to be an hg repository:\n"
150 "---%%<--- (%s)\n%s\n---%%<---\n")
153 "---%%<--- (%s)\n%s\n---%%<---\n")
151 % (safeurl, proto or 'no content-type', resp.read()))
154 % (safeurl, proto or 'no content-type', resp.read()))
152
155
153 if proto.startswith('application/mercurial-'):
156 if proto.startswith('application/mercurial-'):
154 try:
157 try:
155 version = proto.split('-', 1)[1]
158 version = proto.split('-', 1)[1]
156 version_info = tuple([int(n) for n in version.split('.')])
159 version_info = tuple([int(n) for n in version.split('.')])
157 except ValueError:
160 except ValueError:
158 raise error.RepoError(_("'%s' sent a broken Content-Type "
161 raise error.RepoError(_("'%s' sent a broken Content-Type "
159 "header (%s)") % (safeurl, proto))
162 "header (%s)") % (safeurl, proto))
160 if version_info > (0, 1):
163 if version_info > (0, 1):
161 raise error.RepoError(_("'%s' uses newer protocol %s") %
164 raise error.RepoError(_("'%s' uses newer protocol %s") %
162 (safeurl, version))
165 (safeurl, version))
163
166
164 return resp
167 return resp
165
168
166 def _call(self, cmd, **args):
169 def _call(self, cmd, **args):
167 fp = self._callstream(cmd, **args)
170 fp = self._callstream(cmd, **args)
168 try:
171 try:
169 return fp.read()
172 return fp.read()
170 finally:
173 finally:
171 # if using keepalive, allow connection to be reused
174 # if using keepalive, allow connection to be reused
172 fp.close()
175 fp.close()
173
176
174 def _callpush(self, cmd, cg, **args):
177 def _callpush(self, cmd, cg, **args):
175 # have to stream bundle to a temp file because we do not have
178 # have to stream bundle to a temp file because we do not have
176 # http 1.1 chunked transfer.
179 # http 1.1 chunked transfer.
177
180
178 types = self.capable('unbundle')
181 types = self.capable('unbundle')
179 try:
182 try:
180 types = types.split(',')
183 types = types.split(',')
181 except AttributeError:
184 except AttributeError:
182 # servers older than d1b16a746db6 will send 'unbundle' as a
185 # servers older than d1b16a746db6 will send 'unbundle' as a
183 # boolean capability. They only support headerless/uncompressed
186 # boolean capability. They only support headerless/uncompressed
184 # bundles.
187 # bundles.
185 types = [""]
188 types = [""]
186 for x in types:
189 for x in types:
187 if x in changegroup.bundletypes:
190 if x in changegroup.bundletypes:
188 type = x
191 type = x
189 break
192 break
190
193
191 tempname = changegroup.writebundle(cg, None, type)
194 tempname = changegroup.writebundle(cg, None, type)
192 fp = httpconnection.httpsendfile(self.ui, tempname, "rb")
195 fp = httpconnection.httpsendfile(self.ui, tempname, "rb")
193 headers = {'Content-Type': 'application/mercurial-0.1'}
196 headers = {'Content-Type': 'application/mercurial-0.1'}
194
197
195 try:
198 try:
196 try:
199 try:
197 r = self._call(cmd, data=fp, headers=headers, **args)
200 r = self._call(cmd, data=fp, headers=headers, **args)
198 vals = r.split('\n', 1)
201 vals = r.split('\n', 1)
199 if len(vals) < 2:
202 if len(vals) < 2:
200 raise error.ResponseError(_("unexpected response:"), r)
203 raise error.ResponseError(_("unexpected response:"), r)
201 return vals
204 return vals
202 except socket.error, err:
205 except socket.error, err:
203 if err.args[0] in (errno.ECONNRESET, errno.EPIPE):
206 if err.args[0] in (errno.ECONNRESET, errno.EPIPE):
204 raise util.Abort(_('push failed: %s') % err.args[1])
207 raise util.Abort(_('push failed: %s') % err.args[1])
205 raise util.Abort(err.args[1])
208 raise util.Abort(err.args[1])
206 finally:
209 finally:
207 fp.close()
210 fp.close()
208 os.unlink(tempname)
211 os.unlink(tempname)
209
212
210 def _abort(self, exception):
213 def _abort(self, exception):
211 raise exception
214 raise exception
212
215
213 def _decompress(self, stream):
216 def _decompress(self, stream):
214 return util.chunkbuffer(zgenerator(stream))
217 return util.chunkbuffer(zgenerator(stream))
215
218
216 class httpspeer(httppeer):
219 class httpspeer(httppeer):
217 def __init__(self, ui, path):
220 def __init__(self, ui, path):
218 if not url.has_https:
221 if not url.has_https:
219 raise util.Abort(_('Python support for SSL and HTTPS '
222 raise util.Abort(_('Python support for SSL and HTTPS '
220 'is not installed'))
223 'is not installed'))
221 httppeer.__init__(self, ui, path)
224 httppeer.__init__(self, ui, path)
222
225
223 def instance(ui, path, create):
226 def instance(ui, path, create):
224 if create:
227 if create:
225 raise util.Abort(_('cannot create new http repository'))
228 raise util.Abort(_('cannot create new http repository'))
226 try:
229 try:
227 if path.startswith('https:'):
230 if path.startswith('https:'):
228 inst = httpspeer(ui, path)
231 inst = httpspeer(ui, path)
229 else:
232 else:
230 inst = httppeer(ui, path)
233 inst = httppeer(ui, path)
231 try:
234 try:
232 # Try to do useful work when checking compatibility.
235 # Try to do useful work when checking compatibility.
233 # Usually saves a roundtrip since we want the caps anyway.
236 # Usually saves a roundtrip since we want the caps anyway.
234 inst._fetchcaps()
237 inst._fetchcaps()
235 except error.RepoError:
238 except error.RepoError:
236 # No luck, try older compatibility check.
239 # No luck, try older compatibility check.
237 inst.between([(nullid, nullid)])
240 inst.between([(nullid, nullid)])
238 return inst
241 return inst
239 except error.RepoError, httpexception:
242 except error.RepoError, httpexception:
240 try:
243 try:
241 r = statichttprepo.instance(ui, "static-" + path, create)
244 r = statichttprepo.instance(ui, "static-" + path, create)
242 ui.note('(falling back to static-http)\n')
245 ui.note('(falling back to static-http)\n')
243 return r
246 return r
244 except error.RepoError:
247 except error.RepoError:
245 raise httpexception # use the original http RepoError instead
248 raise httpexception # use the original http RepoError instead
General Comments 0
You need to be logged in to leave comments. Login now