plan9fox/sys/lib/python/hgext/hgwebfs.py

79 lines
1.5 KiB
Python
Raw Normal View History

2012-03-16 16:13:35 +00:00
''' webfs support '''
import mercurial.url
import re
class Webconn:
def __init__(self, mnt, req):
if type(req) == str:
self.url = req
else:
self.url = req.get_full_url()
if self.url[0:5] == 'file:':
self.dir = '/dev/null'
self.body = open(self.url[5:], 'r', 0)
return
2012-03-16 16:13:35 +00:00
ctl = open(mnt+'/clone', 'r+', 0)
try:
self.dir = mnt+'/'+ctl.readline().rstrip('\n')
ctl.seek(0)
ctl.write('url '+self.url)
2012-03-16 16:47:36 +00:00
m = 'User-Agent: mercurial/proto-1.0\r\n';
2012-03-16 16:18:44 +00:00
for h in req.headers:
m += h+': '+req.headers[h]+'\r\n'
2012-03-16 16:47:36 +00:00
ctl.seek(0)
ctl.write('headers '+m)
2012-03-16 16:13:35 +00:00
if req.has_data():
data = req.get_data()
2012-03-16 16:47:36 +00:00
post = open(self.dir+'/postbody', 'w', 0);
2012-03-16 16:13:35 +00:00
try:
while True:
buf = data.read(4096)
if len(buf) == 0:
break
post.write(buf)
finally:
post.close()
self.body = open(self.dir+'/body', 'r', 0)
finally:
ctl.close()
def read(self, amt=4096):
return self.body.read(amt);
def close(self):
self.body.close()
self.body = None
self.dir = None
def geturl(self):
return self.url
def getheader(self, key):
name = re.sub(r'[^a-z]+', '', key.lower())
try:
f = open(self.dir+'/'+name, 'r', 0)
try:
hdr = f.read()
finally:
f.close()
return hdr
except:
return None
class Webopener:
def __init__(self):
self.handlers = []
def open(self, req, data=None):
return Webconn('/mnt/web', req)
def close(self):
pass
def webopener(ui, authinfo=None):
return Webopener();
mercurial.url.opener = webopener