annotate mercurial/hgweb/wsgicgi.py @ 4072:e916bc0dfdd6

transplant: remote bundle source was not closed before deleting the fetched bundle. It fails under win32 because "normal" opened files cannot be removed.
author Patrick Mezard <pmezard@gmail.com>
date Sun, 11 Feb 2007 18:37:23 +0100
parents eb0b4a2d70a9
children 5a89c61c189c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
1 # hgweb/wsgicgi.py - CGI->WSGI translator
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
2 #
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
3 # Copyright 2006 Eric Hopper <hopper@omnifarious.org>
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
4 #
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
5 # This software may be used and distributed according to the terms
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
6 # of the GNU General Public License, incorporated herein by reference.
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
7 #
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
8 # This was originally copied from the public domain code at
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
9 # http://www.python.org/dev/peps/pep-0333/#the-server-gateway-side
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
10
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
11 import os, sys
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
12
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
13 def launch(application):
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
14
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
15 environ = dict(os.environ.items())
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
16 environ['wsgi.input'] = sys.stdin
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
17 environ['wsgi.errors'] = sys.stderr
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
18 environ['wsgi.version'] = (1, 0)
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
19 environ['wsgi.multithread'] = False
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
20 environ['wsgi.multiprocess'] = True
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
21 environ['wsgi.run_once'] = True
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
22
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
23 if environ.get('HTTPS','off') in ('on','1'):
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
24 environ['wsgi.url_scheme'] = 'https'
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
25 else:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
26 environ['wsgi.url_scheme'] = 'http'
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
27
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
28 headers_set = []
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
29 headers_sent = []
2558
1120302009d7 hgweb: fix unbundle.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2506
diff changeset
30 out = sys.stdout
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
31
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
32 def write(data):
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
33 if not headers_set:
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
34 raise AssertionError("write() before start_response()")
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
35
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
36 elif not headers_sent:
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
37 # Before the first output, send the stored headers
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
38 status, response_headers = headers_sent[:] = headers_set
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
39 out.write('Status: %s\r\n' % status)
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
40 for header in response_headers:
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
41 out.write('%s: %s\r\n' % header)
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
42 out.write('\r\n')
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
43
2558
1120302009d7 hgweb: fix unbundle.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2506
diff changeset
44 out.write(data)
1120302009d7 hgweb: fix unbundle.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 2506
diff changeset
45 out.flush()
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
46
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
47 def start_response(status, response_headers, exc_info=None):
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
48 if exc_info:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
49 try:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
50 if headers_sent:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
51 # Re-raise original exception if headers sent
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
52 raise exc_info[0], exc_info[1], exc_info[2]
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
53 finally:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
54 exc_info = None # avoid dangling circular ref
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
55 elif headers_set:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
56 raise AssertionError("Headers already set!")
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
57
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2558
diff changeset
58 headers_set[:] = [status, response_headers]
2506
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
59 return write
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
60
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
61 result = application(environ, start_response)
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
62 try:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
63 for data in result:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
64 if data: # don't send headers until body appears
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
65 write(data)
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
66 if not headers_sent:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
67 write('') # send headers now if body was empty
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
68 finally:
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
69 if hasattr(result,'close'):
d0db3462d568 This patch make several WSGI related alterations.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
70 result.close()