#!/usr/bin/python -S import os, sys real_script = "/var/www/html/aetest/index.cgi" def main(): SCRIPT_NAME = os.environ['SCRIPT_NAME'] PATH_INFO = os.environ.get('PATH_INFO', '') or '__index__' SCRIPT_FILENAME = os.environ['SCRIPT_FILENAME'] REQUEST_METHOD = os.environ['REQUEST_METHOD'] cache = os.path.join(real_script + "-cache", PATH_INFO.strip("/")) # print >>sys.stderr, "cache=%r" % cache dep = os.path.join(cache, "_dep") cache = os.path.join(cache, "_cache") if cache_valid(cache, dep): cache = open(cache, "rb").read() if REQUEST_METHOD == 'HEAD': cache = headers(cache) open("hitlog", "a").write("hit %r %r %r %r\n" % ( REQUEST_METHOD, PATH_INFO, os.environ.get("QUERY_STRING", None), open(dep, "rb").read().split('\0'))) sys.stdout.write(cache) else: try: os.unlink(cache) except os.error: pass open("hitlog", "a").write("miss %r %r %r %s\n" % ( REQUEST_METHOD, PATH_INFO, os.environ.get("QUERY_STRING", None), reason)) sys.stdout.write(cache) os.execv(real_script, sys.argv) def headers(x): i = x.find("\n\n") if i == -1: return x return x[:i+2] def cache_valid(cache, dep): global reason reason = "" if os.environ.get("QUERY_STRING", ""): reason = "QUERY_STRING" return False if os.environ['REQUEST_METHOD'] not in ["HEAD", "GET"]: reason = "REQUEST_METHOD" return False if not os.path.exists(cache): resaon = "cache exists" return False if not os.path.exists(dep): reason = "dep exists" return False deptime = os.stat(dep).st_mtime dep = open(dep).read().split('\0') for d in dep: try: dt = os.stat(d).st_mtime except os.error: reason = "error opening %r" % d return False # File no longer exists if dt > deptime: reason = "mtime %r on %r newer than %r" % (dt, d, deptime) return False return True main() # vim:sw=4:sts=4:et