httppeer: detect redirect to URL without query string (issue5860)
197d10e157ce subtly changed the HTTP peer's handling of HTTP redirects.
Before that changeset, we instantiated an HTTP peer instance and
performed the capabilities lookup with that instance. The old code had
the following relevant properties:
1) The HTTP request layer would automatically follow HTTP redirects.
2) An encountered HTTP redirect would update a peer instance variable
pointing to the repo URL.
3) The peer would automagically perform a "capabilities" command
request if a caller requested capabilities but capabilities were
not yet defined.
The first HTTP request issued by a peer is for ?cmd=capabilities. If
the server responds with an HTTP redirect to a ?cmd=capabilities URL,
the HTTP request layer automatically followed it, retrieved a valid
capabilities response, and the peer's base URL was updated
automatically so subsequent requests used the proper URL. In other
words, things "just worked."
In the case where the server redirected to a URL without the
?cmd=capabilities query string, the HTTP request layer would follow
the redirect and likely encounter HTML. The peer's base URL would be
updated and the unexpected Content-Type would raise a RepoError. We
would catch RepoError and immediately call between() (testing the case
for pre 0.9.1 servers not supporting the "capabilities" command). e.g.
try:
inst._fetchcaps()
except error.RepoError:
inst.between([(nullid, nullid)])
between() would eventually call into _callstream(). And _callstream()
made a call to self.capable('httpheader'). capable() would call
self.capabilities(), which would see that no capabilities were set
(because HTML was returned for that request) and call the "capabilities"
command to fetch capabilities. Because the base URL had been updated
from the redirect, this 2nd "capabilities" command would succeed and
the client would immediately call "between," which would also succeed.
The legacy handshake succeeded. Only because "capabilities" was
successfully executed as a side effect did the peer recognize that it
was talking to a modern server. In other words, this all appeared to
work accidentally.
After 197d10e157ce, we stopped calling the "capabilities" command on
the peer instance. Instead, we made the request via a low-level opener,
detected the redirect as part of response handling code, and passed the
redirected URL into the constructed peer instance.
For cases where the redirected URL included the query string, this
"just worked." But for cases where the redirected URL stripped the query
string, we threw RepoError and because we removed the "between" handshake
fallback, we fell through to the "is a static HTTP repo" check and
performed an HTTP request for .hg/requires.
While 197d10e157ce was marked as backwards incompatible, the only
intended backwards incompatible behavior was not performing the
"between" fallback. It was not realized that the "between" command
had the side-effect of recovering from an errant redirect that
dropped the query string.
This commit restores the previous behavior and allows clients to
handle a redirect that drops the query string. In the case where
the request is redirected and the query string is dropped, we raise
a special case of RepoError. We then catch this special exception in
the handshake code and perform another "capabilities" request against
the redirected URL. If that works, all is well. Otherwise, we fall back
to the "is a static HTTP repo" check.
The new code is arguably better than before 197d10e157ce, as it is
explicit about the expected behavior and we avoid performing a
"between" request, saving a server round trip.
Differential Revision: https://phab.mercurial-scm.org/D3433
# cvs.py: CVS conversion code inspired by hg-cvs-import and git-cvsimport
#
# Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
from __future__ import absolute_import
import errno
import os
import re
import socket
from mercurial.i18n import _
from mercurial import (
encoding,
error,
pycompat,
util,
)
from mercurial.utils import (
dateutil,
procutil,
)
from . import (
common,
cvsps,
)
stringio = util.stringio
checktool = common.checktool
commit = common.commit
converter_source = common.converter_source
makedatetimestamp = common.makedatetimestamp
NoRepo = common.NoRepo
class convert_cvs(converter_source):
def __init__(self, ui, repotype, path, revs=None):
super(convert_cvs, self).__init__(ui, repotype, path, revs=revs)
cvs = os.path.join(path, "CVS")
if not os.path.exists(cvs):
raise NoRepo(_("%s does not look like a CVS checkout") % path)
checktool('cvs')
self.changeset = None
self.files = {}
self.tags = {}
self.lastbranch = {}
self.socket = None
self.cvsroot = open(os.path.join(cvs, "Root"), 'rb').read()[:-1]
self.cvsrepo = open(os.path.join(cvs, "Repository"), 'rb').read()[:-1]
self.encoding = encoding.encoding
self._connect()
def _parse(self):
if self.changeset is not None:
return
self.changeset = {}
maxrev = 0
if self.revs:
if len(self.revs) > 1:
raise error.Abort(_('cvs source does not support specifying '
'multiple revs'))
# TODO: handle tags
try:
# patchset number?
maxrev = int(self.revs[0])
except ValueError:
raise error.Abort(_('revision %s is not a patchset number')
% self.revs[0])
d = pycompat.getcwd()
try:
os.chdir(self.path)
id = None
cache = 'update'
if not self.ui.configbool('convert', 'cvsps.cache'):
cache = None
db = cvsps.createlog(self.ui, cache=cache)
db = cvsps.createchangeset(self.ui, db,
fuzz=int(self.ui.config('convert', 'cvsps.fuzz')),
mergeto=self.ui.config('convert', 'cvsps.mergeto'),
mergefrom=self.ui.config('convert', 'cvsps.mergefrom'))
for cs in db:
if maxrev and cs.id > maxrev:
break
id = (b"%d" % cs.id)
cs.author = self.recode(cs.author)
self.lastbranch[cs.branch] = id
cs.comment = self.recode(cs.comment)
if self.ui.configbool('convert', 'localtimezone'):
cs.date = makedatetimestamp(cs.date[0])
date = dateutil.datestr(cs.date, '%Y-%m-%d %H:%M:%S %1%2')
self.tags.update(dict.fromkeys(cs.tags, id))
files = {}
for f in cs.entries:
files[f.file] = "%s%s" % ('.'.join([(b"%d" % x)
for x in f.revision]),
['', '(DEAD)'][f.dead])
# add current commit to set
c = commit(author=cs.author, date=date,
parents=[(b"%d" % p.id) for p in cs.parents],
desc=cs.comment, branch=cs.branch or '')
self.changeset[id] = c
self.files[id] = files
self.heads = self.lastbranch.values()
finally:
os.chdir(d)
def _connect(self):
root = self.cvsroot
conntype = None
user, host = None, None
cmd = ['cvs', 'server']
self.ui.status(_("connecting to %s\n") % root)
if root.startswith(":pserver:"):
root = root[9:]
m = re.match(r'(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?(.*)',
root)
if m:
conntype = "pserver"
user, passw, serv, port, root = m.groups()
if not user:
user = "anonymous"
if not port:
port = 2401
else:
port = int(port)
format0 = ":pserver:%s@%s:%s" % (user, serv, root)
format1 = ":pserver:%s@%s:%d%s" % (user, serv, port, root)
if not passw:
passw = "A"
cvspass = os.path.expanduser("~/.cvspass")
try:
pf = open(cvspass, 'rb')
for line in pf.read().splitlines():
part1, part2 = line.split(' ', 1)
# /1 :pserver:user@example.com:2401/cvsroot/foo
# Ah<Z
if part1 == '/1':
part1, part2 = part2.split(' ', 1)
format = format1
# :pserver:user@example.com:/cvsroot/foo Ah<Z
else:
format = format0
if part1 == format:
passw = part2
break
pf.close()
except IOError as inst:
if inst.errno != errno.ENOENT:
if not getattr(inst, 'filename', None):
inst.filename = cvspass
raise
sck = socket.socket()
sck.connect((serv, port))
sck.send("\n".join(["BEGIN AUTH REQUEST", root, user, passw,
"END AUTH REQUEST", ""]))
if sck.recv(128) != "I LOVE YOU\n":
raise error.Abort(_("CVS pserver authentication failed"))
self.writep = self.readp = sck.makefile('r+')
if not conntype and root.startswith(":local:"):
conntype = "local"
root = root[7:]
if not conntype:
# :ext:user@host/home/user/path/to/cvsroot
if root.startswith(":ext:"):
root = root[5:]
m = re.match(br'(?:([^@:/]+)@)?([^:/]+):?(.*)', root)
# Do not take Windows path "c:\foo\bar" for a connection strings
if os.path.isdir(root) or not m:
conntype = "local"
else:
conntype = "rsh"
user, host, root = m.group(1), m.group(2), m.group(3)
if conntype != "pserver":
if conntype == "rsh":
rsh = encoding.environ.get("CVS_RSH") or "ssh"
if user:
cmd = [rsh, '-l', user, host] + cmd
else:
cmd = [rsh, host] + cmd
# popen2 does not support argument lists under Windows
cmd = [procutil.shellquote(arg) for arg in cmd]
cmd = procutil.quotecommand(' '.join(cmd))
self.writep, self.readp = procutil.popen2(cmd)
self.realroot = root
self.writep.write("Root %s\n" % root)
self.writep.write("Valid-responses ok error Valid-requests Mode"
" M Mbinary E Checked-in Created Updated"
" Merged Removed\n")
self.writep.write("valid-requests\n")
self.writep.flush()
r = self.readp.readline()
if not r.startswith("Valid-requests"):
raise error.Abort(_('unexpected response from CVS server '
'(expected "Valid-requests", but got %r)')
% r)
if "UseUnchanged" in r:
self.writep.write("UseUnchanged\n")
self.writep.flush()
r = self.readp.readline()
def getheads(self):
self._parse()
return self.heads
def getfile(self, name, rev):
def chunkedread(fp, count):
# file-objects returned by socket.makefile() do not handle
# large read() requests very well.
chunksize = 65536
output = stringio()
while count > 0:
data = fp.read(min(count, chunksize))
if not data:
raise error.Abort(_("%d bytes missing from remote file")
% count)
count -= len(data)
output.write(data)
return output.getvalue()
self._parse()
if rev.endswith("(DEAD)"):
return None, None
args = ("-N -P -kk -r %s --" % rev).split()
args.append(self.cvsrepo + '/' + name)
for x in args:
self.writep.write("Argument %s\n" % x)
self.writep.write("Directory .\n%s\nco\n" % self.realroot)
self.writep.flush()
data = ""
mode = None
while True:
line = self.readp.readline()
if line.startswith("Created ") or line.startswith("Updated "):
self.readp.readline() # path
self.readp.readline() # entries
mode = self.readp.readline()[:-1]
count = int(self.readp.readline()[:-1])
data = chunkedread(self.readp, count)
elif line.startswith(" "):
data += line[1:]
elif line.startswith("M "):
pass
elif line.startswith("Mbinary "):
count = int(self.readp.readline()[:-1])
data = chunkedread(self.readp, count)
else:
if line == "ok\n":
if mode is None:
raise error.Abort(_('malformed response from CVS'))
return (data, "x" in mode and "x" or "")
elif line.startswith("E "):
self.ui.warn(_("cvs server: %s\n") % line[2:])
elif line.startswith("Remove"):
self.readp.readline()
else:
raise error.Abort(_("unknown CVS response: %s") % line)
def getchanges(self, rev, full):
if full:
raise error.Abort(_("convert from cvs does not support --full"))
self._parse()
return sorted(self.files[rev].iteritems()), {}, set()
def getcommit(self, rev):
self._parse()
return self.changeset[rev]
def gettags(self):
self._parse()
return self.tags
def getchangedfiles(self, rev, i):
self._parse()
return sorted(self.files[rev])