Show More
@@ -1,90 +1,92 | |||||
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 |
|
6 | # This software may be used and distributed according to the terms | |
7 | # of the GNU General Public License, incorporated herein by reference. |
|
7 | # of the GNU General Public License, incorporated herein by reference. | |
8 |
|
8 | |||
9 | import socket, cgi, errno |
|
9 | import socket, cgi, errno | |
10 | from mercurial.i18n import gettext as _ |
|
10 | from mercurial.i18n import gettext as _ | |
11 | from common import ErrorResponse, statusmessage |
|
11 | from common import ErrorResponse, statusmessage | |
12 |
|
12 | |||
13 | class wsgirequest(object): |
|
13 | class wsgirequest(object): | |
14 | def __init__(self, wsgienv, start_response): |
|
14 | def __init__(self, wsgienv, start_response): | |
15 | version = wsgienv['wsgi.version'] |
|
15 | version = wsgienv['wsgi.version'] | |
16 | if (version < (1, 0)) or (version >= (2, 0)): |
|
16 | if (version < (1, 0)) or (version >= (2, 0)): | |
17 | raise RuntimeError("Unknown and unsupported WSGI version %d.%d" |
|
17 | raise RuntimeError("Unknown and unsupported WSGI version %d.%d" | |
18 | % version) |
|
18 | % version) | |
19 | self.inp = wsgienv['wsgi.input'] |
|
19 | self.inp = wsgienv['wsgi.input'] | |
20 | self.server_write = None |
|
20 | self.server_write = None | |
21 | self.err = wsgienv['wsgi.errors'] |
|
21 | self.err = wsgienv['wsgi.errors'] | |
22 | self.threaded = wsgienv['wsgi.multithread'] |
|
22 | self.threaded = wsgienv['wsgi.multithread'] | |
23 | self.multiprocess = wsgienv['wsgi.multiprocess'] |
|
23 | self.multiprocess = wsgienv['wsgi.multiprocess'] | |
24 | self.run_once = wsgienv['wsgi.run_once'] |
|
24 | self.run_once = wsgienv['wsgi.run_once'] | |
25 | self.env = wsgienv |
|
25 | self.env = wsgienv | |
26 | self.form = cgi.parse(self.inp, self.env, keep_blank_values=1) |
|
26 | self.form = cgi.parse(self.inp, self.env, keep_blank_values=1) | |
27 | self.start_response = start_response |
|
27 | self.start_response = start_response | |
28 | self.headers = [] |
|
28 | self.headers = [] | |
29 |
|
29 | |||
30 | def __iter__(self): |
|
30 | def __iter__(self): | |
31 | return iter([]) |
|
31 | return iter([]) | |
32 |
|
32 | |||
33 | def read(self, count=-1): |
|
33 | def read(self, count=-1): | |
34 | return self.inp.read(count) |
|
34 | return self.inp.read(count) | |
35 |
|
35 | |||
36 | def respond(self, status, *things): |
|
36 | def respond(self, status, *things): | |
37 | for thing in things: |
|
37 | for thing in things: | |
38 | if hasattr(thing, "__iter__"): |
|
38 | if hasattr(thing, "__iter__"): | |
39 | for part in thing: |
|
39 | for part in thing: | |
40 | self.respond(status, part) |
|
40 | self.respond(status, part) | |
41 | else: |
|
41 | else: | |
42 | thing = str(thing) |
|
42 | thing = str(thing) | |
43 | if self.server_write is None: |
|
43 | if self.server_write is None: | |
44 | if not self.headers: |
|
44 | if not self.headers: | |
45 | raise RuntimeError("request.write called before headers sent (%s)." % thing) |
|
45 | raise RuntimeError("request.write called before headers sent (%s)." % thing) | |
46 | if isinstance(status, ErrorResponse): |
|
46 | if isinstance(status, ErrorResponse): | |
47 | status = statusmessage(status.code) |
|
47 | status = statusmessage(status.code) | |
48 | elif isinstance(status, int): |
|
48 | elif isinstance(status, int): | |
49 | status = statusmessage(status) |
|
49 | status = statusmessage(status) | |
50 | self.server_write = self.start_response(status, |
|
50 | self.server_write = self.start_response(status, | |
51 | self.headers) |
|
51 | self.headers) | |
52 | self.start_response = None |
|
52 | self.start_response = None | |
53 | self.headers = [] |
|
53 | self.headers = [] | |
54 | try: |
|
54 | try: | |
55 | self.server_write(thing) |
|
55 | self.server_write(thing) | |
56 | except socket.error, inst: |
|
56 | except socket.error, inst: | |
57 | if inst[0] != errno.ECONNRESET: |
|
57 | if inst[0] != errno.ECONNRESET: | |
58 | raise |
|
58 | raise | |
59 |
|
59 | |||
60 | def write(self, *things): |
|
60 | def write(self, *things): | |
61 | self.respond('200 Script output follows', *things) |
|
61 | self.respond('200 Script output follows', *things) | |
62 |
|
62 | |||
63 | def writelines(self, lines): |
|
63 | def writelines(self, lines): | |
64 | for line in lines: |
|
64 | for line in lines: | |
65 | self.write(line) |
|
65 | self.write(line) | |
66 |
|
66 | |||
67 | def flush(self): |
|
67 | def flush(self): | |
68 | return None |
|
68 | return None | |
69 |
|
69 | |||
70 | def close(self): |
|
70 | def close(self): | |
71 | return None |
|
71 | return None | |
72 |
|
72 | |||
73 | def header(self, headers=[('Content-type','text/html')]): |
|
73 | def header(self, headers=[('Content-type','text/html')]): | |
74 | self.headers.extend(headers) |
|
74 | self.headers.extend(headers) | |
75 |
|
75 | |||
76 | def httphdr(self, type, filename=None, length=0, headers={}): |
|
76 | def httphdr(self, type, filename=None, length=0, headers={}): | |
77 | headers = headers.items() |
|
77 | headers = headers.items() | |
78 | headers.append(('Content-type', type)) |
|
78 | headers.append(('Content-type', type)) | |
79 | if filename: |
|
79 | if filename: | |
80 | headers.append(('Content-disposition', 'attachment; filename=%s' % |
|
80 | headers.append(('Content-disposition', 'attachment; filename=%s' % | |
81 | filename)) |
|
81 | filename)) | |
82 | if length: |
|
82 | if length: | |
83 | headers.append(('Content-length', str(length))) |
|
83 | headers.append(('Content-length', str(length))) | |
84 | self.header(headers) |
|
84 | self.header(headers) | |
85 |
|
85 | |||
86 | def wsgiapplication(app_maker): |
|
86 | def wsgiapplication(app_maker): | |
|
87 | '''For compatibility with old CGI scripts. A plain hgweb() or hgwebdir() | |||
|
88 | can and should now be used as a WSGI application.''' | |||
87 | application = app_maker() |
|
89 | application = app_maker() | |
88 | def run_wsgi(env, respond): |
|
90 | def run_wsgi(env, respond): | |
89 | application(env, respond) |
|
91 | return application(env, respond) | |
90 | return run_wsgi |
|
92 | return run_wsgi |
General Comments 0
You need to be logged in to leave comments.
Login now