/usr/share/pyshared/cherrypy/_cpwsgi.py is in python-cherrypy 2.3.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | """A WSGI application interface (see PEP 333)."""
try:
GeneratorExit
except NameError:
GeneratorExit = None
import sys
import cherrypy
from cherrypy import _cputil, _cpwsgiserver, _cpwsgiserver3
from cherrypy.lib import httptools
def requestLine(environ):
"""Rebuild first line of the request (e.g. "GET /path HTTP/1.0")."""
resource = httptools.urljoin(environ.get('SCRIPT_NAME', ''),
environ.get('PATH_INFO', ''))
if resource == "/*":
resource = "*"
elif not resource.startswith("/"):
resource = "/" + resource
qString = environ.get('QUERY_STRING')
if qString:
resource += '?' + qString
resource = resource.replace(" ", "%20")
return ('%s %s %s' % (environ['REQUEST_METHOD'],
resource or '/',
environ['SERVER_PROTOCOL']
)
)
headerNames = {'HTTP_CGI_AUTHORIZATION': 'Authorization',
'CONTENT_LENGTH': 'Content-Length',
'CONTENT_TYPE': 'Content-Type',
'REMOTE_HOST': 'Remote-Host',
'REMOTE_ADDR': 'Remote-Addr',
}
def translate_headers(environ):
"""Translate CGI-environ header names to HTTP header names."""
for cgiName in environ:
translatedHeader = headerNames.get(cgiName.upper())
if translatedHeader:
yield translatedHeader, environ[cgiName]
elif cgiName.upper().startswith("HTTP_"):
# Hackish attempt at recovering original header names.
translatedHeader = cgiName[5:].replace("_", "-")
yield translatedHeader, environ[cgiName]
class NullWriter(object):
def write(self, data):
pass
class ResponseIter(object):
def __init__(self, request, body):
self.body = body
self.request = request
def __iter__(self):
if not self.body:
raise StopIteration
try:
for chunk in self.body:
# WSGI requires all data to be of type "str". This coercion should
# not take any time at all if chunk is already of type "str".
# If it's unicode, it could be a big performance hit (x ~500).
if not isinstance(chunk, str):
chunk = chunk.encode("ISO-8859-1")
yield chunk
except (KeyboardInterrupt, SystemExit), ex:
raise ex
except GeneratorExit:
raise
except:
cherrypy.log(traceback=True)
s, h, b = _cputil.bareError()
# CherryPy test suite expects bareError body to be output,
# so don't call start_response (which, according to PEP 333,
# may raise its own error at that point).
for chunk in b:
# WSGI requires all data to be of type "str". This coercion should
# not take any time at all if chunk is already of type "str".
# If it's unicode, it could be a big performance hit (x ~500).
if not isinstance(chunk, str):
chunk = chunk.encode("ISO-8859-1")
yield chunk
def close(self):
try:
if self.request:
self.request.close()
except:
cherrypy.log(traceback=True)
self.request = None
def wsgiApp(environ, start_response):
"""The WSGI 'application object' for CherryPy."""
# Trap screen output from BaseHTTPRequestHandler.log_message()
if not cherrypy.config.get('server.log_to_screen'):
sys.stderr = NullWriter()
request = None
try:
env = environ.get
clientAddr = (env('REMOTE_ADDR', ''), int(env('REMOTE_PORT', -1)))
request = cherrypy.server.request(clientAddr, '', environ['wsgi.url_scheme'])
# LOGON_USER is served by IIS, and is the name of the
# user after having been mapped to a local account.
# Both IIS and Apache set REMOTE_USER, when possible.
request.login = (env('LOGON_USER') or env('REMOTE_USER') or None)
request.multithread = environ['wsgi.multithread']
request.multiprocess = environ['wsgi.multiprocess']
request.wsgi_environ = environ
response = request.run(requestLine(environ),
translate_headers(environ),
environ['wsgi.input'])
s, h, b = response.status, response.header_list, response.body
exc = None
except (KeyboardInterrupt, SystemExit), ex:
try:
if request:
request.close()
except:
cherrypy.log(traceback=True)
request = None
raise ex
except:
if cherrypy.config.get("server.throw_errors", False):
if request:
request.close()
request = None
raise
tb = _cputil.formatExc()
cherrypy.log(tb)
if not cherrypy.config.get("server.show_tracebacks", False):
tb = ""
s, h, b = _cputil.bareError(tb)
exc = sys.exc_info()
start_response(s, h, exc)
return ResponseIter(request, b)
# Server components.
class CPHTTPRequest(_cpwsgiserver.HTTPRequest):
def __init__(self, socket, addr, server):
_cpwsgiserver.HTTPRequest.__init__(self, socket, addr, server)
mhs = int(cherrypy.config.get('server.max_request_header_size',
500 * 1024))
if mhs > 0:
self.rfile = httptools.SizeCheckWrapper(self.rfile, mhs)
def parse_request(self):
try:
_cpwsgiserver.HTTPRequest.parse_request(self)
except httptools.MaxSizeExceeded:
msg = "Request Entity Too Large"
proto = self.environ.get("SERVER_PROTOCOL", "HTTP/1.0")
self.wfile.write("%s 413 %s\r\n" % (proto, msg))
self.wfile.write("Content-Length: %s\r\n\r\n" % len(msg))
self.wfile.write(msg)
self.wfile.flush()
self.ready = False
cherrypy.log(traceback=True)
else:
if self.ready:
if isinstance(self.rfile, httptools.SizeCheckWrapper):
# Unwrap the rfile
self.rfile = self.rfile.rfile
self.environ["wsgi.input"] = self.rfile
class WSGIServer(_cpwsgiserver.CherryPyWSGIServer):
"""Wrapper for _cpwsgiserver.CherryPyWSGIServer.
_cpwsgiserver has been designed to not reference CherryPy in any way,
so that it can be used in other frameworks and applications. Therefore,
we wrap it here, so we can set our own mount points from cherrypy.tree.
"""
RequestHandlerClass = CPHTTPRequest
def __init__(self):
conf = cherrypy.config.get
sockFile = cherrypy.config.get('server.socket_file')
if sockFile:
bind_addr = sockFile
else:
bind_addr = (conf("server.socket_host"), conf("server.socket_port"))
pts = cherrypy.tree.mount_points
if pts:
apps = [(base, wsgiApp) for base in pts.keys()]
else:
apps = [("", wsgiApp)]
s = _cpwsgiserver.CherryPyWSGIServer
s.__init__(self, bind_addr, apps,
conf("server.thread_pool"),
conf("server.socket_host"),
request_queue_size = conf('server.socket_queue_size'),
timeout = conf('server.socket_timeout'),
)
# Server3 components #
class CPHTTPRequest3(_cpwsgiserver3.HTTPRequest):
def parse_request(self):
mhs = int(cherrypy.config.get('server.max_request_header_size',
500 * 1024))
if mhs > 0:
self.rfile = httptools.SizeCheckWrapper(self.rfile, mhs)
try:
_cpwsgiserver3.HTTPRequest.parse_request(self)
except httptools.MaxSizeExceeded:
self.simple_response("413 Request Entity Too Large")
cherrypy.log(traceback=True)
else:
if self.ready:
if isinstance(self.rfile, httptools.SizeCheckWrapper):
# Unwrap the rfile
self.rfile = self.rfile.rfile
self.environ["wsgi.input"] = self.rfile
def decode_chunked(self):
"""Decode the 'chunked' transfer coding."""
if isinstance(self.rfile, httptools.SizeCheckWrapper):
self.rfile = self.rfile.rfile
mbs = int(cherrypy.config.get('server.max_request_body_size',
100 * 1024 * 1024))
if mbs > 0:
self.rfile = httptools.SizeCheckWrapper(self.rfile, mbs)
try:
return _cpwsgiserver3.HTTPRequest.decode_chunked(self)
except httptools.MaxSizeExceeded:
self.simple_response("413 Request Entity Too Large")
cherrypy.log(traceback=True)
return False
class CPHTTPConnection3(_cpwsgiserver3.HTTPConnection):
RequestHandlerClass = CPHTTPRequest3
class CPWSGIServer3(_cpwsgiserver3.CherryPyWSGIServer):
"""Wrapper for _cpwsgiserver3.CherryPyWSGIServer.
wsgiserver has been designed to not reference CherryPy in any way,
so that it can be used in other frameworks and applications. Therefore,
we wrap it here, so we can set our own mount points from cherrypy.tree.
"""
ConnectionClass = CPHTTPConnection3
def __init__(self):
conf = cherrypy.config.get
sockFile = cherrypy.config.get('server.socket_file')
if sockFile:
bind_addr = sockFile
else:
bind_addr = (conf("server.socket_host"), conf("server.socket_port"))
pts = cherrypy.tree.mount_points
if pts:
apps = [(base, wsgiApp) for base in pts.keys()]
else:
apps = [("", wsgiApp)]
s = _cpwsgiserver3.CherryPyWSGIServer
s.__init__(self, bind_addr, apps,
conf("server.thread_pool"),
conf("server.socket_host"),
request_queue_size = conf('server.socket_queue_size'),
timeout = conf('server.socket_timeout'),
)
self.protocol = conf("server.protocol_version")
self.ssl_certificate = conf("server.ssl_certificate")
self.ssl_private_key = conf("server.ssl_private_key")
|