WSGI middleware for serving static files.
| 240 | |
| 241 | |
| 242 | class StaticMiddleware: |
| 243 | """WSGI middleware for serving static files.""" |
| 244 | |
| 245 | def __init__(self, app, prefix="/static/"): |
| 246 | self.app = app |
| 247 | self.prefix = prefix |
| 248 | |
| 249 | def __call__(self, environ, start_response): |
| 250 | path = environ.get("PATH_INFO", "") |
| 251 | path = self.normpath(path) |
| 252 | |
| 253 | if path.startswith(self.prefix): |
| 254 | return StaticApp(environ, start_response) |
| 255 | else: |
| 256 | return self.app(environ, start_response) |
| 257 | |
| 258 | def normpath(self, path): |
| 259 | path2 = posixpath.normpath(unquote(path)) |
| 260 | if path.endswith("/"): |
| 261 | path2 += "/" |
| 262 | return path2 |
| 263 | |
| 264 | |
| 265 | class LogMiddleware: |