Package cherrypy :: Package test :: Module modfcgid
[hide private]
[frames] | no frames]

Source Code for Module cherrypy.test.modfcgid

  1  """Wrapper for mod_fcgid, for use as a CherryPy HTTP server when testing. 
  2   
  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. 
  7   
  8  You'll also need the WSGIServer from flup.servers. 
  9  See http://projects.amor.org/misc/wiki/ModPythonGateway 
 10   
 11   
 12  KNOWN BUGS 
 13  ========== 
 14   
 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  """ 
 35   
 36  import os 
 37  curdir = os.path.join(os.getcwd(), os.path.dirname(__file__)) 
 38  import re 
 39  import sys 
 40  import time 
 41   
 42  import cherrypy 
 43  from cherrypy.process import plugins, servers 
 44  from cherrypy.test import test 
 45   
 46   
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
58 59 60 APACHE_PATH = "httpd" 61 CONF_PATH = "fcgi.conf" 62 63 conf_fcgid = """ 64 # Apache2 server conf file for testing CherryPy with mod_fcgid. 65 66 DocumentRoot "%(root)s" 67 Listen %(port)s 68 LoadModule fastcgi_module modules/mod_fastcgi.dll 69 LoadModule rewrite_module modules/mod_rewrite.so 70 71 Options ExecCGI 72 SetHandler fastcgi-script 73 RewriteEngine On 74 RewriteRule ^(.*)$ /fastcgi.pyc [L] 75 FastCgiExternalServer "%(server)s" -host 127.0.0.1:4000 76 """ 77
78 -def start_apache(host, port, conf_template):
79 fcgiconf = CONF_PATH 80 if not os.path.isabs(fcgiconf): 81 fcgiconf = os.path.join(curdir, fcgiconf) 82 83 # Write the Apache conf file. 84 f = open(fcgiconf, 'wb') 85 try: 86 server = repr(os.path.join(curdir, 'fastcgi.pyc'))[1:-1] 87 output = conf_template % {'port': port, 'root': curdir, 88 'server': server} 89 output = output.replace('\r\n', '\n') 90 f.write(output) 91 finally: 92 f.close() 93 94 result = read_process(APACHE_PATH, "-k start -f %s" % fcgiconf) 95 if result: 96 print result
97
98 -def stop():
99 """Gracefully shutdown a server that is serving forever.""" 100 read_process(APACHE_PATH, "-k stop")
101 102
103 -class FCGITestHarness(test.TestHarness):
104 """TestHarness for fcgid and CherryPy.""" 105
106 - def _run(self, conf):
107 cherrypy.server.using_wsgi = True 108 cherrypy.server.using_apache = True 109 cherrypy.server.httpserver = servers.FlupFCGIServer( 110 application=cherrypy.tree, bindAddress=('127.0.0.1', 4000)) 111 cherrypy.server.httpserver.bind_addr = ('127.0.0.1', 4000) 112 try: 113 start_apache(self.host, self.port, conf_fcgid) 114 return test.TestHarness._run(self, conf) 115 finally: 116 stop()
117