When pulling from a non hg repository URL (e.g. http://www.kernel.org/hg)

you geta pretty obscure error (zlib: uknown compression type). The
attached patch modifies hgweb.py and hg.py to supply and check a
'Content-type: application/hg-0.1' HTTP header for the branches,
between and changegroup commands, so that we know it's a proper hg
repo before snarfing the input. Comments appreciated!
This commit is contained in:
Muli Ben-Yehuda 2005-07-21 18:18:43 -05:00
parent d1b02c9855
commit 0bf14d0d7e
2 changed files with 22 additions and 9 deletions

View File

@ -1753,10 +1753,20 @@ class httprepository:
self.ui.warn("unexpected response:\n" + d[:400] + "\n...\n") self.ui.warn("unexpected response:\n" + d[:400] + "\n...\n")
raise raise
def verify_hg_repo(self, resp):
if (resp.headers['content-type'] == 'application/hg-0.1'):
pass
else:
msg = """'%s' does not appear to be a valid hg repository -
missing a 'Content-type: application/hg-0.1' HTTP header""" % (self.url,)
raise RepoError(msg)
def branches(self, nodes): def branches(self, nodes):
n = " ".join(map(hex, nodes)) n = " ".join(map(hex, nodes))
d = self.do_cmd("branches", nodes=n).read() resp = self.do_cmd("branches", nodes=n);
self.verify_hg_repo(resp);
try: try:
d = resp.read()
br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ] br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ]
return br return br
except: except:
@ -1765,8 +1775,10 @@ class httprepository:
def between(self, pairs): def between(self, pairs):
n = "\n".join(["-".join(map(hex, p)) for p in pairs]) n = "\n".join(["-".join(map(hex, p)) for p in pairs])
d = self.do_cmd("between", pairs=n).read() resp = self.do_cmd("between", pairs=n)
self.verify_hg_repo(resp)
try: try:
d = resp.read()
p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ] p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ]
return p return p
except: except:
@ -1775,7 +1787,8 @@ class httprepository:
def changegroup(self, nodes): def changegroup(self, nodes):
n = " ".join(map(hex, nodes)) n = " ".join(map(hex, nodes))
f = self.do_cmd("changegroup", roots=n) resp = self.do_cmd("changegroup", roots=n)
self.verify_hg_repo(resp)
bytes = 0 bytes = 0
class zread: class zread:
@ -1785,7 +1798,7 @@ class httprepository:
self.buf = "" self.buf = ""
def read(self, l): def read(self, l):
while l > len(self.buf): while l > len(self.buf):
r = f.read(4096) r = self.f.read(4096)
if r: if r:
self.buf += self.zd.decompress(r) self.buf += self.zd.decompress(r)
else: else:
@ -1794,7 +1807,7 @@ class httprepository:
d, self.buf = self.buf[:l], self.buf[l:] d, self.buf = self.buf[:l], self.buf[l:]
return d return d
return zread(f) return zread(resp)
class remotelock: class remotelock:
def __init__(self, repo): def __init__(self, repo):

View File

@ -657,12 +657,12 @@ class hgweb:
write(self.filelog(args['file'][0], args['filenode'][0])) write(self.filelog(args['file'][0], args['filenode'][0]))
elif args['cmd'][0] == 'heads': elif args['cmd'][0] == 'heads':
httphdr("text/plain") httphdr("application/mercurial-0.1")
h = self.repo.heads() h = self.repo.heads()
sys.stdout.write(" ".join(map(hex, h)) + "\n") sys.stdout.write(" ".join(map(hex, h)) + "\n")
elif args['cmd'][0] == 'branches': elif args['cmd'][0] == 'branches':
httphdr("text/plain") httphdr("application/mercurial-0.1")
nodes = [] nodes = []
if args.has_key('nodes'): if args.has_key('nodes'):
nodes = map(bin, args['nodes'][0].split(" ")) nodes = map(bin, args['nodes'][0].split(" "))
@ -670,7 +670,7 @@ class hgweb:
sys.stdout.write(" ".join(map(hex, b)) + "\n") sys.stdout.write(" ".join(map(hex, b)) + "\n")
elif args['cmd'][0] == 'between': elif args['cmd'][0] == 'between':
httphdr("text/plain") httphdr("application/hg-0.1")
nodes = [] nodes = []
if args.has_key('pairs'): if args.has_key('pairs'):
pairs = [ map(bin, p.split("-")) pairs = [ map(bin, p.split("-"))
@ -679,7 +679,7 @@ class hgweb:
sys.stdout.write(" ".join(map(hex, b)) + "\n") sys.stdout.write(" ".join(map(hex, b)) + "\n")
elif args['cmd'][0] == 'changegroup': elif args['cmd'][0] == 'changegroup':
httphdr("application/hg-changegroup") httphdr("application/mercurial-0.1")
nodes = [] nodes = []
if self.viewonly: if self.viewonly:
return return