/usr/share/pyshared/circuits/web/wsgi.py is in python-circuits 2.1.0-2.
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 | # Module: wsgi
# Date: 6th November 2008
# Author: James Mills, prologic at shortcircuit dot net dot au
"""WSGI Components
This module implements WSGI Components.
"""
try:
from urllib.parse import unquote
except ImportError:
from urllib import unquote # NOQA
from operator import itemgetter
from traceback import format_tb
from sys import exc_info as _exc_info
from circuits.tools import tryimport
from circuits.core import handler, BaseComponent
StringIO = tryimport(("cStringIO", "StringIO", "io"), "StringIO")
from . import wrappers
from .http import HTTP
from .events import Request
from .headers import Headers
from .errors import HTTPError
from .dispatchers import Dispatcher
def create_environ(errors, path, req):
environ = {}
env = environ.__setitem__
env("REQUEST_METHOD", req.method)
env("SERVER_NAME", req.host.split(":", 1)[0])
env("SERVER_PORT", "%i" % (req.server.port or 0))
env("SERVER_PROTOCOL", "HTTP/%d.%d" % req.server_protocol)
env("QUERY_STRING", req.qs)
env("SCRIPT_NAME", req.script_name)
env("CONTENT_TYPE", req.headers.get("Content-Type", ""))
env("CONTENT_LENGTH", req.headers.get("Content-Length", ""))
env("REMOTE_ADDR", req.remote.ip)
env("REMOTE_PORT", "%i" % (req.remote.port or 0))
env("wsgi.version", (1, 0))
env("wsgi.input", req.body)
env("wsgi.errors", errors)
env("wsgi.multithread", False)
env("wsgi.multiprocess", False)
env("wsgi.run_once", False)
env("wsgi.url_scheme", req.scheme)
if req.path:
req.script_name = req.path[:len(path)]
req.path = req.path[len(path):]
env("SCRIPT_NAME", req.script_name)
env("PATH_INFO", req.path)
for k, v in list(req.headers.items()):
env("HTTP_%s" % k.upper().replace("-", "_"), v)
return environ
class Application(BaseComponent):
channel = "web"
headerNames = {
"HTTP_CGI_AUTHORIZATION": "Authorization",
"CONTENT_LENGTH": "Content-Length",
"CONTENT_TYPE": "Content-Type",
"REMOTE_HOST": "Remote-Host",
"REMOTE_ADDR": "Remote-Addr",
}
def init(self):
self._finished = False
HTTP().register(self)
Dispatcher().register(self)
def translateHeaders(self, environ):
for cgiName in environ:
# We assume all incoming header keys are uppercase already.
if cgiName in self.headerNames:
yield self.headerNames[cgiName], environ[cgiName]
elif cgiName[:5] == "HTTP_":
# Hackish attempt at recovering original header names.
translatedHeader = cgiName[5:].replace("_", "-")
yield translatedHeader, environ[cgiName]
def getRequestResponse(self, environ):
env = environ.get
headers = Headers(list(self.translateHeaders(environ)))
protocol = tuple(map(int, env("SERVER_PROTOCOL")[5:].split(".")))
request = wrappers.Request(
None,
env("REQUEST_METHOD"),
env("wsgi.url_scheme"),
env("PATH_INFO", ""),
protocol,
env("QUERY_STRING", "")
)
request.server = None
request.remote = wrappers.Host(env("REMOTE_ADDR"), env("REMTOE_PORT"))
request.headers = headers
request.script_name = env("SCRIPT_NAME")
request.wsgi_environ = environ
try:
cl = int(headers.get("Content-Length", "0"))
except:
cl = 0
request.body.write(env("wsgi.input").read(cl))
request.body.seek(0)
response = wrappers.Response(request)
response.gzip = "gzip" in request.headers.get("Accept-Encoding", "")
return request, response
def __call__(self, environ, start_response, exc_info=None):
self.request, self.response = self.getRequestResponse(environ)
self.fire(Request(self.request, self.response))
self._finished = False
while self or not self._finished:
self.tick()
self.response.prepare()
body = self.response.body
status = self.response.status
headers = list(self.response.headers.items())
start_response(status, headers, exc_info)
return body
@handler("response", filter=True, channel="web")
def response(self, response):
self._finished = True
return True
class _Empty(str):
def __bool__(self):
return True
__nonzero__ = __bool__
empty = _Empty()
del _Empty
class Gateway(BaseComponent):
channel = "web"
def init(self, apps):
self.apps = apps
self.errors = dict((k, StringIO()) for k in self.apps.keys())
@handler("request", filter=True, priority=0.2)
def _on_request(self, event, request, response):
if not self.apps:
return
parts = request.path.split("/")
candidates = []
for i in range(len(parts)):
k = "/".join(parts[:(i + 1)]) or "/"
if k in self.apps:
candidates.append((k, self.apps[k]))
candidates = sorted(candidates, key=itemgetter(0), reverse=True)
if not candidates:
return
path, app = candidates[0]
buffer = StringIO()
def start_response(status, headers, exc_info=None):
response.code = int(status.split(" ", 1)[0])
for header in headers:
response.headers.add_header(*header)
return buffer.write
errors = self.errors[path]
environ = create_environ(errors, path, request)
try:
body = app(environ, start_response)
if isinstance(body, list):
body = "".join(body)
if not body:
if not buffer.tell():
return empty
else:
buffer.seek(0)
return buffer
else:
return body
except Exception as error:
etype, evalue, etraceback = _exc_info()
error = (etype, evalue, format_tb(etraceback))
return HTTPError(request, response, 500, error=error)
|