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