Mercurial > hg-stable
annotate mercurial/hgweb/request.py @ 2506:d0db3462d568
This patch make several WSGI related alterations.
First, it changes the server to be almost a generic WSGI server.
Second, it changes request.py to have wsgiapplication and
_wsgirequest. wsgiapplication is a class that creates _wsgirequests
when called by a WSGI compliant server. It needs to know whether
or not it should create hgwebdir or hgweb requests.
Lastly, wsgicgi.py is added, and the CGI scripts are altered to
use it to launch wsgiapplications in a WSGI compliant way.
As a side effect, all the keepalive code has been removed from
request.py. This code needs to be moved so that it is exclusively
in server.py
author | Eric Hopper <hopper@omnifarious.org> |
---|---|
date | Tue, 27 Jun 2006 00:09:33 -0700 |
parents | e10665147d26 |
children | 7e01da2bc7f3 |
rev | line source |
---|---|
2391
d351a3be3371
Fixing up comment headers for split up code.
Eric Hopper <hopper@omnifarious.org>
parents:
2355
diff
changeset
|
1 # hgweb/request.py - An http request from either CGI or the standalone server. |
131 | 2 # |
238
3b92f8fe47ae
hgweb.py: kill #! line, clean up copyright notice
mpm@selenic.com
parents:
222
diff
changeset
|
3 # Copyright 21 May 2005 - (c) 2005 Jake Edge <jake@edge2.net> |
575 | 4 # Copyright 2005 Matt Mackall <mpm@selenic.com> |
131 | 5 # |
6 # This software may be used and distributed according to the terms | |
7 # of the GNU General Public License, incorporated herein by reference. | |
8 | |
2311
b832b6eb65ab
Moving hgweb.py into it's own module in preparation for breaking it up.
Eric Hopper <hopper@omnifarious.org>
parents:
2275
diff
changeset
|
9 from mercurial.demandload import demandload |
2394
a8f1049d1d2d
hgweb: fix errors and warnings found by pychecker
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2391
diff
changeset
|
10 demandload(globals(), "socket sys cgi os errno") |
2311
b832b6eb65ab
Moving hgweb.py into it's own module in preparation for breaking it up.
Eric Hopper <hopper@omnifarious.org>
parents:
2275
diff
changeset
|
11 from mercurial.i18n import gettext as _ |
138 | 12 |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
13 class wsgiapplication(object): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
14 def __init__(self, destmaker): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
15 self.destmaker = destmaker |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
16 |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
17 def __call__(self, wsgienv, start_response): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
18 return _wsgirequest(self.destmaker(), wsgienv, start_response) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
19 |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
20 class _wsgioutputfile(object): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
21 def __init__(self, request): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
22 self.request = request |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
23 |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
24 def write(self, data): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
25 self.request.write(data) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
26 def writelines(self, lines): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
27 for line in lines: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
28 self.write(line) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
29 def flush(self): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
30 return None |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
31 def close(self): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
32 return None |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
33 |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
34 class _wsgirequest(object): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
35 def __init__(self, destination, wsgienv, start_response): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
36 version = wsgienv['wsgi.version'] |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
37 if (version < (1,0)) or (version >= (2, 0)): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
38 raise RuntimeError("Unknown and unsupported WSGI version %d.%d" \ |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
39 % version) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
40 self.inp = wsgienv['wsgi.input'] |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
41 self.out = _wsgioutputfile(self) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
42 self.server_write = None |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
43 self.err = wsgienv['wsgi.errors'] |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
44 self.threaded = wsgienv['wsgi.multithread'] |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
45 self.multiprocess = wsgienv['wsgi.multiprocess'] |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
46 self.run_once = wsgienv['wsgi.run_once'] |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
47 self.env = wsgienv |
1407
db571bcaa35d
allow empty values for url so we can have /?tip
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1406
diff
changeset
|
48 self.form = cgi.parse(self.inp, self.env, keep_blank_values=1) |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
49 self.start_response = start_response |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
50 self.headers = [] |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
51 destination.run(self) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
52 |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
53 def __iter__(self): |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
54 return iter([]) |
131 | 55 |
2464
09b1c9ef317c
push over http: server support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2434
diff
changeset
|
56 def read(self, count=-1): |
09b1c9ef317c
push over http: server support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2434
diff
changeset
|
57 return self.inp.read(count) |
09b1c9ef317c
push over http: server support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2434
diff
changeset
|
58 |
1159
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
59 def write(self, *things): |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
60 if self.server_write is None: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
61 if not self.headers: |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
62 raise RuntimeError("request.write called before headers sent.") |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
63 self.server_write = self.start_response('200 Script output follows', |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
64 self.headers) |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
65 self.start_response = None |
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
66 self.headers = None |
1159
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
67 for thing in things: |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
68 if hasattr(thing, "__iter__"): |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
69 for part in thing: |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
70 self.write(part) |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
71 else: |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
72 try: |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
73 self.server_write(str(thing)) |
1174
9d9f4973c76a
Added missing 'import errno', and use errno for EPIPE, too.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1172
diff
changeset
|
74 except socket.error, inst: |
9d9f4973c76a
Added missing 'import errno', and use errno for EPIPE, too.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1172
diff
changeset
|
75 if inst[0] != errno.ECONNRESET: |
1159
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
76 raise |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
77 |
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
78 def header(self, headers=[('Content-type','text/html')]): |
2506
d0db3462d568
This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
2466
diff
changeset
|
79 self.headers.extend(headers) |
1159
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
80 |
2466
e10665147d26
push over http: server side authorization support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2464
diff
changeset
|
81 def httphdr(self, type, filename=None, length=0, headers={}): |
e10665147d26
push over http: server side authorization support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2464
diff
changeset
|
82 headers = headers.items() |
e10665147d26
push over http: server side authorization support.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2464
diff
changeset
|
83 headers.append(('Content-type', type)) |
2434
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
84 if filename: |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
85 headers.append(('Content-disposition', 'attachment; filename=%s' % |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
86 filename)) |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
87 if length: |
a2df85adface
http server: support persistent connections.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2394
diff
changeset
|
88 headers.append(('Content-length', str(length))) |
1159
b6f5a947e62e
Change use of global sys.stdout, sys.stdin os.environ to a hgrequest object.
Vincent Wagelaar <vincent@ricardis.tudelft.nl>
parents:
1143
diff
changeset
|
89 self.header(headers) |