def __set_response_expires(self, op_name, expires, max_age=None):
"""Used to set expiration headers on a response dynamically
based on the name of the operation.
'op_name' is a string containing the name of the depot
operation as listed in the REPO_OPS_* constants.
'expires' is an integer value in seconds indicating how
long from when the request was made the content returned
should expire. The maximum value is 365*86400.
'max_age' is an integer value in seconds indicating the
maximum length of time a response should be considered
valid. For some operations, the maximum value for this
parameter is equal to the repository's refresh_seconds
property."""
prefix = self._get_req_pub()
if not prefix:
prefix = self.repo.cfg.get_property("publisher",
"prefix")
rs = None
if prefix:
try:
pub = self.repo.get_publisher(prefix)
except Exception as e:
# Couldn't get pub.
pass
else:
repo = pub.repository
if repo:
rs = repo.refresh_seconds
if rs is None:
rs = 14400
if max_age is None:
max_age = min((rs, expires))
now = cherrypy.response.time
if op_name == "publisher" or op_name == "search" or \
op_name == "catalog":
# For these operations, cap the value based on
# refresh_seconds.
expires = now + min((rs, max_age))
max_age = min((rs, max_age))
else:
expires = now + expires
headers = cherrypy.response.headers
headers["Cache-Control"] = \
"must-revalidate, no-transform, max-age={0:d}".format(
max_age)
headers["Expires"] = formatdate(timeval=expires, usegmt=True)
python类response()的实例源码
def repo_index(self, *tokens, **params):
"""Generate a page showing the list of repositories served by
this Apache instance."""
self.setup(cherrypy.request)
# In order to reuse the pkg.depotd shtml files, we need to use
# the pkg.server.api, which means passing a DepotBUI object,
# despite the fact that we're not serving content for any one
# repository. For the purposes of rendering this page, we'll
# use the first object we come across.
depot = depot_buis[list(depot_buis.keys())[0]]
accept_lang = self.get_accept_lang(cherrypy.request, depot)
cherrypy.request.path_info = "/{0}".format(accept_lang)
tlookup = mako.lookup.TemplateLookup(
directories=[depot.web_root])
pub = None
base = pkg.server.api.BaseInterface(cherrypy.request, depot,
pub)
# build a list of all repositories URIs and BUI links,
# and a dictionary of publishers for each repository URI
repo_list = []
repo_pubs = {}
for repo_prefix in repositories.keys():
repo = repositories[repo_prefix]
depot = depot_buis[repo_prefix]
repo_url = "{0}/{1}".format(cherrypy.request.base,
repo_prefix)
bui_link = "{0}/{1}/index.shtml".format(
repo_prefix, accept_lang)
repo_list.append((repo_url, bui_link))
repo_pubs[repo_url] = \
[(pub, "{0}/{1}/{2}".format(
cherrypy.request.base, repo_prefix,
pub)) for pub in repo.publishers]
repo_list.sort()
template = tlookup.get_template("repos.shtml")
# Starting in CherryPy 3.2, cherrypy.response.body only allows
# bytes.
return misc.force_bytes(template.render(g_vars={"base": base,
"pub": None, "http_depot": "true", "lang": accept_lang,
"repo_list": repo_list, "repo_pubs": repo_pubs
}))