##// END OF EJS Templates
protocol: wrap non-string protocol responses in classes
Dirkjan Ochtman -
r11625:cdeb8613 default
parent child Browse files
Show More
@@ -48,13 +48,20 b' class webproto(object):'
48 self.response = s
48 self.response = s
49 def sendstream(self, source):
49 def sendstream(self, source):
50 self.req.respond(HTTP_OK, HGTYPE)
50 self.req.respond(HTTP_OK, HGTYPE)
51 for chunk in source:
51 for chunk in source.gen:
52 self.req.write(str(chunk))
52 self.req.write(chunk)
53 def sendpushresponse(self, ret):
53 def sendpushresponse(self, rsp):
54 val = sys.stdout.getvalue()
54 val = sys.stdout.getvalue()
55 sys.stdout, sys.stderr = self.oldio
55 sys.stdout, sys.stderr = self.oldio
56 self.req.respond(HTTP_OK, HGTYPE)
56 self.req.respond(HTTP_OK, HGTYPE)
57 self.response = '%d\n%s' % (ret, val)
57 self.response = '%d\n%s' % (rsp.res, val)
58
59 handlers = {
60 str: sendresponse,
61 wireproto.streamres: sendstream,
62 wireproto.pushres: sendpushresponse,
63 }
64
58 def _client(self):
65 def _client(self):
59 return 'remote:%s:%s:%s' % (
66 return 'remote:%s:%s:%s' % (
60 self.req.env.get('wsgi.url_scheme') or 'http',
67 self.req.env.get('wsgi.url_scheme') or 'http',
@@ -66,5 +73,6 b' def iscmd(cmd):'
66
73
67 def call(repo, req, cmd):
74 def call(repo, req, cmd):
68 p = webproto(req)
75 p = webproto(req)
69 wireproto.dispatch(repo, p, cmd)
76 rsp = wireproto.dispatch(repo, p, cmd)
70 yield p.response
77 webproto.handlers[rsp.__class__](p, rsp)
78 return [p.response]
@@ -72,13 +72,13 b' class sshserver(object):'
72 self.fout.flush()
72 self.fout.flush()
73
73
74 def sendstream(self, source):
74 def sendstream(self, source):
75 for chunk in source:
75 for chunk in source.gen:
76 self.fout.write(chunk)
76 self.fout.write(chunk)
77 self.fout.flush()
77 self.fout.flush()
78
78
79 def sendpushresponse(self, ret):
79 def sendpushresponse(self, rsp):
80 self.sendresponse('')
80 self.sendresponse('')
81 self.sendresponse(str(ret))
81 self.sendresponse(str(rsp.res))
82
82
83 def serve_forever(self):
83 def serve_forever(self):
84 try:
84 try:
@@ -89,10 +89,17 b' class sshserver(object):'
89 self.lock.release()
89 self.lock.release()
90 sys.exit(0)
90 sys.exit(0)
91
91
92 handlers = {
93 str: sendresponse,
94 wireproto.streamres: sendstream,
95 wireproto.pushres: sendpushresponse,
96 }
97
92 def serve_one(self):
98 def serve_one(self):
93 cmd = self.fin.readline()[:-1]
99 cmd = self.fin.readline()[:-1]
94 if cmd and cmd in wireproto.commands:
100 if cmd and cmd in wireproto.commands:
95 wireproto.dispatch(self.repo, self, cmd)
101 rsp = wireproto.dispatch(self.repo, self, cmd)
102 self.handlers[rsp.__class__](self, rsp)
96 elif cmd:
103 elif cmd:
97 impl = getattr(self, 'do_' + cmd, None)
104 impl = getattr(self, 'do_' + cmd, None)
98 if impl:
105 if impl:
@@ -133,12 +133,18 b' class wirerepository(repo.repository):'
133
133
134 # server side
134 # server side
135
135
136 class streamres(object):
137 def __init__(self, gen):
138 self.gen = gen
139
140 class pushres(object):
141 def __init__(self, res):
142 self.res = res
143
136 def dispatch(repo, proto, command):
144 def dispatch(repo, proto, command):
137 func, spec = commands[command]
145 func, spec = commands[command]
138 args = proto.getargs(spec)
146 args = proto.getargs(spec)
139 r = func(repo, proto, *args)
147 return func(repo, proto, *args)
140 if r != None:
141 proto.sendresponse(r)
142
148
143 def between(repo, proto, pairs):
149 def between(repo, proto, pairs):
144 pairs = [decodelist(p, '-') for p in pairs.split(" ")]
150 pairs = [decodelist(p, '-') for p in pairs.split(" ")]
@@ -173,13 +179,13 b' def capabilities(repo, proto):'
173 def changegroup(repo, proto, roots):
179 def changegroup(repo, proto, roots):
174 nodes = decodelist(roots)
180 nodes = decodelist(roots)
175 cg = repo.changegroup(nodes, 'serve')
181 cg = repo.changegroup(nodes, 'serve')
176 proto.sendstream(proto.groupchunks(cg))
182 return streamres(proto.groupchunks(cg))
177
183
178 def changegroupsubset(repo, proto, bases, heads):
184 def changegroupsubset(repo, proto, bases, heads):
179 bases = decodelist(bases)
185 bases = decodelist(bases)
180 heads = decodelist(heads)
186 heads = decodelist(heads)
181 cg = repo.changegroupsubset(bases, heads, 'serve')
187 cg = repo.changegroupsubset(bases, heads, 'serve')
182 proto.sendstream(proto.groupchunks(cg))
188 return streamres(proto.groupchunks(cg))
183
189
184 def heads(repo, proto):
190 def heads(repo, proto):
185 h = repo.heads()
191 h = repo.heads()
@@ -215,7 +221,7 b' def pushkey(repo, proto, namespace, key,'
215 return '%s\n' % int(r)
221 return '%s\n' % int(r)
216
222
217 def stream(repo, proto):
223 def stream(repo, proto):
218 proto.sendstream(streamclone.stream_out(repo))
224 return streamres(streamclone.stream_out(repo))
219
225
220 def unbundle(repo, proto, heads):
226 def unbundle(repo, proto, heads):
221 their_heads = decodelist(heads)
227 their_heads = decodelist(heads)
@@ -259,7 +265,7 b' def unbundle(repo, proto, heads):'
259 sys.stderr.write("abort: %s\n" % inst)
265 sys.stderr.write("abort: %s\n" % inst)
260 finally:
266 finally:
261 lock.release()
267 lock.release()
262 proto.sendpushresponse(r)
268 return pushres(r)
263
269
264 finally:
270 finally:
265 fp.close()
271 fp.close()
General Comments 0
You need to be logged in to leave comments. Login now