Show More
@@ -1,78 +1,68 | |||
|
1 | 1 | # |
|
2 | 2 | # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> |
|
3 | 3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | import cStringIO, zlib, sys, urllib |
|
9 | 9 | from mercurial import util, wireproto |
|
10 | 10 | from common import HTTP_OK |
|
11 | 11 | |
|
12 | 12 | HGTYPE = 'application/mercurial-0.1' |
|
13 | 13 | |
|
14 | 14 | class webproto(object): |
|
15 | 15 | def __init__(self, req): |
|
16 | 16 | self.req = req |
|
17 | 17 | self.response = '' |
|
18 | 18 | def getargs(self, args): |
|
19 | 19 | data = {} |
|
20 | 20 | keys = args.split() |
|
21 | 21 | for k in keys: |
|
22 | 22 | if k == '*': |
|
23 | 23 | star = {} |
|
24 | 24 | for key in self.req.form.keys(): |
|
25 | 25 | if key not in keys: |
|
26 | 26 | star[key] = self.req.form[key][0] |
|
27 | 27 | data['*'] = star |
|
28 | 28 | else: |
|
29 | 29 | data[k] = self.req.form[k][0] |
|
30 | 30 | return [data[k] for k in keys] |
|
31 | 31 | def getfile(self, fp): |
|
32 | 32 | length = int(self.req.env['CONTENT_LENGTH']) |
|
33 | 33 | for s in util.filechunkiter(self.req, limit=length): |
|
34 | 34 | fp.write(s) |
|
35 | 35 | def redirect(self): |
|
36 | 36 | self.oldio = sys.stdout, sys.stderr |
|
37 | 37 | sys.stderr = sys.stdout = cStringIO.StringIO() |
|
38 | 38 | def groupchunks(self, cg): |
|
39 | 39 | z = zlib.compressobj() |
|
40 | 40 | while 1: |
|
41 | 41 | chunk = cg.read(4096) |
|
42 | 42 | if not chunk: |
|
43 | 43 | break |
|
44 | 44 | yield z.compress(chunk) |
|
45 | 45 | yield z.flush() |
|
46 | def sendresponse(self, s): | |
|
47 | self.req.respond(HTTP_OK, HGTYPE, length=len(s)) | |
|
48 | self.response = s | |
|
49 | def sendstream(self, source): | |
|
50 | self.req.respond(HTTP_OK, HGTYPE) | |
|
51 | for chunk in source.gen: | |
|
52 | self.req.write(chunk) | |
|
53 | def sendpushresponse(self, rsp): | |
|
54 | val = sys.stdout.getvalue() | |
|
55 | sys.stdout, sys.stderr = self.oldio | |
|
56 | self.req.respond(HTTP_OK, HGTYPE) | |
|
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 | ||
|
65 | 46 | def _client(self): |
|
66 | 47 | return 'remote:%s:%s:%s' % ( |
|
67 | 48 | self.req.env.get('wsgi.url_scheme') or 'http', |
|
68 | 49 | urllib.quote(self.req.env.get('REMOTE_HOST', '')), |
|
69 | 50 | urllib.quote(self.req.env.get('REMOTE_USER', ''))) |
|
70 | 51 | |
|
71 | 52 | def iscmd(cmd): |
|
72 | 53 | return cmd in wireproto.commands |
|
73 | 54 | |
|
74 | 55 | def call(repo, req, cmd): |
|
75 | 56 | p = webproto(req) |
|
76 | 57 | rsp = wireproto.dispatch(repo, p, cmd) |
|
77 | webproto.handlers[rsp.__class__](p, rsp) | |
|
78 | return [p.response] | |
|
58 | if isinstance(rsp, str): | |
|
59 | req.respond(HTTP_OK, HGTYPE, length=len(rsp)) | |
|
60 | return [rsp] | |
|
61 | elif isinstance(rsp, wireproto.streamres): | |
|
62 | req.respond(HTTP_OK, HGTYPE) | |
|
63 | return rsp.gen | |
|
64 | elif isinstance(rsp, wireproto.pushres): | |
|
65 | val = sys.stdout.getvalue() | |
|
66 | sys.stdout, sys.stderr = p.oldio | |
|
67 | req.respond(HTTP_OK, HGTYPE) | |
|
68 | return ['%d\n%s' % (rsp.res, val)] |
General Comments 0
You need to be logged in to leave comments.
Login now