39
|
1 # -*- coding: utf-8 -*-
|
|
2 #
|
|
3 # Copyright (C) 2005 Edgewall Software
|
|
4 # Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de>
|
|
5 # Copyright (C) 2005 Matthew Good <trac@matt-good.net>
|
|
6 # All rights reserved.
|
|
7 #
|
|
8 # This software is licensed as described in the file COPYING, which
|
|
9 # you should have received as part of this distribution. The terms
|
|
10 # are also available at http://trac.edgewall.com/license.html.
|
|
11 #
|
|
12 # This software consists of voluntary contributions made by many
|
|
13 # individuals. For the exact contribution history, see the revision
|
|
14 # history and logs, available at http://projects.edgewall.com/trac/.
|
|
15 #
|
|
16 # Author: Christopher Lenz <cmlenz@gmx.de>
|
|
17 # Matthew Good <trac@matt-good.net>
|
|
18
|
|
19 import os
|
|
20 import sys
|
|
21
|
|
22 from trac.web.main import dispatch_request
|
|
23 from trac.web.wsgi import WSGIGateway
|
|
24
|
|
25
|
|
26 class CGIGateway(WSGIGateway):
|
|
27
|
|
28 wsgi_multithread = False
|
|
29 wsgi_multiprocess = False
|
|
30 wsgi_run_once = True
|
|
31
|
|
32 def __init__(self):
|
|
33 WSGIGateway.__init__(self, dict(os.environ))
|
|
34
|
|
35 def _write(self, data):
|
|
36 assert self.headers_set, 'Response not started'
|
|
37
|
|
38 if not self.headers_sent:
|
|
39 status, headers = self.headers_sent = self.headers_set
|
|
40 sys.stdout.write('Status: %s\r\n' % status)
|
|
41 for header in headers:
|
|
42 sys.stdout.write('%s: %s\r\n' % header)
|
|
43 sys.stdout.write('\r\n')
|
|
44 sys.stdout.flush()
|
|
45
|
|
46 sys.stdout.write(data)
|
|
47 sys.stdout.flush()
|
|
48
|
|
49
|
|
50 def run():
|
|
51 try: # Make FreeBSD use blocking I/O like other platforms
|
|
52 import fcntl
|
|
53 for stream in [sys.stdin, sys.stdout]:
|
|
54 fd = stream.fileno()
|
|
55 flags = fcntl.fcntl(fd, fcntl.F_GETFL)
|
|
56 fcntl.fcntl(fd, fcntl.F_SETFL, flags & ~os.O_NONBLOCK)
|
|
57 except ImportError:
|
|
58 pass
|
|
59
|
|
60 try: # Use binary I/O on Windows
|
|
61 import msvcrt
|
|
62 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
|
|
63 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
|
|
64 except ImportError:
|
|
65 pass
|
|
66
|
|
67 gateway = CGIGateway()
|
|
68 gateway.run(dispatch_request)
|