本文整理汇总了Python中werkzeug.http.parse_cache_control_header函数的典型用法代码示例。如果您正苦于以下问题:Python parse_cache_control_header函数的具体用法?Python parse_cache_control_header怎么用?Python parse_cache_control_header使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse_cache_control_header函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_static_file
def test_static_file(self):
app = flask.Flask(__name__)
# default cache timeout is 12 hours
with app.test_request_context():
rv = app.send_static_file('index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 12 * 60 * 60)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3600
with app.test_request_context():
rv = app.send_static_file('index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 3600)
# override get_send_file_options with some new values and check them
class StaticFileApp(flask.Flask):
def get_send_file_options(self, filename):
opts = super(StaticFileApp, self).get_send_file_options(filename)
opts['cache_timeout'] = 10
# this test catches explicit inclusion of the conditional
# keyword arg in the guts
opts['conditional'] = True
return opts
app = StaticFileApp(__name__)
with app.test_request_context():
rv = app.send_static_file('index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 10)
示例2: test_static_file
def test_static_file(self):
app = flask.Flask(__name__)
# default cache timeout is 12 hours (hard-coded)
with app.test_request_context():
rv = app.send_static_file("index.html")
cc = parse_cache_control_header(rv.headers["Cache-Control"])
self.assert_equal(cc.max_age, 12 * 60 * 60)
# override get_static_file_options with some new values and check them
class StaticFileApp(flask.Flask):
def __init__(self):
super(StaticFileApp, self).__init__(__name__)
def get_static_file_options(self, filename):
opts = super(StaticFileApp, self).get_static_file_options(filename)
opts["cache_timeout"] = 10
# this test catches explicit inclusion of the conditional
# keyword arg in the guts
opts["conditional"] = True
return opts
app = StaticFileApp()
with app.test_request_context():
rv = app.send_static_file("index.html")
cc = parse_cache_control_header(rv.headers["Cache-Control"])
self.assert_equal(cc.max_age, 10)
示例3: fix_headers
def fix_headers(self, environ, headers, status=None):
if self.fix_vary:
header = headers.get("content-type", "")
mimetype, options = parse_options_header(header)
if mimetype not in ("text/html", "text/plain", "text/sgml"):
headers.pop("vary", None)
if self.fix_attach and "content-disposition" in headers:
pragma = parse_set_header(headers.get("pragma", ""))
pragma.discard("no-cache")
header = pragma.to_header()
if not header:
headers.pop("pragma", "")
else:
headers["Pragma"] = header
header = headers.get("cache-control", "")
if header:
cc = parse_cache_control_header(header, cls=ResponseCacheControl)
cc.no_cache = None
cc.no_store = False
header = cc.to_header()
if not header:
headers.pop("cache-control", "")
else:
headers["Cache-Control"] = header
示例4: fix_headers
def fix_headers(self, environ, headers, status=None):
if self.fix_vary:
header = headers.get('content-type', '')
mimetype, options = parse_options_header(header)
if mimetype not in ('text/html', 'text/plain', 'text/sgml'):
headers.pop('vary', None)
if self.fix_attach and 'content-disposition' in headers:
pragma = parse_set_header(headers.get('pragma', ''))
pragma.discard('no-cache')
header = pragma.to_header()
if not header:
headers.pop('pragma', '')
else:
headers['Pragma'] = header
header = headers.get('cache-control', '')
if header:
cc = parse_cache_control_header(header,
cls=ResponseCacheControl)
cc.no_cache = None
cc.no_store = False
header = cc.to_header()
if not header:
headers.pop('cache-control', '')
else:
headers['Cache-Control'] = header
示例5: cache_control
def cache_control(self):
def on_update(cache_control):
if not cache_control and "cache-control" in self.headers:
del self.headers["cache-control"]
elif cache_control:
self.headers["Cache-Control"] = cache_control.to_header()
return parse_cache_control_header(self.headers.get("cache-control"), on_update, ResponseCacheControl)
示例6: get_cache_timeout
def get_cache_timeout(r):
# Cache the weather data at a rate that the upstream service dictates, but
# that also keeps us easily within the 1000 requests per day free limit.
header = r.headers.get('Cache-Control')
control = parse_cache_control_header(header, cls=ResponseCacheControl)
try:
return max(control.max_age, CACHE_MIN_SECONDS)
except TypeError:
return CACHE_MIN_SECONDS
示例7: test_cache_control_header
def test_cache_control_header(self):
cc = http.parse_cache_control_header("max-age=0, no-cache")
assert cc.max_age == 0
assert cc.no_cache
cc = http.parse_cache_control_header('private, community="UCI"', None, datastructures.ResponseCacheControl)
assert cc.private
assert cc["community"] == "UCI"
c = datastructures.ResponseCacheControl()
assert c.no_cache is None
assert c.private is None
c.no_cache = True
assert c.no_cache == "*"
c.private = True
assert c.private == "*"
del c.private
assert c.private is None
assert c.to_header() == "no-cache"
示例8: test_cache_control_header
def test_cache_control_header(self):
cc = http.parse_cache_control_header('max-age=0, no-cache')
assert cc.max_age == 0
assert cc.no_cache
cc = http.parse_cache_control_header('private, community="UCI"', None,
datastructures.ResponseCacheControl)
assert cc.private
assert cc['community'] == 'UCI'
c = datastructures.ResponseCacheControl()
assert c.no_cache is None
assert c.private is None
c.no_cache = True
assert c.no_cache == '*'
c.private = True
assert c.private == '*'
del c.private
assert c.private is None
assert c.to_header() == 'no-cache'
示例9: get_ff_cache
def get_ff_cache(profile_dir, store_body=False):
cache_dir = os.path.join(profile_dir, "Cache")
if not os.path.isdir(cache_dir):
return [] # Firefox updated the cache dir structure since our study
cache_map = os.path.join(cache_dir, "_CACHE_MAP_")
cache_dump = os.path.join(BASE_TMP_DIR, append_timestamp("cache") +
rand_str())
create_dir(cache_dump)
subprocess.call([PERL_PATH, CACHE_PERL_SCRIPT, cache_map, "--recover=" +
cache_dump])
cache_items = []
db_items = ("Etag", "Request String", "Expires", "Cache-Control")
for fname in glob(os.path.join(cache_dump, "*_metadata")):
item = {}
try:
with open(fname) as f:
metadata = f.read()
item = parse_metadata(metadata)
for db_item in db_items:
if db_item not in item:
item[db_item] = ""
# If a response includes both an Expires header and a max-age
# directive, the max-age directive overrides the Expires header
# (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)
expiry_delta_sec = 0
if "Expires" in item:
# parse expiry date
expiry = parse_date(item["Expires"])
if expiry:
expiry_delta = expiry - datetime.now()
expiry_delta_sec = expiry_delta.total_seconds()
if "Cache-Control:" in item:
# parse max-age directive
cache_directives =\
parse_cache_control_header(item["Cache-Control"],
cls=ResponseCacheControl)
if "max-age" in cache_directives:
expiry_delta_sec = cache_directives["max-age"]
if expiry_delta_sec < DELTA_MONTH:
continue
item["Expiry-Delta"] = expiry_delta_sec
with open(fname[:-9]) as f:
data = f.read()
item["Body"] = data if store_body else "" # store as BLOB
item["Hash"] = hash_text(base64.b64encode(data))
except IOError as exc:
print "Error processing cache: %s: %s" % (exc,
traceback.format_exc())
cache_items.append(item)
if os.path.isdir(cache_dump):
shutil.rmtree(cache_dump)
return cache_items
示例10: cache_control
def cache_control(self):
"""The Cache-Control general-header field is used to specify
directives that MUST be obeyed by all caching mechanisms along the
request/response chain.
"""
def on_update(cache_control):
if not cache_control and 'cache-control' in self.headers:
del self.headers['cache-control']
elif cache_control:
self.headers['Cache-Control'] = cache_control.to_header()
return parse_cache_control_header(self.headers.get('cache-control'),
on_update)
示例11: testStaticFile
def testStaticFile(self):
ConfigManager.removeConfig('development')
ConfigManager.addConfig(testConfig)
app = shimehari.Shimehari(__name__)
app.setStaticFolder('static')
with app.testRequestContext():
rv = app.sendStaticFile('index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assertEqual(cc.max_age, 12 * 60 * 60)
rv = shimehari.sendFile(os.path.join(app.rootPath, 'static/index.html'))
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assertEqual(cc.max_age, 12 * 60 * 60)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3600
with app.testRequestContext():
rv = app.sendStaticFile('index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assertEqual(cc.max_age, 3600)
rv = shimehari.sendFile(os.path.join(app.rootPath, 'static/index.html'))
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assertEqual(cc.max_age, 3600)
class StaticFileApp(shimehari.Shimehari):
def getSendFileMaxAge(self, filename):
return 10
app = StaticFileApp(__name__)
app.setStaticFolder('static')
with app.testRequestContext():
rv = app.sendStaticFile('index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assertEqual(cc.max_age, 10)
rv = shimehari.sendFile(os.path.join(app.rootPath, 'static/index.html'))
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assertEqual(cc.max_age, 10)
示例12: test_static_file
def test_static_file(self):
app = flask.Flask(__name__)
# default cache timeout is 12 hours
with app.test_request_context():
# Test with static file handler.
rv = app.send_static_file('index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 12 * 60 * 60)
# Test again with direct use of send_file utility.
rv = flask.send_file('static/index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 12 * 60 * 60)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3600
with app.test_request_context():
# Test with static file handler.
rv = app.send_static_file('index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 3600)
# Test again with direct use of send_file utility.
rv = flask.send_file('static/index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 3600)
class StaticFileApp(flask.Flask):
def get_send_file_max_age(self, filename):
return 10
app = StaticFileApp(__name__)
with app.test_request_context():
# Test with static file handler.
rv = app.send_static_file('index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 10)
# Test again with direct use of send_file utility.
rv = flask.send_file('static/index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 10)
示例13: _get_cache_control_directive
def _get_cache_control_directive(self, name):
success, the_values = self._enumerate_header("cache-control")
if success:
cc = parse_cache_control_header(the_values,
cls=ResponseCacheControl)
if name == "max-age" and cc.max_age is not None:
return True, timedelta(seconds=int(cc.max_age))
elif name == "stale-while-revalidate" and \
cc.stale_while_revalidate is not None:
return True, timedelta(seconds=int(cc.stale_while_revalidate))
else:
return False, None
else:
return False, None
示例14: test_templates_and_static
def test_templates_and_static(self):
from blueprintapp import app
c = app.test_client()
rv = c.get('/')
self.assert_equal(rv.data, b'Hello from the Frontend')
rv = c.get('/admin/')
self.assert_equal(rv.data, b'Hello from the Admin')
rv = c.get('/admin/index2')
self.assert_equal(rv.data, b'Hello from the Admin')
rv = c.get('/admin/static/test.txt')
self.assert_equal(rv.data.strip(), b'Admin File')
rv.close()
rv = c.get('/admin/static/css/test.css')
self.assert_equal(rv.data.strip(), b'/* nested file */')
rv.close()
# try/finally, in case other tests use this app for Blueprint tests.
max_age_default = app.config['SEND_FILE_MAX_AGE_DEFAULT']
try:
expected_max_age = 3600
if app.config['SEND_FILE_MAX_AGE_DEFAULT'] == expected_max_age:
expected_max_age = 7200
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = expected_max_age
rv = c.get('/admin/static/css/test.css')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, expected_max_age)
rv.close()
finally:
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = max_age_default
with app.test_request_context():
self.assert_equal(flask.url_for('admin.static', filename='test.txt'),
'/admin/static/test.txt')
with app.test_request_context():
try:
flask.render_template('missing.html')
except TemplateNotFound as e:
self.assert_equal(e.name, 'missing.html')
else:
self.assert_true(0, 'expected exception')
with flask.Flask(__name__).test_request_context():
self.assert_equal(flask.render_template('nested/nested.txt'), 'I\'m nested')
示例15: surrogate_control
def surrogate_control(self):
"""
The Cache-Control general-header field is used to specify
directives that MUST be obeyed by all caching mechanisms along the
request/response chain.
"""
def on_update(surrogate_control):
if not surrogate_control and "surrogate-control" in self.headers:
del self.headers["surrogate-control"]
elif surrogate_control: # pragma: no cover
self.headers["Surrogate-Control"] = \
surrogate_control.to_header()
return parse_cache_control_header(
self.headers.get("surrogate-control"),
on_update,
ResponseCacheControl,
)