本文整理汇总了Python中werkzeug.http.is_resource_modified函数的典型用法代码示例。如果您正苦于以下问题:Python is_resource_modified函数的具体用法?Python is_resource_modified怎么用?Python is_resource_modified使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_resource_modified函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_is_resource_modified
def test_is_resource_modified(self):
env = create_environ()
# ignore POST
env["REQUEST_METHOD"] = "POST"
assert not http.is_resource_modified(env, etag="testing")
env["REQUEST_METHOD"] = "GET"
# etagify from data
self.assert_raises(TypeError, http.is_resource_modified, env, data="42", etag="23")
env["HTTP_IF_NONE_MATCH"] = http.generate_etag(b"awesome")
assert not http.is_resource_modified(env, data=b"awesome")
env["HTTP_IF_MODIFIED_SINCE"] = http.http_date(datetime(2008, 1, 1, 12, 30))
assert not http.is_resource_modified(env, last_modified=datetime(2008, 1, 1, 12, 00))
assert http.is_resource_modified(env, last_modified=datetime(2008, 1, 1, 13, 00))
示例2: make_conditional
def make_conditional(self, request_or_environ):
"""Make the response conditional to the request. This method works
best if an etag was defined for the response already. The `add_etag`
method can be used to do that. If called without etag just the date
header is set.
This does nothing if the request method in the request or enviorn is
anything but GET or HEAD.
It does not remove the body of the response because that's something
the `__call__` function does for us automatically.
Returns self so that you can do ``return resp.make_conditional(req)``
but modifies the object in-place.
"""
environ = getattr(request_or_environ, 'environ', request_or_environ)
if environ['REQUEST_METHOD'] not in ('GET', 'HEAD'):
return
self.headers['Date'] = http_date()
if 'content-length' in self.headers:
self.headers['Content-Length'] = len(self.data)
if not is_resource_modified(environ, self.headers.get('etag'), None,
self.headers.get('last-modified')):
self.status_code = 304
return self
示例3: make_conditional
def make_conditional(self, request_or_environ):
environ = _get_environ(request_or_environ)
if environ['REQUEST_METHOD'] in ('GET', 'HEAD'):
self.headers['Date'] = http_date()
if 'content-length' in self.headers:
self.headers['Content-Length'] = len(self.data)
if not is_resource_modified(environ, self.headers.get('etag'), None, self.headers.get('last-modified')):
self.status_code = 304
return self
示例4: startupnews
def startupnews():
dt = models.LastUpdated.get('startupnews')
if dt and not is_resource_modified(request.environ, None, None, last_modified=dt):
return Response(status=304)
resp = Response(render_template(
'startupnews.html',
news_list=models.StartupNews.query.order_by('rank').all(),
last_updated=dt))
set_cache(resp, dt)
return resp
示例5: validate_cache
def validate_cache(request):
etag = sha1(repr(sorted(request.cache_key.items()))).hexdigest()
mod_time = request.cache_key.get("modified")
if request.method != "GET":
return etag, mod_time
if not is_resource_modified(request.environ, etag=etag, last_modified=mod_time):
raise NotModified()
if request.if_none_match == etag:
raise NotModified()
return etag, mod_time
示例6: test_is_resource_modified
def test_is_resource_modified(self):
env = create_environ()
# ignore POST
env['REQUEST_METHOD'] = 'POST'
assert not http.is_resource_modified(env, etag='testing')
env['REQUEST_METHOD'] = 'GET'
# etagify from data
self.assert_raises(TypeError, http.is_resource_modified, env,
data='42', etag='23')
env['HTTP_IF_NONE_MATCH'] = http.generate_etag(b'awesome')
assert not http.is_resource_modified(env, data=b'awesome')
env['HTTP_IF_MODIFIED_SINCE'] = http.http_date(datetime(2008, 1, 1, 12, 30))
assert not http.is_resource_modified(env,
last_modified=datetime(2008, 1, 1, 12, 00))
assert http.is_resource_modified(env,
last_modified=datetime(2008, 1, 1, 13, 00))
示例7: __call__
def __call__(self, environ, start_response):
cleaned_path = get_path_info(environ)
if PY2:
cleaned_path = cleaned_path.encode(sys.getfilesystemencoding())
# sanitize the path for non unix systems
cleaned_path = cleaned_path.strip('/')
for sep in os.sep, os.altsep:
if sep and sep != '/':
cleaned_path = cleaned_path.replace(sep, '/')
path = '/' + '/'.join(x for x in cleaned_path.split('/')
if x and x != '..')
file_loader = None
for search_path, loader in iteritems(self.exports):
if search_path == path:
real_filename, file_loader = loader(None)
if file_loader is not None:
break
if not search_path.endswith('/'):
search_path += '/'
if path.startswith(search_path):
real_filename, file_loader = loader(path[len(search_path):])
if file_loader is not None:
break
if file_loader is None or not self.is_allowed(real_filename):
return self.app(environ, start_response)
guessed_type = mimetypes.guess_type(real_filename)
mime_type = guessed_type[0] or self.fallback_mimetype
f, mtime, file_size = file_loader()
headers = [('Date', http_date())]
if self.cache:
timeout = self.cache_timeout
etag = self.generate_etag(mtime, file_size, real_filename)
headers += [
('Etag', '"%s"' % etag),
('Cache-Control', 'max-age=%d, public' % timeout)
]
if not is_resource_modified(environ, etag, last_modified=mtime):
f.close()
start_response('304 Not Modified', headers)
return []
headers.append(('Expires', http_date(time() + timeout)))
else:
headers.append(('Cache-Control', 'public'))
headers.extend((
('Content-Type', mime_type),
('Content-Length', str(file_size)),
('Last-Modified', http_date(mtime))
))
start_response('200 OK', headers)
return wrap_file(environ, f)
示例8: test_is_resource_modified_for_range_requests
def test_is_resource_modified_for_range_requests(self):
env = create_environ()
env["HTTP_IF_MODIFIED_SINCE"] = http.http_date(datetime(2008, 1, 1, 12, 30))
env["HTTP_IF_RANGE"] = http.generate_etag(b"awesome_if_range")
# Range header not present, so If-Range should be ignored
assert not http.is_resource_modified(
env,
data=b"not_the_same",
ignore_if_range=False,
last_modified=datetime(2008, 1, 1, 12, 30),
)
env["HTTP_RANGE"] = ""
assert not http.is_resource_modified(
env, data=b"awesome_if_range", ignore_if_range=False
)
assert http.is_resource_modified(
env, data=b"not_the_same", ignore_if_range=False
)
env["HTTP_IF_RANGE"] = http.http_date(datetime(2008, 1, 1, 13, 30))
assert http.is_resource_modified(
env, last_modified=datetime(2008, 1, 1, 14, 00), ignore_if_range=False
)
assert not http.is_resource_modified(
env, last_modified=datetime(2008, 1, 1, 13, 30), ignore_if_range=False
)
assert http.is_resource_modified(
env, last_modified=datetime(2008, 1, 1, 13, 30), ignore_if_range=True
)
示例9: handle_cache
def handle_cache(response):
"""On 302 redirect, set no-cache headers. If resource is the same, return 304."""
if response.status_code == 302:
response.headers['Last-Modified'] = datetime.now()
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '-1'
return response
elif response.status_code != 200:
return response
# If we set max-age=0, then make sure the response is not locally cached
if response.cache_control.max_age == 0:
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '-1'
matched = not is_resource_modified(request.environ, etag=response.headers.get('etag'))
unmodified = not is_resource_modified(request.environ, last_modified=response.headers.get('last-modified'))
return_304 = matched or unmodified
return return_304 and Response(status=304) or response
示例10: validate_cache
def validate_cache(keys=None, last_modified=None):
if isinstance(last_modified, (list, tuple)):
last_modified = max(last_modified)
if keys is None or not isinstance(keys, dict):
keys = {'keys': keys}
keys['last_modified'] = last_modified
request._grano_etag = generate_etag(keys)
request._grano_modified = last_modified
if not is_resource_modified(request.environ,
etag=request._grano_etag,
last_modified=last_modified):
raise NotModified()
if request.if_none_match == request._grano_etag:
raise NotModified()
示例11: __call__
def __call__(self, environ, start_response):
cleaned_path = get_path_info(environ)
if PY2:
cleaned_path = cleaned_path.encode(get_filesystem_encoding())
# sanitize the path for non unix systems
cleaned_path = cleaned_path.strip("/")
for sep in os.sep, os.altsep:
if sep and sep != "/":
cleaned_path = cleaned_path.replace(sep, "/")
path = "/" + "/".join(x for x in cleaned_path.split("/") if x and x != "..")
file_loader = None
for search_path, loader in iteritems(self.exports):
if search_path == path:
real_filename, file_loader = loader(None)
if file_loader is not None:
break
if not search_path.endswith("/"):
search_path += "/"
if path.startswith(search_path):
real_filename, file_loader = loader(path[len(search_path) :])
if file_loader is not None:
break
if file_loader is None or not self.is_allowed(real_filename):
return self.app(environ, start_response)
guessed_type = mimetypes.guess_type(real_filename)
mime_type = guessed_type[0] or self.fallback_mimetype
f, mtime, file_size = file_loader()
headers = [("Date", http_date())]
if self.cache:
timeout = self.cache_timeout
etag = self.generate_etag(mtime, file_size, real_filename)
headers += [("Etag", '"%s"' % etag), ("Cache-Control", "max-age=%d, public" % timeout)]
if not is_resource_modified(environ, etag, last_modified=mtime):
f.close()
start_response("304 Not Modified", headers)
return []
headers.append(("Expires", http_date(time() + timeout)))
else:
headers.append(("Cache-Control", "public"))
headers.extend(
(("Content-Type", mime_type), ("Content-Length", str(file_size)), ("Last-Modified", http_date(mtime)))
)
start_response("200 OK", headers)
return wrap_file(environ, f)
示例12: startupnews
def startupnews():
dt = models.LastUpdated.get('startupnews')
if dt and not is_resource_modified(request.environ, None, None, last_modified=dt):
return Response(status=304)
resp = Response(render_template('index.html',
title='Startup News Digest',
news_list=models.StartupNews.query.order_by('rank').all(),
navs=[
('Startup News', 'http://news.dbanotes.net/news'),
('New', 'http://news.dbanotes.net/newest'),
('Comments', 'http://news.dbanotes.net/newcomments'),
('Leaders', 'http://news.dbanotes.net/leaders'),
('Submit', 'http://news.dbanotes.net/submit')],
last_updated = dt and human(dt, 1)
))
set_cache(resp, dt)
return resp
示例13: hackernews
def hackernews():
dt = models.LastUpdated.get('hackernews')
if dt and not is_resource_modified(request.environ, None, None, last_modified=dt):
return Response(status=304)
resp = Response(render_template('index.html',
title='Hacker News Digest',
news_list=models.HackerNews.query.order_by('rank').all(),
navs=[
('Hacker News', 'https://news.ycombinator.com/news'),
('New', 'https://news.ycombinator.com/newest'),
('Comments', 'https://news.ycombinator.com/newcomments'),
('Show', 'https://news.ycombinator.com/show'),
('Ask', 'https://news.ycombinator.com/ask'),
('Jobs', 'https://news.ycombinator.com/jobs'),
('Submit', 'https://news.ycombinator.com/submit')],
last_updated = dt and human(dt, 1)
))
set_cache(resp, dt)
return resp
示例14: prepare_response
def prepare_response(self, request=None):
if request is None:
environ = dict()
else:
environ = request.environ
guessed_type = mimetypes.guess_type(self.filename)
mime_type = guessed_type[0] or 'text/plain'
stream = open(self.filename)
self.headers['Date'] = http_date()
if self.cache_timeout and request:
self.headers.extend((
('Etag', '"{:s}"'.format(self.etag)),
('Cache-Control', 'max-age={:d}, public'.format(
self.cache_timeout)),
))
if not is_resource_modified(environ, self.etag):
stream.close()
self.status_code = 304
return []
self.headers['Expires'] = http_date(time() + self.cache_timeout)
else:
self.headers['Cache-Control'] = 'public'
self.headers.extend((
('Content-Type', mime_type),
('Content-Length', str(self.size)),
('Last-Modified', http_date(self.mtime))
))
if self.as_attachment:
self.headers.set('Content-Disposition', 'attachment',
filename=self.attachment_filename)
return wrap_file(environ, stream)
示例15: filedown
def filedown(environ, filename, cache=True, cache_timeout=None, download=False, inline=False, real_filename=None):
guessed_type = mimetypes.guess_type(filename)
mime_type = guessed_type[0] or 'text/plain'
real_filename = real_filename or filename
f, mtime, file_size = _opener(real_filename)
headers = [('Date', http_date())]
d_filename = os.path.basename(filename)
if download:
headers.append(('Content-Disposition', 'attachment; filename=%s' % d_filename))
if inline:
headers.append(('Content-Disposition', 'inline; filename=%s' % d_filename))
if cache:
etag = _generate_etag(mtime, file_size, real_filename)
headers += [
('ETag', '"%s"' % etag),
]
if cache_timeout:
headers += [
('Cache-Control', 'max-age=%d, public' % cache_timeout),
('Expires', http_date(time() + cache_timeout))
]
if not is_resource_modified(environ, etag, last_modified=mtime):
f.close()
return Response(status=304, headers=headers)
else:
headers.append(('Cache-Control', 'public'))
headers.extend((
('Content-Type', mime_type),
('Content-Length', str(file_size)),
('Last-Modified', http_date(mtime))
))
return Response(wrap_file(environ, f), status=200, headers=headers,
direct_passthrough=True)