Show More
@@ -1,155 +1,155 b'' | |||||
1 | # hgweb/request.py - An http request from either CGI or the standalone server. |
|
1 | # hgweb/request.py - An http request from either CGI or the standalone server. | |
2 | # |
|
2 | # | |
3 | # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> |
|
3 | # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> | |
4 | # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> |
|
4 | # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> | |
5 | # |
|
5 | # | |
6 | # This software may be used and distributed according to the terms of the |
|
6 | # This software may be used and distributed according to the terms of the | |
7 | # GNU General Public License version 2 or any later version. |
|
7 | # GNU General Public License version 2 or any later version. | |
8 |
|
8 | |||
9 | from __future__ import absolute_import |
|
9 | from __future__ import absolute_import | |
10 |
|
10 | |||
11 | import cgi |
|
11 | import cgi | |
12 | import errno |
|
12 | import errno | |
13 | import socket |
|
13 | import socket | |
14 |
|
14 | |||
15 | from .common import ( |
|
15 | from .common import ( | |
16 | ErrorResponse, |
|
16 | ErrorResponse, | |
17 | HTTP_NOT_MODIFIED, |
|
17 | HTTP_NOT_MODIFIED, | |
18 | statusmessage, |
|
18 | statusmessage, | |
19 | ) |
|
19 | ) | |
20 |
|
20 | |||
21 | from .. import ( |
|
21 | from .. import ( | |
22 | pycompat, |
|
22 | pycompat, | |
23 | util, |
|
23 | util, | |
24 | ) |
|
24 | ) | |
25 |
|
25 | |||
26 | shortcuts = { |
|
26 | shortcuts = { | |
27 | 'cl': [('cmd', ['changelog']), ('rev', None)], |
|
27 | 'cl': [('cmd', ['changelog']), ('rev', None)], | |
28 | 'sl': [('cmd', ['shortlog']), ('rev', None)], |
|
28 | 'sl': [('cmd', ['shortlog']), ('rev', None)], | |
29 | 'cs': [('cmd', ['changeset']), ('node', None)], |
|
29 | 'cs': [('cmd', ['changeset']), ('node', None)], | |
30 | 'f': [('cmd', ['file']), ('filenode', None)], |
|
30 | 'f': [('cmd', ['file']), ('filenode', None)], | |
31 | 'fl': [('cmd', ['filelog']), ('filenode', None)], |
|
31 | 'fl': [('cmd', ['filelog']), ('filenode', None)], | |
32 | 'fd': [('cmd', ['filediff']), ('node', None)], |
|
32 | 'fd': [('cmd', ['filediff']), ('node', None)], | |
33 | 'fa': [('cmd', ['annotate']), ('filenode', None)], |
|
33 | 'fa': [('cmd', ['annotate']), ('filenode', None)], | |
34 | 'mf': [('cmd', ['manifest']), ('manifest', None)], |
|
34 | 'mf': [('cmd', ['manifest']), ('manifest', None)], | |
35 | 'ca': [('cmd', ['archive']), ('node', None)], |
|
35 | 'ca': [('cmd', ['archive']), ('node', None)], | |
36 | 'tags': [('cmd', ['tags'])], |
|
36 | 'tags': [('cmd', ['tags'])], | |
37 | 'tip': [('cmd', ['changeset']), ('node', ['tip'])], |
|
37 | 'tip': [('cmd', ['changeset']), ('node', ['tip'])], | |
38 | 'static': [('cmd', ['static']), ('file', None)] |
|
38 | 'static': [('cmd', ['static']), ('file', None)] | |
39 | } |
|
39 | } | |
40 |
|
40 | |||
41 | def normalize(form): |
|
41 | def normalize(form): | |
42 | # first expand the shortcuts |
|
42 | # first expand the shortcuts | |
43 | for k in shortcuts: |
|
43 | for k in shortcuts: | |
44 | if k in form: |
|
44 | if k in form: | |
45 | for name, value in shortcuts[k]: |
|
45 | for name, value in shortcuts[k]: | |
46 | if value is None: |
|
46 | if value is None: | |
47 | value = form[k] |
|
47 | value = form[k] | |
48 | form[name] = value |
|
48 | form[name] = value | |
49 | del form[k] |
|
49 | del form[k] | |
50 | # And strip the values |
|
50 | # And strip the values | |
51 | for k, v in form.iteritems(): |
|
51 | for k, v in form.iteritems(): | |
52 | form[k] = [i.strip() for i in v] |
|
52 | form[k] = [i.strip() for i in v] | |
53 | return form |
|
53 | return form | |
54 |
|
54 | |||
55 | class wsgirequest(object): |
|
55 | class wsgirequest(object): | |
56 | """Higher-level API for a WSGI request. |
|
56 | """Higher-level API for a WSGI request. | |
57 |
|
57 | |||
58 | WSGI applications are invoked with 2 arguments. They are used to |
|
58 | WSGI applications are invoked with 2 arguments. They are used to | |
59 | instantiate instances of this class, which provides higher-level APIs |
|
59 | instantiate instances of this class, which provides higher-level APIs | |
60 | for obtaining request parameters, writing HTTP output, etc. |
|
60 | for obtaining request parameters, writing HTTP output, etc. | |
61 | """ |
|
61 | """ | |
62 | def __init__(self, wsgienv, start_response): |
|
62 | def __init__(self, wsgienv, start_response): | |
63 | version = wsgienv[r'wsgi.version'] |
|
63 | version = wsgienv[r'wsgi.version'] | |
64 | if (version < (1, 0)) or (version >= (2, 0)): |
|
64 | if (version < (1, 0)) or (version >= (2, 0)): | |
65 | raise RuntimeError("Unknown and unsupported WSGI version %d.%d" |
|
65 | raise RuntimeError("Unknown and unsupported WSGI version %d.%d" | |
66 | % version) |
|
66 | % version) | |
67 | self.inp = wsgienv[r'wsgi.input'] |
|
67 | self.inp = wsgienv[r'wsgi.input'] | |
68 | self.err = wsgienv[r'wsgi.errors'] |
|
68 | self.err = wsgienv[r'wsgi.errors'] | |
69 | self.threaded = wsgienv[r'wsgi.multithread'] |
|
69 | self.threaded = wsgienv[r'wsgi.multithread'] | |
70 | self.multiprocess = wsgienv[r'wsgi.multiprocess'] |
|
70 | self.multiprocess = wsgienv[r'wsgi.multiprocess'] | |
71 | self.run_once = wsgienv[r'wsgi.run_once'] |
|
71 | self.run_once = wsgienv[r'wsgi.run_once'] | |
72 | self.env = wsgienv |
|
72 | self.env = wsgienv | |
73 | self.form = normalize(cgi.parse(self.inp, |
|
73 | self.form = normalize(cgi.parse(self.inp, | |
74 | self.env, |
|
74 | self.env, | |
75 | keep_blank_values=1)) |
|
75 | keep_blank_values=1)) | |
76 | self._start_response = start_response |
|
76 | self._start_response = start_response | |
77 | self.server_write = None |
|
77 | self.server_write = None | |
78 | self.headers = [] |
|
78 | self.headers = [] | |
79 |
|
79 | |||
80 | def __iter__(self): |
|
80 | def __iter__(self): | |
81 | return iter([]) |
|
81 | return iter([]) | |
82 |
|
82 | |||
83 | def read(self, count=-1): |
|
83 | def read(self, count=-1): | |
84 | return self.inp.read(count) |
|
84 | return self.inp.read(count) | |
85 |
|
85 | |||
86 | def drain(self): |
|
86 | def drain(self): | |
87 | '''need to read all data from request, httplib is half-duplex''' |
|
87 | '''need to read all data from request, httplib is half-duplex''' | |
88 | length = int(self.env.get('CONTENT_LENGTH') or 0) |
|
88 | length = int(self.env.get('CONTENT_LENGTH') or 0) | |
89 | for s in util.filechunkiter(self.inp, limit=length): |
|
89 | for s in util.filechunkiter(self.inp, limit=length): | |
90 | pass |
|
90 | pass | |
91 |
|
91 | |||
92 | def respond(self, status, type, filename=None, body=None): |
|
92 | def respond(self, status, type, filename=None, body=None): | |
93 | if not isinstance(type, str): |
|
93 | if not isinstance(type, str): | |
94 | type = pycompat.sysstr(type) |
|
94 | type = pycompat.sysstr(type) | |
95 | if self._start_response is not None: |
|
95 | if self._start_response is not None: | |
96 | self.headers.append(('Content-Type', type)) |
|
96 | self.headers.append((r'Content-Type', type)) | |
97 | if filename: |
|
97 | if filename: | |
98 | filename = (filename.rpartition('/')[-1] |
|
98 | filename = (filename.rpartition('/')[-1] | |
99 | .replace('\\', '\\\\').replace('"', '\\"')) |
|
99 | .replace('\\', '\\\\').replace('"', '\\"')) | |
100 | self.headers.append(('Content-Disposition', |
|
100 | self.headers.append(('Content-Disposition', | |
101 | 'inline; filename="%s"' % filename)) |
|
101 | 'inline; filename="%s"' % filename)) | |
102 | if body is not None: |
|
102 | if body is not None: | |
103 | self.headers.append(('Content-Length', str(len(body)))) |
|
103 | self.headers.append((r'Content-Length', str(len(body)))) | |
104 |
|
104 | |||
105 | for k, v in self.headers: |
|
105 | for k, v in self.headers: | |
106 | if not isinstance(v, str): |
|
106 | if not isinstance(v, str): | |
107 | raise TypeError('header value must be string: %r' % (v,)) |
|
107 | raise TypeError('header value must be string: %r' % (v,)) | |
108 |
|
108 | |||
109 | if isinstance(status, ErrorResponse): |
|
109 | if isinstance(status, ErrorResponse): | |
110 | self.headers.extend(status.headers) |
|
110 | self.headers.extend(status.headers) | |
111 | if status.code == HTTP_NOT_MODIFIED: |
|
111 | if status.code == HTTP_NOT_MODIFIED: | |
112 | # RFC 2616 Section 10.3.5: 304 Not Modified has cases where |
|
112 | # RFC 2616 Section 10.3.5: 304 Not Modified has cases where | |
113 | # it MUST NOT include any headers other than these and no |
|
113 | # it MUST NOT include any headers other than these and no | |
114 | # body |
|
114 | # body | |
115 | self.headers = [(k, v) for (k, v) in self.headers if |
|
115 | self.headers = [(k, v) for (k, v) in self.headers if | |
116 | k in ('Date', 'ETag', 'Expires', |
|
116 | k in ('Date', 'ETag', 'Expires', | |
117 | 'Cache-Control', 'Vary')] |
|
117 | 'Cache-Control', 'Vary')] | |
118 | status = statusmessage(status.code, str(status)) |
|
118 | status = statusmessage(status.code, str(status)) | |
119 | elif status == 200: |
|
119 | elif status == 200: | |
120 | status = '200 Script output follows' |
|
120 | status = '200 Script output follows' | |
121 | elif isinstance(status, int): |
|
121 | elif isinstance(status, int): | |
122 | status = statusmessage(status) |
|
122 | status = statusmessage(status) | |
123 |
|
123 | |||
124 | self.server_write = self._start_response(status, self.headers) |
|
124 | self.server_write = self._start_response(status, self.headers) | |
125 | self._start_response = None |
|
125 | self._start_response = None | |
126 | self.headers = [] |
|
126 | self.headers = [] | |
127 | if body is not None: |
|
127 | if body is not None: | |
128 | self.write(body) |
|
128 | self.write(body) | |
129 | self.server_write = None |
|
129 | self.server_write = None | |
130 |
|
130 | |||
131 | def write(self, thing): |
|
131 | def write(self, thing): | |
132 | if thing: |
|
132 | if thing: | |
133 | try: |
|
133 | try: | |
134 | self.server_write(thing) |
|
134 | self.server_write(thing) | |
135 | except socket.error as inst: |
|
135 | except socket.error as inst: | |
136 | if inst[0] != errno.ECONNRESET: |
|
136 | if inst[0] != errno.ECONNRESET: | |
137 | raise |
|
137 | raise | |
138 |
|
138 | |||
139 | def writelines(self, lines): |
|
139 | def writelines(self, lines): | |
140 | for line in lines: |
|
140 | for line in lines: | |
141 | self.write(line) |
|
141 | self.write(line) | |
142 |
|
142 | |||
143 | def flush(self): |
|
143 | def flush(self): | |
144 | return None |
|
144 | return None | |
145 |
|
145 | |||
146 | def close(self): |
|
146 | def close(self): | |
147 | return None |
|
147 | return None | |
148 |
|
148 | |||
149 | def wsgiapplication(app_maker): |
|
149 | def wsgiapplication(app_maker): | |
150 | '''For compatibility with old CGI scripts. A plain hgweb() or hgwebdir() |
|
150 | '''For compatibility with old CGI scripts. A plain hgweb() or hgwebdir() | |
151 | can and should now be used as a WSGI application.''' |
|
151 | can and should now be used as a WSGI application.''' | |
152 | application = app_maker() |
|
152 | application = app_maker() | |
153 | def run_wsgi(env, respond): |
|
153 | def run_wsgi(env, respond): | |
154 | return application(env, respond) |
|
154 | return application(env, respond) | |
155 | return run_wsgi |
|
155 | return run_wsgi |
General Comments 0
You need to be logged in to leave comments.
Login now