def test_ie_fixes(self): @fixers.InternetExplorerFix @Request.application def application(request): response = Response('binary data here', mimetype='application/vnd.ms-excel') response.headers['Vary'] = 'Cookie' response.headers[ 'Content-Disposition'] = 'attachment; filename=foo.xls' return response c = Client(application, Response) response = c.get( '/', headers=[('User-Agent', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)')]) # IE gets no vary assert response.data == 'binary data here' assert 'vary' not in response.headers assert response.headers[ 'content-disposition'] == 'attachment; filename=foo.xls' assert response.headers['content-type'] == 'application/vnd.ms-excel' # other browsers do c = Client(application, Response) response = c.get('/') assert response.data == 'binary data here' assert 'vary' in response.headers cc = ResponseCacheControl() cc.no_cache = True @fixers.InternetExplorerFix @Request.application def application(request): response = Response('binary data here', mimetype='application/vnd.ms-excel') response.headers['Pragma'] = ', '.join(pragma) response.headers['Cache-Control'] = cc.to_header() response.headers[ 'Content-Disposition'] = 'attachment; filename=foo.xls' return response # IE has no pragma or cache control pragma = ('no-cache', ) c = Client(application, Response) response = c.get( '/', headers=[('User-Agent', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)')]) assert response.data == 'binary data here' assert 'pragma' not in response.headers assert 'cache-control' not in response.headers assert response.headers[ 'content-disposition'] == 'attachment; filename=foo.xls' # IE has simplified pragma pragma = ('no-cache', 'x-foo') cc.proxy_revalidate = True response = c.get( '/', headers=[('User-Agent', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)')]) assert response.data == 'binary data here' assert response.headers['pragma'] == 'x-foo' assert response.headers['cache-control'] == 'proxy-revalidate' assert response.headers[ 'content-disposition'] == 'attachment; filename=foo.xls' # regular browsers get everything response = c.get('/') assert response.data == 'binary data here' assert response.headers['pragma'] == 'no-cache, x-foo' cc = parse_cache_control_header(response.headers['cache-control'], cls=ResponseCacheControl) assert cc.no_cache assert cc.proxy_revalidate assert response.headers[ 'content-disposition'] == 'attachment; filename=foo.xls'
def test_ie_fixes(self): @fixers.InternetExplorerFix @Request.application def application(request): response = Response('binary data here', mimetype='application/vnd.ms-excel') response.headers['Vary'] = 'Cookie' response.headers['Content-Disposition'] = 'attachment; filename=foo.xls' return response c = Client(application, Response) response = c.get('/', headers=[ ('User-Agent', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)') ]) # IE gets no vary assert response.get_data() == b'binary data here' assert 'vary' not in response.headers assert response.headers['content-disposition'] == 'attachment; filename=foo.xls' assert response.headers['content-type'] == 'application/vnd.ms-excel' # other browsers do c = Client(application, Response) response = c.get('/') assert response.get_data() == b'binary data here' assert 'vary' in response.headers cc = ResponseCacheControl() cc.no_cache = True @fixers.InternetExplorerFix @Request.application def application(request): response = Response('binary data here', mimetype='application/vnd.ms-excel') response.headers['Pragma'] = ', '.join(pragma) response.headers['Cache-Control'] = cc.to_header() response.headers['Content-Disposition'] = 'attachment; filename=foo.xls' return response # IE has no pragma or cache control pragma = ('no-cache',) c = Client(application, Response) response = c.get('/', headers=[ ('User-Agent', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)') ]) assert response.get_data() == b'binary data here' assert 'pragma' not in response.headers assert 'cache-control' not in response.headers assert response.headers['content-disposition'] == 'attachment; filename=foo.xls' # IE has simplified pragma pragma = ('no-cache', 'x-foo') cc.proxy_revalidate = True response = c.get('/', headers=[ ('User-Agent', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)') ]) assert response.get_data() == b'binary data here' assert response.headers['pragma'] == 'x-foo' assert response.headers['cache-control'] == 'proxy-revalidate' assert response.headers['content-disposition'] == 'attachment; filename=foo.xls' # regular browsers get everything response = c.get('/') assert response.get_data() == b'binary data here' assert response.headers['pragma'] == 'no-cache, x-foo' cc = parse_cache_control_header(response.headers['cache-control'], cls=ResponseCacheControl) assert cc.no_cache assert cc.proxy_revalidate assert response.headers['content-disposition'] == 'attachment; filename=foo.xls'
def cache_control(self, value: ResponseCacheControl) -> None: self._set_or_pop_header("Cache-Control", value.to_header())
def on_update(cache_control: ResponseCacheControl) -> None: 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()
def test_ie_fixes(self): @fixers.InternetExplorerFix @Request.application def application(request): response = Response("binary data here", mimetype="application/vnd.ms-excel") response.headers["Vary"] = "Cookie" response.headers[ "Content-Disposition"] = "attachment; filename=foo.xls" return response c = Client(application, Response) response = c.get( "/", headers=[("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)")], ) # IE gets no vary assert response.get_data() == b"binary data here" assert "vary" not in response.headers assert response.headers[ "content-disposition"] == "attachment; filename=foo.xls" assert response.headers["content-type"] == "application/vnd.ms-excel" # other browsers do c = Client(application, Response) response = c.get("/") assert response.get_data() == b"binary data here" assert "vary" in response.headers cc = ResponseCacheControl() cc.no_cache = True @fixers.InternetExplorerFix @Request.application def application(request): response = Response("binary data here", mimetype="application/vnd.ms-excel") response.headers["Pragma"] = ", ".join(pragma) response.headers["Cache-Control"] = cc.to_header() response.headers[ "Content-Disposition"] = "attachment; filename=foo.xls" return response # IE has no pragma or cache control pragma = ("no-cache", ) c = Client(application, Response) response = c.get( "/", headers=[("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)")], ) assert response.get_data() == b"binary data here" assert "pragma" not in response.headers assert "cache-control" not in response.headers assert response.headers[ "content-disposition"] == "attachment; filename=foo.xls" # IE has simplified pragma pragma = ("no-cache", "x-foo") cc.proxy_revalidate = True response = c.get( "/", headers=[("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)")], ) assert response.get_data() == b"binary data here" assert response.headers["pragma"] == "x-foo" assert response.headers["cache-control"] == "proxy-revalidate" assert response.headers[ "content-disposition"] == "attachment; filename=foo.xls" # regular browsers get everything response = c.get("/") assert response.get_data() == b"binary data here" assert response.headers["pragma"] == "no-cache, x-foo" cc = parse_cache_control_header(response.headers["cache-control"], cls=ResponseCacheControl) assert cc.no_cache assert cc.proxy_revalidate assert response.headers[ "content-disposition"] == "attachment; filename=foo.xls"
class Handler(object): """ Represents the capabilities of methods on resources, used by the mapper to determine how to handle requests """ SAFE = False EMBED = False EXPIRES = False REDIRECT = None VISIBLE = True CACHE = ResponseCacheControl() def __init__(self, handler=None): if handler: self.safe = handler.safe self.embed = handler.embed self.expires = handler.expires self.cache = handler.cache self.visible = handler.visible else: self.safe = self.SAFE self.embed = self.EMBED self.redirect = self.REDIRECT self.cache = self.CACHE self.visible = self.VISIBLE @staticmethod def parse(resource, data): return parse(data) @staticmethod def dump(resource, data, resolver, inline): return CONTENT_TYPE, dump(data, resolver, inline) @classmethod def is_safe(cls, m): try: return m.__glyph_method__.safe except StandardError: if m.__name__ == 'GET': return True return cls.SAFE @classmethod def is_embed(cls, m): try: return m.__glyph_method__.embed except StandardError: return cls.EMBED @classmethod def is_redirect(cls, m): try: return m.__glyph_method__.redirect is not None except StandardError: return cls.REDIRECT @classmethod def is_visible(cls, m, name): try: return m.__glyph_method__.visible except StandardError: return not name.startswith('_') @staticmethod def redirect_code(m): return m.__glyph_method__.redirect @classmethod def make_link(cls, m): if cls.is_safe(m): if cls.is_embed(m): return embedlink(m, content=m()) else: return link(m) else: return form(m) @classmethod def call(cls, attr, request, router): verb = request.method if verb == 'GET' and cls.is_safe(attr): result = attr() elif verb == 'POST' and not cls.is_safe(attr): try: data = collections.OrderedDict( cls.parse(attr, get_stream(request))) except StandardError: raise raise BadRequest() result = attr(**data) else: raise MethodNotAllowed() if result is None: return Response('', status='204 None') else: if cls.is_redirect(attr): try: url = router.url(result) except LookupError: pass else: return Redirect(url, code=cls.redirect_code(attr)) content_type, result = cls.dump(attr, result, router.url, router.inline) return Response(result, content_type=content_type)