1 """Wrapper for mod_fcgid, for use as a CherryPy HTTP server when testing.
3 To autostart fcgid, 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
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
37 curdir
= os
.path
.join(os
.getcwd(), os
.path
.dirname(__file__
))
43 from cherrypy
._cpcompat
import ntob
44 from cherrypy
.process
import plugins
, servers
45 from cherrypy
.test
import helper
48 def read_process(cmd
, args
=""):
49 pipein
, pipeout
= os
.popen4("%s %s" % (cmd
, args
))
51 firstline
= pipeout
.readline()
52 if (re
.search(r
"(not recognized|No such file|not found)", firstline
,
54 raise IOError('%s must be on your system path.' % cmd
)
55 output
= firstline
+ pipeout
.read()
62 CONF_PATH
= "fcgi.conf"
65 # Apache2 server conf file for testing CherryPy with mod_fcgid.
67 DocumentRoot "%(root)s"
70 LoadModule fastcgi_module modules/mod_fastcgi.dll
71 LoadModule rewrite_module modules/mod_rewrite.so
74 SetHandler fastcgi-script
76 RewriteRule ^(.*)$ /fastcgi.pyc [L]
77 FastCgiExternalServer "%(server)s" -host 127.0.0.1:4000
80 class ModFCGISupervisor(helper
.LocalSupervisor
):
87 return "FCGI Server on %s:%s" % (self
.host
, self
.port
)
89 def start(self
, modulename
):
90 cherrypy
.server
.httpserver
= servers
.FlupFCGIServer(
91 application
=cherrypy
.tree
, bindAddress
=('127.0.0.1', 4000))
92 cherrypy
.server
.httpserver
.bind_addr
= ('127.0.0.1', 4000)
93 # For FCGI, we both start apache...
95 # ...and our local server
96 helper
.LocalServer
.start(self
, modulename
)
98 def start_apache(self
):
100 if not os
.path
.isabs(fcgiconf
):
101 fcgiconf
= os
.path
.join(curdir
, fcgiconf
)
103 # Write the Apache conf file.
104 f
= open(fcgiconf
, 'wb')
106 server
= repr(os
.path
.join(curdir
, 'fastcgi.pyc'))[1:-1]
107 output
= self
.template
% {'port': self
.port
, 'root': curdir
,
109 output
= ntob(output
.replace('\r\n', '\n'))
114 result
= read_process(APACHE_PATH
, "-k start -f %s" % fcgiconf
)
119 """Gracefully shutdown a server that is serving forever."""
120 read_process(APACHE_PATH
, "-k stop")
121 helper
.LocalServer
.stop(self
)
124 cherrypy
.server
.httpserver
.fcgiserver
.application
= self
.get_app()