Fixed python_path problem.
[smonitor.git] / lib / cherrypy / test / modfastcgi.py
blob95acf1412e92c9dab0177cda1ab9183d893fcfb4
1 """Wrapper for mod_fastcgi, for use as a CherryPy HTTP server when testing.
3 To autostart fastcgi, the "apache" executable or script must be
4 on your system path, or you must override the global APACHE_PATH.
5 On some platforms, "apache" may be called "apachectl", "apache2ctl",
6 or "httpd"--create a symlink to them if needed.
8 You'll also need the WSGIServer from flup.servers.
9 See http://projects.amor.org/misc/wiki/ModPythonGateway
12 KNOWN BUGS
13 ==========
15 1. Apache processes Range headers automatically; CherryPy's truncated
16 output is then truncated again by Apache. See test_core.testRanges.
17 This was worked around in http://www.cherrypy.org/changeset/1319.
18 2. Apache does not allow custom HTTP methods like CONNECT as per the spec.
19 See test_core.testHTTPMethods.
20 3. Max request header and body settings do not work with Apache.
21 4. Apache replaces status "reason phrases" automatically. For example,
22 CherryPy may set "304 Not modified" but Apache will write out
23 "304 Not Modified" (capital "M").
24 5. Apache does not allow custom error codes as per the spec.
25 6. Apache (or perhaps modpython, or modpython_gateway) unquotes %xx in the
26 Request-URI too early.
27 7. mod_python will not read request bodies which use the "chunked"
28 transfer-coding (it passes REQUEST_CHUNKED_ERROR to ap_setup_client_block
29 instead of REQUEST_CHUNKED_DECHUNK, see Apache2's http_protocol.c and
30 mod_python's requestobject.c).
31 8. Apache will output a "Content-Length: 0" response header even if there's
32 no response entity body. This isn't really a bug; it just differs from
33 the CherryPy default.
34 """
36 import os
37 curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
38 import re
39 import sys
40 import time
42 import cherrypy
43 from cherrypy.process import plugins, servers
44 from cherrypy.test import helper
47 def read_process(cmd, args=""):
48 pipein, pipeout = os.popen4("%s %s" % (cmd, args))
49 try:
50 firstline = pipeout.readline()
51 if (re.search(r"(not recognized|No such file|not found)", firstline,
52 re.IGNORECASE)):
53 raise IOError('%s must be on your system path.' % cmd)
54 output = firstline + pipeout.read()
55 finally:
56 pipeout.close()
57 return output
60 APACHE_PATH = "apache2ctl"
61 CONF_PATH = "fastcgi.conf"
63 conf_fastcgi = """
64 # Apache2 server conf file for testing CherryPy with mod_fastcgi.
65 # fumanchu: I had to hard-code paths due to crazy Debian layouts :(
66 ServerRoot /usr/lib/apache2
67 User #1000
68 ErrorLog %(root)s/mod_fastcgi.error.log
70 DocumentRoot "%(root)s"
71 ServerName 127.0.0.1
72 Listen %(port)s
73 LoadModule fastcgi_module modules/mod_fastcgi.so
74 LoadModule rewrite_module modules/mod_rewrite.so
76 Options +ExecCGI
77 SetHandler fastcgi-script
78 RewriteEngine On
79 RewriteRule ^(.*)$ /fastcgi.pyc [L]
80 FastCgiExternalServer "%(server)s" -host 127.0.0.1:4000
81 """
83 def erase_script_name(environ, start_response):
84 environ['SCRIPT_NAME'] = ''
85 return cherrypy.tree(environ, start_response)
87 class ModFCGISupervisor(helper.LocalWSGISupervisor):
89 httpserver_class = "cherrypy.process.servers.FlupFCGIServer"
90 using_apache = True
91 using_wsgi = True
92 template = conf_fastcgi
94 def __str__(self):
95 return "FCGI Server on %s:%s" % (self.host, self.port)
97 def start(self, modulename):
98 cherrypy.server.httpserver = servers.FlupFCGIServer(
99 application=erase_script_name, bindAddress=('127.0.0.1', 4000))
100 cherrypy.server.httpserver.bind_addr = ('127.0.0.1', 4000)
101 cherrypy.server.socket_port = 4000
102 # For FCGI, we both start apache...
103 self.start_apache()
104 # ...and our local server
105 cherrypy.engine.start()
106 self.sync_apps()
108 def start_apache(self):
109 fcgiconf = CONF_PATH
110 if not os.path.isabs(fcgiconf):
111 fcgiconf = os.path.join(curdir, fcgiconf)
113 # Write the Apache conf file.
114 f = open(fcgiconf, 'wb')
115 try:
116 server = repr(os.path.join(curdir, 'fastcgi.pyc'))[1:-1]
117 output = self.template % {'port': self.port, 'root': curdir,
118 'server': server}
119 output = output.replace('\r\n', '\n')
120 f.write(output)
121 finally:
122 f.close()
124 result = read_process(APACHE_PATH, "-k start -f %s" % fcgiconf)
125 if result:
126 print(result)
128 def stop(self):
129 """Gracefully shutdown a server that is serving forever."""
130 read_process(APACHE_PATH, "-k stop")
131 helper.LocalWSGISupervisor.stop(self)
133 def sync_apps(self):
134 cherrypy.server.httpserver.fcgiserver.application = self.get_app(erase_script_name)