Commit | Line | Data |
---|---|---|
173e0e9e FT |
1 | """WSGI handler for serving chained WSGI modules from physical files |
2 | ||
7ed9b82b FT |
3 | The WSGI handler in this module ensures that the SCRIPT_FILENAME |
4 | variable is properly set in every request and points out a file that | |
5 | exists and is readable. It then dispatches the request in one of two | |
6 | ways: If the header X-Ash-Python-Handler is set in the request, its | |
7 | value is used as the name of a handler object to dispatch the request | |
8 | to; otherwise, the file extension of the SCRIPT_FILENAME is used to | |
9 | determine the handler object. | |
10 | ||
11 | The name of a handler object is specified as a string, which is split | |
12 | along its last constituent dot. The part left of the dot is the name | |
13 | of a module, which is imported; and the part right of the dot is the | |
14 | name of an object in that module, which should be a callable adhering | |
15 | to the WSGI specification. Alternatively, the module part may be | |
16 | omitted (such that the name is a string with no dots), in which case | |
17 | the handler object is looked up from this module. | |
18 | ||
19 | By default, this module will handle files with the extensions `.wsgi' | |
2d4ab435 | 20 | or `.wsgi3' using the `chain' handler, which chainloads such files and |
7ed9b82b FT |
21 | runs them as independent WSGI applications. See its documentation for |
22 | details. | |
173e0e9e FT |
23 | |
24 | This module itself contains both an `application' and a `wmain' | |
25 | object. If this module is used by ashd-wsgi(1) or scgi-wsgi(1) so that | |
26 | its wmain function is called, arguments can be specified to it to | |
27 | install handlers for other file extensions. Such arguments take the | |
7ed9b82b FT |
28 | form `.EXT=HANDLER', where EXT is the file extension to be handled, |
29 | and HANDLER is a handler name, as described above. For example, the | |
30 | argument `.fpy=my.module.foohandler' can be given to pass requests for | |
31 | `.fpy' files to the function `foohandler' in the module `my.module' | |
32 | (which must, of course, be importable). When writing such handler | |
33 | functions, you may want to use the getmod() function in this module. | |
173e0e9e FT |
34 | """ |
35 | ||
58ee5c4a | 36 | import sys, os, threading, types, logging, importlib, getopt |
173e0e9e FT |
37 | from . import wsgiutil |
38 | ||
5c1a2105 | 39 | __all__ = ["application", "wmain", "getmod", "cachedmod", "chain"] |
173e0e9e | 40 | |
58ee5c4a FT |
41 | log = logging.getLogger("wsgidir") |
42 | ||
173e0e9e FT |
43 | class cachedmod(object): |
44 | """Cache entry for modules loaded by getmod() | |
45 | ||
46 | Instances of this class are returned by the getmod() | |
47 | function. They contain three data attributes: | |
48 | * mod - The loaded module | |
49 | * lock - A threading.Lock object, which can be used for | |
50 | manipulating this instance in a thread-safe manner | |
51 | * mtime - The time the file was last modified | |
52 | ||
53 | Additional data attributes can be arbitrarily added for recording | |
54 | any meta-data about the module. | |
55 | """ | |
7fe08a6f | 56 | def __init__(self, mod = None, mtime = -1): |
173e0e9e FT |
57 | self.lock = threading.Lock() |
58 | self.mod = mod | |
59 | self.mtime = mtime | |
60 | ||
173e0e9e FT |
61 | modcache = {} |
62 | cachelock = threading.Lock() | |
63 | ||
64 | def mangle(path): | |
65 | ret = "" | |
66 | for c in path: | |
67 | if c.isalnum(): | |
68 | ret += c | |
69 | else: | |
70 | ret += "_" | |
71 | return ret | |
72 | ||
73 | def getmod(path): | |
74 | """Load the given file as a module, caching it appropriately | |
75 | ||
76 | The given file is loaded and compiled into a Python module. The | |
77 | compiled module is cached and returned upon subsequent requests | |
78 | for the same file, unless the file has changed (as determined by | |
79 | its mtime), in which case the cached module is discarded and the | |
80 | new file contents are reloaded in its place. | |
81 | ||
82 | The return value is an instance of the cachedmod class, which can | |
83 | be used for locking purposes and for storing arbitrary meta-data | |
84 | about the module. See its documentation for details. | |
85 | """ | |
86 | sb = os.stat(path) | |
2037cee2 | 87 | with cachelock: |
173e0e9e FT |
88 | if path in modcache: |
89 | entry = modcache[path] | |
7fe08a6f | 90 | else: |
b8d56e8f | 91 | entry = [threading.Lock(), None] |
7fe08a6f | 92 | modcache[path] = entry |
b8d56e8f FT |
93 | with entry[0]: |
94 | if entry[1] is None or sb.st_mtime > entry[1].mtime: | |
7fe08a6f FT |
95 | with open(path, "rb") as f: |
96 | text = f.read() | |
97 | code = compile(text, path, "exec") | |
98 | mod = types.ModuleType(mangle(path)) | |
99 | mod.__file__ = path | |
100 | exec(code, mod.__dict__) | |
b8d56e8f FT |
101 | entry[1] = cachedmod(mod, sb.st_mtime) |
102 | return entry[1] | |
173e0e9e | 103 | |
e9817fee FT |
104 | class handler(object): |
105 | def __init__(self): | |
106 | self.lock = threading.Lock() | |
107 | self.handlers = {} | |
108 | self.exts = {} | |
109 | self.addext("wsgi", "chain") | |
110 | self.addext("wsgi3", "chain") | |
111 | ||
112 | def resolve(self, name): | |
113 | with self.lock: | |
114 | if name in self.handlers: | |
115 | return self.handlers[name] | |
116 | p = name.rfind('.') | |
117 | if p < 0: | |
118 | return globals()[name] | |
119 | mname = name[:p] | |
120 | hname = name[p + 1:] | |
121 | mod = importlib.import_module(mname) | |
122 | ret = getattr(mod, hname) | |
123 | self.handlers[name] = ret | |
124 | return ret | |
125 | ||
126 | def addext(self, ext, handler): | |
127 | self.exts[ext] = self.resolve(handler) | |
128 | ||
129 | def handle(self, env, startreq): | |
130 | if not "SCRIPT_FILENAME" in env: | |
131 | return wsgiutil.simpleerror(env, startreq, 500, "Internal Error", "The server is erroneously configured.") | |
132 | path = env["SCRIPT_FILENAME"] | |
7ed9b82b | 133 | if not os.access(path, os.R_OK): |
e9817fee | 134 | return wsgiutil.simpleerror(env, startreq, 500, "Internal Error", "The server is erroneously configured.") |
7ed9b82b FT |
135 | if "HTTP_X_ASH_PYTHON_HANDLER" in env: |
136 | handler = self.resolve(env["HTTP_X_ASH_PYTHON_HANDLER"]) | |
137 | else: | |
138 | base = os.path.basename(path) | |
139 | p = base.rfind('.') | |
140 | if p < 0: | |
141 | return wsgiutil.simpleerror(env, startreq, 500, "Internal Error", "The server is erroneously configured.") | |
142 | ext = base[p + 1:] | |
143 | if not ext in self.exts: | |
144 | return wsgiutil.simpleerror(env, startreq, 500, "Internal Error", "The server is erroneously configured.") | |
145 | handler = self.exts[ext] | |
146 | return handler(env, startreq) | |
e9817fee FT |
147 | |
148 | def wmain(*argv): | |
149 | """Main function for ashd(7)-compatible WSGI handlers | |
150 | ||
151 | Returns the `application' function. If any arguments are given, | |
152 | they are parsed according to the module documentation. | |
153 | """ | |
6085469b FT |
154 | hnd = handler() |
155 | ret = hnd.handle | |
156 | ||
157 | opts, args = getopt.getopt(argv, "-V") | |
158 | for o, a in opts: | |
159 | if o == "-V": | |
160 | import wsgiref.validate | |
161 | ret = wsgiref.validate.validator(ret) | |
162 | ||
163 | for arg in args: | |
e9817fee FT |
164 | if arg[0] == '.': |
165 | p = arg.index('=') | |
6085469b FT |
166 | hnd.addext(arg[1:p], arg[p + 1:]) |
167 | return ret | |
e9817fee | 168 | |
173e0e9e | 169 | def chain(env, startreq): |
7ed9b82b FT |
170 | """Chain-loading WSGI handler |
171 | ||
172 | This handler loads requested files, compiles them and loads them | |
173 | into their own modules. The compiled modules are cached and reused | |
174 | until the file is modified, in which case the previous module is | |
175 | discarded and the new file contents are loaded into a new module | |
176 | in its place. When chaining such modules, an object named `wmain' | |
177 | is first looked for and called with no arguments if found. The | |
178 | object it returns is then used as the WSGI application object for | |
179 | that module, which is reused until the module is reloaded. If | |
180 | `wmain' is not found, an object named `application' is looked for | |
181 | instead. If found, it is used directly as the WSGI application | |
182 | object. | |
183 | """ | |
173e0e9e | 184 | path = env["SCRIPT_FILENAME"] |
58ee5c4a FT |
185 | try: |
186 | mod = getmod(path) | |
187 | except Exception: | |
188 | log.error("Exception occurred when loading %s" % path, exc_info=sys.exc_info()) | |
189 | return wsgiutil.simpleerror(env, startreq, 500, "Internal Error", "Could not load WSGI handler.") | |
173e0e9e FT |
190 | entry = None |
191 | if mod is not None: | |
2037cee2 | 192 | with mod.lock: |
173e0e9e FT |
193 | if hasattr(mod, "entry"): |
194 | entry = mod.entry | |
195 | else: | |
196 | if hasattr(mod.mod, "wmain"): | |
197 | entry = mod.mod.wmain() | |
198 | elif hasattr(mod.mod, "application"): | |
199 | entry = mod.mod.application | |
200 | mod.entry = entry | |
173e0e9e FT |
201 | if entry is not None: |
202 | return entry(env, startreq) | |
203 | return wsgiutil.simpleerror(env, startreq, 500, "Internal Error", "Invalid WSGI handler.") | |
173e0e9e | 204 | |
e9817fee | 205 | application = handler().handle |