Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/branches/cherrypy-2.1/cherrypy/_cpwsgi.py

Revision 693 (checked in by fumanchu, 3 years ago)

Phew. Tracked down all possible occurrences of KeyboardInterrupt? and trapped them appropriately. There's still a problem in test_states when using the CherryPyHTTPServer (it hangs during KeyboardInterrupt? testing), but all other servers pass. Maybe *this* fixes #321. ;)

Line 
1 """
2 Copyright (c) 2005, CherryPy Team (team@cherrypy.org)
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8     * Redistributions of source code must retain the above copyright notice,
9       this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright notice,
11       this list of conditions and the following disclaimer in the documentation
12       and/or other materials provided with the distribution.
13     * Neither the name of the CherryPy Team nor the names of its contributors
14       may be used to endorse or promote products derived from this software
15       without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 """
28
29 """
30 A WSGI application interface (see PEP 333).
31 """
32
33 import sys
34 import cherrypy
35 from cherrypy import _cputil, _cphttptools
36 from cherrypy._cpwsgiserver import CherryPyWSGIServer as server
37
38
39 def requestLine(environ):
40     """Rebuild first line of the request (e.g. "GET /path HTTP/1.0")."""
41    
42     resource = environ.get('SCRIPT_NAME', '') + environ.get('PATH_INFO', '')
43     if not (resource == "*" or resource.startswith("/")):
44         resource = "/" + resource
45    
46     qString = environ.get('QUERY_STRING')
47     if qString:
48         resource += '?' + qString
49    
50     resource = resource.replace(" ", "%20")
51    
52     return ('%s %s %s' % (environ['REQUEST_METHOD'],
53                           resource or '/',
54                           environ['SERVER_PROTOCOL']
55                           )
56             )
57
58 headerNames = {'HTTP_CGI_AUTHORIZATION': 'Authorization',
59                'CONTENT_LENGTH': 'Content-Length',
60                'CONTENT_TYPE': 'Content-Type',
61                'REMOTE_HOST': 'Remote-Host',
62                'REMOTE_ADDR': 'Remote-Addr',
63                }
64
65 def translate_headers(environ):
66     """Translate CGI-environ header names to HTTP header names."""
67     for cgiName in environ:
68         translatedHeader = headerNames.get(cgiName.upper())
69         if translatedHeader:
70             yield translatedHeader, environ[cgiName]
71         elif cgiName.upper().startswith("HTTP_"):
72             # Hackish attempt at recovering original header names.
73             translatedHeader = cgiName[5:].replace("_", "-")
74             yield translatedHeader, environ[cgiName]
75
76
77 class NullWriter(object):
78    
79     def write(self, data):
80         pass
81
82
83 def wsgiApp(environ, start_response):
84     """The WSGI 'application object' for CherryPy."""
85    
86     # Trap screen output from BaseHTTPRequestHandler.log_message()
87     if not cherrypy.config.get('server.logToScreen'):
88         sys.stderr = NullWriter()
89    
90     try:
91         cherrypy.request.purge__()
92         cherrypy.response.purge__()
93        
94         # LOGON_USER is served by IIS, and is the name of the
95         # user after having been mapped to a local account.
96         # Both IIS and Apache set REMOTE_USER, when possible.
97         cherrypy.request.login = (environ.get('LOGON_USER')
98                                   or environ.get('REMOTE_USER') or None)
99         cherrypy.request.multithread = environ['wsgi.multithread']
100         cherrypy.request.multiprocess = environ['wsgi.multiprocess']
101         clientAddr = (
102             environ.get('REMOTE_ADDR', ''),
103             int(environ.get('REMOTE_PORT', -1))
104         )
105         cherrypy.server.request(clientAddr,
106                                 environ.get('REMOTE_ADDR', ''),
107                                 requestLine(environ),
108                                 translate_headers(environ),
109                                 environ['wsgi.input'],
110                                 environ['wsgi.url_scheme'],
111                                 )
112     except (KeyboardInterrupt, SystemExit):
113         raise
114     except:
115         tb = _cputil.formatExc()
116         cherrypy.log(tb)
117         defaultOn = (cherrypy.config.get('server.environment') == 'development')
118         if not cherrypy.config.get("server.showTracebacks", defaultOn):
119             tb = ""
120         s, h, b = _cphttptools.bareError(tb)
121         exc = sys.exc_info()
122     else:
123         response = cherrypy.response
124         s, h, b = response.status, response.headers, response.body
125         exc = None
126    
127     try:
128         start_response(s, h, exc)
129         for chunk in b:
130             # WSGI requires all data to be of type "str". This coercion should
131             # not take any time at all if chunk is already of type "str".
132             # If it's unicode, it could be a big performance hit (x ~500).
133             chunk = str(chunk)
134             yield chunk
135     except (KeyboardInterrupt, SystemExit):
136         raise
137     except:
138         tb = _cputil.formatExc()
139         cherrypy.log(tb)
140         s, h, b = _cphttptools.bareError()
141         # CherryPy test suite expects bareError body to be output,
142         # so don't call start_response (which, according to PEP 333,
143         # may raise its own error at that point).
144         for chunk in b:
145             yield str(chunk)
146
147
148
149 # Server components.
150
151 class WSGIServer(server):
152    
153     """Wrapper for _cpwsgiserver.CherryPyWSGIServer.
154     
155     _cpwsgiserver has been designed to not reference CherryPy in any way,
156     so that it can be used in other frameworks and applications. Therefore,
157     we wrap it here.
158     
159     """
160    
161     def __init__(self):
162         conf = cherrypy.config.get
163         server.__init__(self,
164                         (conf("server.socketHost"), conf("server.socketPort")),
165                         wsgiApp,
166                         conf("server.threadPool"),
167                         conf("server.socketHost"),
168                         config = cherrypy.config
169                         )
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets