diff options
author | Donald Stufft <donald@stufft.io> | 2013-06-22 12:47:03 -0400 |
---|---|---|
committer | Donald Stufft <donald@stufft.io> | 2013-06-22 12:47:03 -0400 |
commit | 5e3c1e6fb41ac00fa4f74ff2c5950f72277d667c (patch) | |
tree | 7ed4d2664153b80cbbc2101fc88d48c487ef1634 | |
parent | 08a589da873c5178e1118722e03d318fab212779 (diff) | |
download | decorator-5e3c1e6fb41ac00fa4f74ff2c5950f72277d667c.tar.gz |
Make a CacheControlMiddleware that adds Cache-Control directives
-rw-r--r-- | pypi.wsgi | 23 |
1 files changed, 23 insertions, 0 deletions
@@ -41,6 +41,25 @@ class Request: self.start_response(self.status, self.headers) +class CacheControlMiddleware(object): + + def __init__(self, app): + self.app = app + + def __call__(self, environ, start_response): + + def _start_response(status, headers, exc_info=None): + script = environ.get("SCRIPT_NAME", None) + if script in set(["/simple", "/serversig", "/packages"]): + headers += [("Cache-Control", "max-age=86400, public")] + else: + headers += [("Cache-Control", "private")] + + return start_response(status, headers, exc_info) + + return self.app(environ, _start_response) + + def debug(environ, start_response): if environ['PATH_INFO'].startswith("/auth") and \ "HTTP_AUTHORIZATION" not in environ: @@ -68,6 +87,10 @@ def application(environ, start_response): #application=debug +# Handle Caching at the WSGI layer +application = CacheControlMiddleware(application) + + # pretend to be like the UWSGI configuration - set SCRIPT_NAME to the first # part of the PATH_INFO if it's valid and remove that part from the PATH_INFO def site_fake(app, environ, start_response): |