Show More
@@ -1,203 +1,203 b'' | |||||
1 | # |
|
1 | # | |
2 | # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> |
|
2 | # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> | |
3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
8 | from __future__ import absolute_import |
|
8 | from __future__ import absolute_import | |
9 |
|
9 | |||
10 | import cgi |
|
10 | import cgi | |
11 | import struct |
|
11 | import struct | |
12 |
|
12 | |||
13 | from .common import ( |
|
13 | from .common import ( | |
14 | HTTP_OK, |
|
14 | HTTP_OK, | |
15 | ) |
|
15 | ) | |
16 |
|
16 | |||
17 | from .. import ( |
|
17 | from .. import ( | |
18 | error, |
|
18 | error, | |
19 | util, |
|
19 | util, | |
20 | wireproto, |
|
20 | wireproto, | |
21 | ) |
|
21 | ) | |
22 | stringio = util.stringio |
|
22 | stringio = util.stringio | |
23 |
|
23 | |||
24 | urlerr = util.urlerr |
|
24 | urlerr = util.urlerr | |
25 | urlreq = util.urlreq |
|
25 | urlreq = util.urlreq | |
26 |
|
26 | |||
27 | HGTYPE = 'application/mercurial-0.1' |
|
27 | HGTYPE = 'application/mercurial-0.1' | |
28 | HGTYPE2 = 'application/mercurial-0.2' |
|
28 | HGTYPE2 = 'application/mercurial-0.2' | |
29 | HGERRTYPE = 'application/hg-error' |
|
29 | HGERRTYPE = 'application/hg-error' | |
30 |
|
30 | |||
31 | def decodevaluefromheaders(req, headerprefix): |
|
31 | def decodevaluefromheaders(req, headerprefix): | |
32 | """Decode a long value from multiple HTTP request headers.""" |
|
32 | """Decode a long value from multiple HTTP request headers.""" | |
33 | chunks = [] |
|
33 | chunks = [] | |
34 | i = 1 |
|
34 | i = 1 | |
35 | while True: |
|
35 | while True: | |
36 | v = req.env.get('HTTP_%s_%d' % ( |
|
36 | v = req.env.get('HTTP_%s_%d' % ( | |
37 | headerprefix.upper().replace('-', '_'), i)) |
|
37 | headerprefix.upper().replace('-', '_'), i)) | |
38 | if v is None: |
|
38 | if v is None: | |
39 | break |
|
39 | break | |
40 | chunks.append(v) |
|
40 | chunks.append(v) | |
41 | i += 1 |
|
41 | i += 1 | |
42 |
|
42 | |||
43 | return ''.join(chunks) |
|
43 | return ''.join(chunks) | |
44 |
|
44 | |||
45 | class webproto(wireproto.abstractserverproto): |
|
45 | class webproto(wireproto.abstractserverproto): | |
46 | def __init__(self, req, ui): |
|
46 | def __init__(self, req, ui): | |
47 | self.req = req |
|
47 | self.req = req | |
48 | self.response = '' |
|
48 | self.response = '' | |
49 | self.ui = ui |
|
49 | self.ui = ui | |
50 | self.name = 'http' |
|
50 | self.name = 'http' | |
51 |
|
51 | |||
52 | def getargs(self, args): |
|
52 | def getargs(self, args): | |
53 | knownargs = self._args() |
|
53 | knownargs = self._args() | |
54 | data = {} |
|
54 | data = {} | |
55 | keys = args.split() |
|
55 | keys = args.split() | |
56 | for k in keys: |
|
56 | for k in keys: | |
57 | if k == '*': |
|
57 | if k == '*': | |
58 | star = {} |
|
58 | star = {} | |
59 | for key in knownargs.keys(): |
|
59 | for key in knownargs.keys(): | |
60 | if key != 'cmd' and key not in keys: |
|
60 | if key != 'cmd' and key not in keys: | |
61 | star[key] = knownargs[key][0] |
|
61 | star[key] = knownargs[key][0] | |
62 | data['*'] = star |
|
62 | data['*'] = star | |
63 | else: |
|
63 | else: | |
64 | data[k] = knownargs[k][0] |
|
64 | data[k] = knownargs[k][0] | |
65 | return [data[k] for k in keys] |
|
65 | return [data[k] for k in keys] | |
66 | def _args(self): |
|
66 | def _args(self): | |
67 | args = self.req.form.copy() |
|
67 | args = self.req.form.copy() | |
68 | postlen = int(self.req.env.get('HTTP_X_HGARGS_POST', 0)) |
|
68 | postlen = int(self.req.env.get('HTTP_X_HGARGS_POST', 0)) | |
69 | if postlen: |
|
69 | if postlen: | |
70 | args.update(cgi.parse_qs( |
|
70 | args.update(cgi.parse_qs( | |
71 | self.req.read(postlen), keep_blank_values=True)) |
|
71 | self.req.read(postlen), keep_blank_values=True)) | |
72 | return args |
|
72 | return args | |
73 |
|
73 | |||
74 | argvalue = decodevaluefromheaders(self.req, 'X-HgArg') |
|
74 | argvalue = decodevaluefromheaders(self.req, 'X-HgArg') | |
75 | args.update(cgi.parse_qs(argvalue, keep_blank_values=True)) |
|
75 | args.update(cgi.parse_qs(argvalue, keep_blank_values=True)) | |
76 | return args |
|
76 | return args | |
77 | def getfile(self, fp): |
|
77 | def getfile(self, fp): | |
78 | length = int(self.req.env['CONTENT_LENGTH']) |
|
78 | length = int(self.req.env['CONTENT_LENGTH']) | |
79 | # If httppostargs is used, we need to read Content-Length |
|
79 | # If httppostargs is used, we need to read Content-Length | |
80 | # minus the amount that was consumed by args. |
|
80 | # minus the amount that was consumed by args. | |
81 | length -= int(self.req.env.get('HTTP_X_HGARGS_POST', 0)) |
|
81 | length -= int(self.req.env.get('HTTP_X_HGARGS_POST', 0)) | |
82 | for s in util.filechunkiter(self.req, limit=length): |
|
82 | for s in util.filechunkiter(self.req, limit=length): | |
83 | fp.write(s) |
|
83 | fp.write(s) | |
84 | def redirect(self): |
|
84 | def redirect(self): | |
85 | self.oldio = self.ui.fout, self.ui.ferr |
|
85 | self.oldio = self.ui.fout, self.ui.ferr | |
86 | self.ui.ferr = self.ui.fout = stringio() |
|
86 | self.ui.ferr = self.ui.fout = stringio() | |
87 | def restore(self): |
|
87 | def restore(self): | |
88 | val = self.ui.fout.getvalue() |
|
88 | val = self.ui.fout.getvalue() | |
89 | self.ui.ferr, self.ui.fout = self.oldio |
|
89 | self.ui.ferr, self.ui.fout = self.oldio | |
90 | return val |
|
90 | return val | |
91 |
|
91 | |||
92 | def _client(self): |
|
92 | def _client(self): | |
93 | return 'remote:%s:%s:%s' % ( |
|
93 | return 'remote:%s:%s:%s' % ( | |
94 | self.req.env.get('wsgi.url_scheme') or 'http', |
|
94 | self.req.env.get('wsgi.url_scheme') or 'http', | |
95 | urlreq.quote(self.req.env.get('REMOTE_HOST', '')), |
|
95 | urlreq.quote(self.req.env.get('REMOTE_HOST', '')), | |
96 | urlreq.quote(self.req.env.get('REMOTE_USER', ''))) |
|
96 | urlreq.quote(self.req.env.get('REMOTE_USER', ''))) | |
97 |
|
97 | |||
98 | def responsetype(self, v1compressible=False): |
|
98 | def responsetype(self, v1compressible=False): | |
99 | """Determine the appropriate response type and compression settings. |
|
99 | """Determine the appropriate response type and compression settings. | |
100 |
|
100 | |||
101 | The ``v1compressible`` argument states whether the response with |
|
101 | The ``v1compressible`` argument states whether the response with | |
102 | application/mercurial-0.1 media types should be zlib compressed. |
|
102 | application/mercurial-0.1 media types should be zlib compressed. | |
103 |
|
103 | |||
104 | Returns a tuple of (mediatype, compengine, engineopts). |
|
104 | Returns a tuple of (mediatype, compengine, engineopts). | |
105 | """ |
|
105 | """ | |
106 | # For now, if it isn't compressible in the old world, it's never |
|
106 | # For now, if it isn't compressible in the old world, it's never | |
107 | # compressible. We can change this to send uncompressed 0.2 payloads |
|
107 | # compressible. We can change this to send uncompressed 0.2 payloads | |
108 | # later. |
|
108 | # later. | |
109 | if not v1compressible: |
|
109 | if not v1compressible: | |
110 | return HGTYPE, None, None |
|
110 | return HGTYPE, None, None | |
111 |
|
111 | |||
112 | # Determine the response media type and compression engine based |
|
112 | # Determine the response media type and compression engine based | |
113 | # on the request parameters. |
|
113 | # on the request parameters. | |
114 | protocaps = decodevaluefromheaders(self.req, 'X-HgProto').split(' ') |
|
114 | protocaps = decodevaluefromheaders(self.req, 'X-HgProto').split(' ') | |
115 |
|
115 | |||
116 | if '0.2' in protocaps: |
|
116 | if '0.2' in protocaps: | |
117 | # Default as defined by wire protocol spec. |
|
117 | # Default as defined by wire protocol spec. | |
118 | compformats = ['zlib', 'none'] |
|
118 | compformats = ['zlib', 'none'] | |
119 | for cap in protocaps: |
|
119 | for cap in protocaps: | |
120 | if cap.startswith('comp='): |
|
120 | if cap.startswith('comp='): | |
121 | compformats = cap[5:].split(',') |
|
121 | compformats = cap[5:].split(',') | |
122 | break |
|
122 | break | |
123 |
|
123 | |||
124 | # Now find an agreed upon compression format. |
|
124 | # Now find an agreed upon compression format. | |
125 | for engine in wireproto.supportedcompengines(self.ui, self, |
|
125 | for engine in wireproto.supportedcompengines(self.ui, self, | |
126 | util.SERVERROLE): |
|
126 | util.SERVERROLE): | |
127 | if engine.wireprotosupport().name in compformats: |
|
127 | if engine.wireprotosupport().name in compformats: | |
128 | opts = {} |
|
128 | opts = {} | |
129 | level = self.ui.configint('server', |
|
129 | level = self.ui.configint('server', | |
130 | '%slevel' % engine.name()) |
|
130 | '%slevel' % engine.name()) | |
131 | if level is not None: |
|
131 | if level is not None: | |
132 | opts['level'] = level |
|
132 | opts['level'] = level | |
133 |
|
133 | |||
134 | return HGTYPE2, engine, opts |
|
134 | return HGTYPE2, engine, opts | |
135 |
|
135 | |||
136 | # No mutually supported compression format. Fall back to the |
|
136 | # No mutually supported compression format. Fall back to the | |
137 | # legacy protocol. |
|
137 | # legacy protocol. | |
138 |
|
138 | |||
139 | # Don't allow untrusted settings because disabling compression or |
|
139 | # Don't allow untrusted settings because disabling compression or | |
140 | # setting a very high compression level could lead to flooding |
|
140 | # setting a very high compression level could lead to flooding | |
141 | # the server's network or CPU. |
|
141 | # the server's network or CPU. | |
142 | opts = {'level': self.ui.configint('server', 'zliblevel')} |
|
142 | opts = {'level': self.ui.configint('server', 'zliblevel')} | |
143 | return HGTYPE, util.compengines['zlib'], opts |
|
143 | return HGTYPE, util.compengines['zlib'], opts | |
144 |
|
144 | |||
145 | def iscmd(cmd): |
|
145 | def iscmd(cmd): | |
146 | return cmd in wireproto.commands |
|
146 | return cmd in wireproto.commands | |
147 |
|
147 | |||
148 | def call(repo, req, cmd): |
|
148 | def call(repo, req, cmd): | |
149 | p = webproto(req, repo.ui) |
|
149 | p = webproto(req, repo.ui) | |
150 |
|
150 | |||
151 | def genversion2(gen, compress, engine, engineopts): |
|
151 | def genversion2(gen, compress, engine, engineopts): | |
152 | # application/mercurial-0.2 always sends a payload header |
|
152 | # application/mercurial-0.2 always sends a payload header | |
153 | # identifying the compression engine. |
|
153 | # identifying the compression engine. | |
154 | name = engine.wireprotosupport().name |
|
154 | name = engine.wireprotosupport().name | |
155 | assert 0 < len(name) < 256 |
|
155 | assert 0 < len(name) < 256 | |
156 | yield struct.pack('B', len(name)) |
|
156 | yield struct.pack('B', len(name)) | |
157 | yield name |
|
157 | yield name | |
158 |
|
158 | |||
159 | if compress: |
|
159 | if compress: | |
160 | for chunk in engine.compressstream(gen, opts=engineopts): |
|
160 | for chunk in engine.compressstream(gen, opts=engineopts): | |
161 | yield chunk |
|
161 | yield chunk | |
162 | else: |
|
162 | else: | |
163 | for chunk in gen: |
|
163 | for chunk in gen: | |
164 | yield chunk |
|
164 | yield chunk | |
165 |
|
165 | |||
166 | rsp = wireproto.dispatch(repo, p, cmd) |
|
166 | rsp = wireproto.dispatch(repo, p, cmd) | |
167 |
if isinstance(rsp, |
|
167 | if isinstance(rsp, bytes): | |
168 | req.respond(HTTP_OK, HGTYPE, body=rsp) |
|
168 | req.respond(HTTP_OK, HGTYPE, body=rsp) | |
169 | return [] |
|
169 | return [] | |
170 | elif isinstance(rsp, wireproto.streamres): |
|
170 | elif isinstance(rsp, wireproto.streamres): | |
171 | if rsp.reader: |
|
171 | if rsp.reader: | |
172 | gen = iter(lambda: rsp.reader.read(32768), '') |
|
172 | gen = iter(lambda: rsp.reader.read(32768), '') | |
173 | else: |
|
173 | else: | |
174 | gen = rsp.gen |
|
174 | gen = rsp.gen | |
175 |
|
175 | |||
176 | # This code for compression should not be streamres specific. It |
|
176 | # This code for compression should not be streamres specific. It | |
177 | # is here because we only compress streamres at the moment. |
|
177 | # is here because we only compress streamres at the moment. | |
178 | mediatype, engine, engineopts = p.responsetype(rsp.v1compressible) |
|
178 | mediatype, engine, engineopts = p.responsetype(rsp.v1compressible) | |
179 |
|
179 | |||
180 | if mediatype == HGTYPE and rsp.v1compressible: |
|
180 | if mediatype == HGTYPE and rsp.v1compressible: | |
181 | gen = engine.compressstream(gen, engineopts) |
|
181 | gen = engine.compressstream(gen, engineopts) | |
182 | elif mediatype == HGTYPE2: |
|
182 | elif mediatype == HGTYPE2: | |
183 | gen = genversion2(gen, rsp.v1compressible, engine, engineopts) |
|
183 | gen = genversion2(gen, rsp.v1compressible, engine, engineopts) | |
184 |
|
184 | |||
185 | req.respond(HTTP_OK, mediatype) |
|
185 | req.respond(HTTP_OK, mediatype) | |
186 | return gen |
|
186 | return gen | |
187 | elif isinstance(rsp, wireproto.pushres): |
|
187 | elif isinstance(rsp, wireproto.pushres): | |
188 | val = p.restore() |
|
188 | val = p.restore() | |
189 | rsp = '%d\n%s' % (rsp.res, val) |
|
189 | rsp = '%d\n%s' % (rsp.res, val) | |
190 | req.respond(HTTP_OK, HGTYPE, body=rsp) |
|
190 | req.respond(HTTP_OK, HGTYPE, body=rsp) | |
191 | return [] |
|
191 | return [] | |
192 | elif isinstance(rsp, wireproto.pusherr): |
|
192 | elif isinstance(rsp, wireproto.pusherr): | |
193 | # drain the incoming bundle |
|
193 | # drain the incoming bundle | |
194 | req.drain() |
|
194 | req.drain() | |
195 | p.restore() |
|
195 | p.restore() | |
196 | rsp = '0\n%s\n' % rsp.res |
|
196 | rsp = '0\n%s\n' % rsp.res | |
197 | req.respond(HTTP_OK, HGTYPE, body=rsp) |
|
197 | req.respond(HTTP_OK, HGTYPE, body=rsp) | |
198 | return [] |
|
198 | return [] | |
199 | elif isinstance(rsp, wireproto.ooberror): |
|
199 | elif isinstance(rsp, wireproto.ooberror): | |
200 | rsp = rsp.message |
|
200 | rsp = rsp.message | |
201 | req.respond(HTTP_OK, HGERRTYPE, body=rsp) |
|
201 | req.respond(HTTP_OK, HGERRTYPE, body=rsp) | |
202 | return [] |
|
202 | return [] | |
203 | raise error.ProgrammingError('hgweb.protocol internal failure', rsp) |
|
203 | raise error.ProgrammingError('hgweb.protocol internal failure', rsp) |
General Comments 0
You need to be logged in to leave comments.
Login now