def request(self, event, request, response): if self.path is not None and not request.path.startswith(self.path): return req = event path = request.path if self.path is not None: path = path[len(self.path):] path = path.strip("/") filename = None if path: filename = os.path.abspath(os.path.join(self.docroot, path)) else: for default in self.defaults: filename = os.path.abspath(os.path.join(self.docroot, default)) if os.path.exists(filename): break if filename and os.path.exists(filename): expires(request, response, 3600*24*30) return serve_file(request, response, filename)
def expires(self, secs=0, force=False): tools.expires(self.request, self.response, secs, force)
def request(self, request, response): if self.path is not None and not request.path.startswith(self.path): return path = request.path if self.path is not None: path = path[len(self.path):] path = path.strip("/") if path: location = os.path.abspath(os.path.join(self.docroot, path)) else: location = os.path.abspath(os.path.join(self.docroot, ".")) if not os.path.exists(location): return # Is it a file we can serve directly? if os.path.isfile(location): expires(request, response, 3600 * 24 * 30) return serve_file(request, response, location) # Is it a directory? elif os.path.isdir(location): # Try to serve one of default files first.. for default in self.defaults: location = os.path.abspath( os.path.join(self.docroot, path, default)) if os.path.exists(location): expires(request, response, 3600 * 24 * 30) return serve_file(request, response, location) # .. serve a directory listing if allowed to. if self.dirlisting: directory = os.path.abspath(os.path.join(self.docroot, path)) cur_dir = os.path.join(self.path, path) if self.path else "" if not path: url_up = "" else: if self.path is None: url_up = os.path.join("/", os.path.split(path)[0]) else: url_up = os.path.join(cur_dir, "..") url_up = '<li><a href="%s">%s</a></li>' % (url_up, "..") listing = [] for item in os.listdir(directory): if not item.startswith("."): url = os.path.join("/", path, cur_dir, item) location = os.path.abspath( os.path.join(self.docroot, path, item)) if os.path.isdir(location): li = '<li><a href="%s/">%s/</a></li>' % (url, item) else: li = '<li><a href="%s">%s</a></li>' % (url, item) listing.append(li) ctx = {} ctx["directory"] = cur_dir or os.path.join("/", cur_dir, path) ctx["url_up"] = url_up ctx["listing"] = "\n".join(listing) return _dirlisting_template.safe_substitute(ctx)
def request(self, request, response): if self.path is not None and not request.path.startswith(self.path): return path = request.path if self.path is not None: path = path[len(self.path):] path = path.strip("/") if path: location = os.path.abspath(os.path.join(self.docroot, path)) else: location = os.path.abspath(os.path.join(self.docroot, ".")) if not os.path.exists(location): return # Is it a file we can serve directly? if os.path.isfile(location): expires(request, response, 3600*24*30) return serve_file(request, response, location) # Is it a directory? elif os.path.isdir(location): # Try to serve one of default files first.. for default in self.defaults: location = os.path.abspath( os.path.join(self.docroot, path, default)) if os.path.exists(location): expires(request, response, 3600*24*30) return serve_file(request, response, location) # .. serve a directory listing if allowed to. if self.dirlisting: directory = os.path.abspath(os.path.join(self.docroot, path)) cur_dir = os.path.join(self.path, path) if self.path else "" if not path: url_up = "" else: if self.path is None: url_up = os.path.join("/", os.path.split(path)[0]) else: url_up = os.path.join(cur_dir, "..") url_up = '<li><a href="%s">%s</a></li>' % (url_up, "..") listing = [] for item in os.listdir(directory): if not item.startswith("."): url = os.path.join("/", path, cur_dir, item) location = os.path.abspath( os.path.join(self.docroot, path, item)) if os.path.isdir(location): li = '<li><a href="%s/">%s/</a></li>' % (url, item) else: li = '<li><a href="%s">%s</a></li>' % (url, item) listing.append(li) ctx = {} ctx["directory"] = cur_dir or os.path.join("/", cur_dir, path) ctx["url_up"] = url_up ctx["listing"] = "\n".join(listing) return _dirlisting_template.safe_substitute(ctx)